如何向C#应用程序公开C ++方法? [英] How do I expose a C++ method to my C# application?

查看:165
本文介绍了如何向C#应用程序公开C ++方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习VC ++.我创建了一个具有阻止/允许IP地址功能的应用程序,我想将此功能公开给C#应用程序.

I am currently learning VC++. I have created an application that has functionality to block/allow IP addresses and I would like to expose this functionality to a C# application.

我在头文件中定义了以下成员,这些成员引用了.cpp文件中的方法,这些方法需要在应用程序外部访问.

I have defined the following members in my header file, which reference methods in my .cpp file which need to be accessible outside my application.

public:

// Constructor.
ZizFilter();

// Destructor.
~ZizFilter();

BOOL StartFirewall();
BOOL StopFirewall();

BOOL AddIPAddressToBlockedList(char* IP)
BOOL RemoveIPAddressFromBlockedList(char* IP)

BOOL BlockAll(char* tunnelAddress);
BOOL UnblockAll();

我了解C#Interop方面以及如何使用公开的汇编方法,但是我不知道如何使我的C ++应用程序公开公开这些方法.

I understand the C# Interop side and how to consume exposed assembly methods, but I don't know how to get my C++ application to publicly expose the methods.

推荐答案

一种解决方法是在C ++ CLI中编写包装器-这是c ++的扩展版本,可以执行托管的.net事情.

One way to handle this is to write a wrapper in C++ CLI - this is the version of c++ that is extended to do managed .net things.

您可以在C ++ cli中创建托管类或引用"类,该类将作为c#的常规.net类出现.

You can create a managed or "ref" class in C++ cli that will appear as a normal .net class to c#.

在c ++ cli类中,您可以正常调用纯c ++类.

Within the c++ cli class you can call your pure c++ class as normal.

这篇关于如何向C#应用程序公开C ++方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆