gpt4 book ai didi

c# - 从 C++/CLI 函数调用 C# 函数

转载 作者:行者123 更新时间:2023-11-28 02:26:18 25 4
gpt4 key购买 nike

我的项目要求我使用 C# 为 C++ 提供用户界面。我调用的其中一个 C++ 函数执行大量工作并通过另一个“对象”提供定期进度更新。这是我的意思的一个例子。

C++

class AppDelegate : public ProgressDelegate
{
void AppDelegate::UpdateStatusText(const char* text)
{
// Go() will end up calling me at some point.
OutputDebugString(text);
}
void AppDelegate::ShowMessage(const char* text)
{
// Go() will end up calling me at some point.
OutputDebugString(text);
}
};

int CppWrapper::Go()
{
return cppInstance->Go()
}

CSharp

void UpdateStatusText(String text)
{
//update UI
}
void ShowMessage(String text)
{
//update UI
}

我想要做的是获取 updateStatusText 和 ShowMessage 并将文本传递给 C# 以更新我的 UI。我的问题是如何公开适当的 C# 方法,以便我的 C++ 代码可以调用它们?请注意,修改 Go 不是我的选择。

最佳答案

也许这个例子可以帮助你:

编写托管 DLL

要创建一个简单的托管 DLL,它有一个公共(public)方法来添加两个数字并返回结果,请按照下列步骤操作:

启动 Microsoft Visual Studio .NET 或 Microsoft Visual Studio 2005。在"file"菜单上,指向“新建”,然后单击“项目”。 “新建项目”对话框打开。在项目类型下,单击 Visual C# 项目。

注意 在 Visual Studio 2005 中,单击项目类型下的 Visual C#。在模板下,单击类库。在“名称”文本框中,键入 sManagedDLL,然后单击“确定”。在代码 View 中打开 Class1.cs 文件。要声明具有将两个数字相加的方法的公共(public)接口(interface),请将以下代码添加到 Class1.cs 文件:

// Interface declaration.
public interface ICalculator
{
int Add(int Number1, int Number2);
};

要在类中实现此公共(public)接口(interface),请将以下代码添加到 Class1.cs 文件中:

// Interface implementation.
public class ManagedClass:ICalculator
{
public int Add(int Number1,int Number2)
{
return Number1+Number2;
}
}

注册托管 DLL 以用于 COM 或 native C++要将托管 DLL 与 COM 或 native C++ 一起使用,您必须在 Windows 注册表中注册 DLL 的程序集信息。为此,请按照下列步骤操作:

从 native C++ 代码调用托管 DLL

// Import the type library.
#import "..\ManagedDLL\bin\Debug\ManagedDLL.tlb" raw_interfaces_only

如果您计算机上的路径与此路径不同,请更改类型库的路径。要声明要使用的命名空间,请将以下代码添加到 CPPClient.cpp 文件中:

using namespace ManagedDLL;

完整代码 list

  //Managed DLL
// Class1.cs
// A simple managed DLL that contains a method to add two numbers.
using System;

namespace ManagedDLL
{
// Interface declaration.
public interface ICalculator
{
int Add(int Number1, int Number2);
};

// Interface implementation.
public class ManagedClass:ICalculator
{
public int Add(int Number1,int Number2)
{
return Number1+Number2;
}
}
}


//C++ Client
// 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;
}

引用: How to call a managed DLL from native Visual C++ code in Visual Studio.NET or in Visual Studio 2005

关于c# - 从 C++/CLI 函数调用 C# 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30539332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com