【问题标题】:creating instance for a class in a dll在 dll 中为类创建实例
【发布时间】:2016-02-17 14:31:21
【问题描述】:

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

【问题讨论】:

  • 那么在你将对象传递给c# dll之后你会做什么呢?
  • 我会把它传递给xml序列化函数。我想设置对象中的所有数据并将其传递给 dll。 Dll 将使用它进行 xml 序列化。

标签: c# c++ dll managed


【解决方案1】:

在您提供的链接中,在 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 "..\ManagedDLL\bin\Debug\ManagedDLL.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 %d\n", lResult);


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

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

【讨论】:

  • 我的 dll 中有一些其他类。我可以为这些类创建对象吗?我也需要访问数据属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
相关资源
最近更新 更多