观察这个最简单的IDL文件:
import "unknwn.idl";
typedef struct _MyStruct
{
DWORD len;
[size_is(len)] BYTE *buffer;
} MyStruct;
[
object,
uuid(903C11E8-46A0-4E6E-B54C-6619B6A42CCB),
pointer_default(unique)
]
interface IMyIntf : IUnknown
{
[local]
HRESULT Func([in] MyStruct *pObj);
};
[
uuid(BAC6289C-503C-4ADD-B27A-4F22D726C109),
version(1.0),
]
library Lib
{
importlib("stdole2.tlb");
[
uuid(76D40083-C27F-45FB-88DC-C3E7DF9B5988)
]
coclass MyClass
{
[default] interface IMyIntf;
};
};
以下是我编译它的方法:
z:\Work>del 1.tlb
z:\Work>midl /W1 /nologo /char signed /env win32 /Oicf /tlb "1.tlb" /robust 1.idl
Processing .\1.idl
1.idl
Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\unknwn.idl
unknwn.idl
Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\wtypes.idl
wtypes.idl
Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\basetsd.h
basetsd.h
Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\guiddef.h
guiddef.h
Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\oaidl.idl
oaidl.idl
Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\objidl.idl
objidl.idl
Processing C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\oaidl.acf
oaidl.acf
z:\Work>tlbimp 1.tlb
Microsoft (R) .NET Framework Type Library to Assembly Converter 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.
TlbImp : warning TI3016 : The type library importer could not convert the signature for the member 'Lib._MyStruct.buffer'.
TlbImp : Type library imported to Lib.dll
z:\Work>
注意TlbImp警告。生产的互操作组件是:
namespace Lib
{
[StructLayout(LayoutKind.Sequential, Pack=4), ComConversionLoss]
public struct _MyStruct
{
public uint len;
[ComConversionLoss]
public IntPtr buffer;
}
[ComImport, Guid("903C11E8-46A0-4E6E-B54C-6619B6A42CCB"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IMyIntf
{
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime)]
void Func([In] ref _MyStruct pObj);
}
[ComImport, Guid("903C11E8-46A0-4E6E-B54C-6619B6A42CCB"), CoClass(typeof(MyClassClass))]
public interface MyClass : IMyIntf
{
}
[ComImport, TypeLibType(TypeLibTypeFlags.FCanCreate), ClassInterface(ClassInterfaceType.None), Guid("76D40083-C27F-45FB-88DC-C3E7DF9B5988")]
public class MyClassClass : IMyIntf, MyClass
{
// Methods
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime)]
public virtual extern void Func([In] ref _MyStruct pObj);
}
}
ComConversionLoss
属性的存在让我心疼,所以
我的问题是如何摆脱这种警告?
P.S。 我的问题可能与What causes "The type library importer could not convert the signature for the member" warnings?完全相同。但是,它不是,因为我在命令行编译并且警告不会在任何地方消失。