问题:从 C++ MFC 调用 C# Unmanged COM

Problem: Calling C# Unmanged COM from C++ MFC

我使用 UNMANAGED COM 到 link 使用 tlb 文件的 C# 项目到 MFC C++ 项目。程序 运行s 在主 PC(我编译代码的地方)中正确。但是当我运行程序在另一台电脑(同样是OS,即Win 10)时,出现运行库错误

C# Code

namespace CSharpUnmanaged
{
public interface IGlobalInfo
{
    void multiPly();
    int Sum(int a, int b);
    double SuperPow(int x, int y);

}
public class GlobalInfo : IGlobalInfo
{
    public void multiPly()
    {
        int c = 4 * 5;
        System.IO.File.WriteAllText("D:\test.txt", c.ToString());
        Debug.WriteLine("MultiPly:" + c.ToString());
    }

    public int Sum(int a, int b)
    {
        return a + b;
    }
}
}

C++ MFC Code

#import "CSharpUnmanaged.tlb" named_guids raw_interfaces_only
int main(){
   HRESULT hr = CoInitialize(0);
   globalInfo.CreateInstance(__uuidof(CSharpUnmanaged::GlobalInfo));
   CoUninitialize();
   if (globalInfo == NULL) AfxMessageBox(L"NULL");
   globalInfo->multiPly();
}

I tried to track the error and I'm sure that the problem is related to the globalInfo which stays NULL during running the program.

感谢@SantoshDhanawade...

在目标机器上这个脚本必须 运行:

RegAsm.exe -tlb -codebase yourdll.dll

注意:运行作为管理员的命令提示符。

此脚本创建一个 tlb 文件,必须用原始 tlb 文件替换。

干杯...