在 DLL 中为类创建实例

creating instance for a class in a dll

本文关键字:创建 实例 DLL      更新时间:2023-10-16

如何在 c# dll 中为在 c++ 项目中导入的类创建实例?我已按照此处给出的说明将 c# 库导入 c++。我可以使用公共接口调用函数。
[编辑]我想通过 c++ 为 c# dll 中的类属性设置值,并将其传递给 dll。这样我就可以跳过 c# dll 中的许多设置函数。如果我可以为类创建对象,我将使用该对象设置值并将对象传递给 c# dll。

在您提供的链接中,在 c++ 客户端代码说明中

// CPPClient.cpp: Defines the entry point for the console application.
// C++ client that calls a managed DLL.
#include "stdafx.h"
#include "tchar.h"
// Import the type library.
#import "..ManagedDLLbinDebugManagedDLL.tlb" raw_interfaces_only
using namespace ManagedDLL;
int _tmain(int argc, _TCHAR* argv[])
{
    // Initialize COM.
    HRESULT hr = CoInitialize(NULL);
    // Create the interface pointer.
    ICalculatorPtr pICalc(__uuidof(ManagedClass));
    long lResult = 0;
    // Call the Add method.
    pICalc->Add(5, 10, &lResult);
    wprintf(L"The result is %dn", lResult);

    // Uninitialize COM.
    CoUninitialize();
    return 0;
}

指针的创建,pICalc几乎是类对象的创建。 在行中创建 ICalculatorPtr pICalc(__uuidof(ManagedClass));