3

观察这个最简单的 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让我感觉很糟糕,所以我的问题是如何摆脱这个警告?

PS 我的问题可能与什么原因导致“类型库导入器无法转换成员的签名”警告完全相同?. 但是,事实并非如此,因为我在命令行上编译并且警告不会在任何地方消失。

4

0 回答 0