我需要从正在使用它的C ++程序中注册.NET COM dll。对于早于.NET 4的.NET版本,这在How to run regasm.exe from a C++ program?中有解释。以下是提供旧版CLR路径的最小代码(无检查)。
CComBSTR mscoreeName("mscoree.dll");
HINSTANCE hMscoree = CoLoadLibrary(mscoreeName, FALSE);
typedef HRESULT (WINAPI *LPFNGETCORSYSDIR)(LPWSTR, DWORD, DWORD*);
LPFNGETCORSYSDIR lpfunc = (LPFNGETCORSYSDIR)GetProcAddress(hMscoree,_T("GetCORSystemDirectory"));
DWORD bufferSize = 256;
DWORD bufferUsed;
LPWSTR pwzBuffer = new WCHAR[bufferSize];
(*lpfunc)(pwzBuffer, bufferSize, &bufferUsed);
但是,由于我使用.NET 4,因此GetCORSystemDirectory方法被ICLRRuntimeInfo :: GetRuntimeDirectory取代,后者不是mscoree.dll中的入口点(使用depends检查)。根据{{3}}上的文档,该方法作为MSCorEE.dll中的资源包含在内。
问题是如何从C ++访问此方法?
除此之外,我想知道是否有更简单的方法......
答案 0 :(得分:0)
问题的解决方法是找到RegAsm的正确位置。感谢Hans Passant对使用RegistrationService.RegisterAssembly的评论,我将ClassLibrary更改为自注册的可执行文件。
static void Main(string[] args)
{
if (args.Length != 1)
{
ShowHelpMessage();
return;
}
if (args[0].CompareTo("/register") == 0)
{
Assembly currAssembly = Assembly.GetExecutingAssembly();
var rs = new RegistrationServices();
if (rs.RegisterAssembly(currAssembly, AssemblyRegistrationFlags.SetCodeBase))
{
Console.WriteLine("Succesfully registered " + currAssembly.GetName());
} else
{
Console.WriteLine("Failed to register " + currAssembly.GetName());
}
return;
}
if (args[0].CompareTo("/remove") == 0)
{
Assembly currAssembly = Assembly.GetExecutingAssembly();
var rs = new RegistrationServices();
if (rs.UnregisterAssembly(currAssembly))
{
Console.WriteLine("Succesfully removed " + currAssembly.GetName());
}
else
{
Console.WriteLine("Failed to remove " + currAssembly.GetName());
}
return;
}
ShowHelpMessage();
}