在Visual Studio 2017中,如果我创建一个基本的C#库并将其导出为DLL,然后创建一个TLB,我可以在C ++项目中#import TLB,它似乎可以成功运行。但是,GetRecordInfoFromGuids()始终因E_INVALIDARG而失败。我认为问题是生成的TLH文件在一个地方缺少UUID。这就是我所拥有的:
Base.cs
using System.Windows.Forms;
Namespace Test {
public class Base : IBase {
public void BaseFoo() {
MessageBox.Show("Test");
}
}
}
IBase.cs
Namespace Test {
public inteface IBase {
void BaseFoo();
}
}
TestBase.cpp
#import "path\to\Test.tlb"
void TestBase() {
HRESULT hrCoInit = 0;
hrCoInit = CoInitialize(NULL);
if (!SUCCEEDED(hrCoInit)) { return; }
IRecordInfoPtr recordInfoBase = NULL;
HRESULT hrRIBase = GetRecordInfoFromGuids(
Test::LIBID_Test,
1,
0,
0,
Test::CLSID_Base,
&recordInfoBase
); /* always results in E_INVALIDARG */
}
test.tlh
// Created by Microsoft (R) C/C++ Compiler Version 14.11.25547.0 (8f5b6942).
//
// d:\garre\documents\source\test\testlib\debug\test.tlh
//
// C++ source equivalent of Win32 type library ..\Test\bin\Debug\Test.tlb
// compiler-generated file created 01/29/18 at 22:51:17 - DO NOT EDIT!
#pragma once
#pragma pack(push, 8)
#include <comdef.h>
namespace Test {
//
// Forward references and typedefs
//
struct __declspec(uuid("066dd02f-1f6d-460c-8833-d22344da22e6"))
/* LIBID */ __Test;
/* ************************** Missing UUID Here? ***********************/
struct /* coclass */ Base;
struct __declspec(uuid("d123a47b-b549-3e35-baa6-63fbf1d2e996"))
/* dual interface */ IBase;
struct __declspec(uuid("49a2c4f5-cb61-3192-a3c5-fb90c6bab315"))
/* dual interface */ _Base;
//
// Smart pointer typedef declarations
//
_COM_SMARTPTR_TYPEDEF(IBase, __uuidof(IBase));
_COM_SMARTPTR_TYPEDEF(_Base, __uuidof(_Base));
//
// Type library items
//
struct __declspec(uuid("7b25bdc6-43c0-34db-ac1d-5f4e6a791982"))
Base;
// [ default ] interface _Base
// interface _Object
// interface IBase
struct __declspec(uuid("d123a47b-b549-3e35-baa6-63fbf1d2e996"))
IBase : IDispatch
{
//
// Raw methods provided by interface
//
virtual HRESULT __stdcall BaseFoo ( ) = 0;
};
struct __declspec(uuid("49a2c4f5-cb61-3192-a3c5-fb90c6bab315"))
_Base : IDispatch
{};
//
// Named GUID constants initializations
//
extern "C" const GUID __declspec(selectany) LIBID_Test =
{0x066dd02f,0x1f6d,0x460c,{0x88,0x33,0xd2,0x23,0x44,0xda,0x22,0xe6}};
extern "C" const GUID __declspec(selectany) CLSID_Base =
{0x7b25bdc6,0x43c0,0x34db,{0xac,0x1d,0x5f,0x4e,0x6a,0x79,0x19,0x82}};
extern "C" const GUID __declspec(selectany) IID_IBase =
{0xd123a47b,0xb549,0x3e35,{0xba,0xa6,0x63,0xfb,0xf1,0xd2,0xe9,0x96}};
extern "C" const GUID __declspec(selectany) IID__Base =
{0x49a2c4f5,0xcb61,0x3192,{0xa3,0xc5,0xfb,0x90,0xc6,0xba,0xb3,0x15}};
} // namespace Test
#pragma pack(pop)
我尝试使用__uuidof(Test :: Base),使用不同的LCID,覆盖Base.cs中的UUID类Base [ComVisible(true)],但这些似乎都没有任何效果。我可以使用Test :: BasePtr创建一个实例,所以其他一切似乎都是正确的;我只是无法得到任何引用IRecordInfo的东西。
在设置中设置“使程序集COM-Visible”。我提供了签名的钥匙。使用RegAsm.exe或使用“注册COM互操作”会产生同样的结果。