在C#Visual Studio应用程序中调用C ++ __stdcall函数指针 [英] calling a c++ __stdcall function pointer in c# visual studio application
问题描述
我有一个带有.h和.cpp文件的Visual c ++应用程序.我想在c#应用程序中调用函数指针.我已经插入了C ++应用程序的.h文件.请为此提供帮助
I have a Visual c++ application with .h and .cpp files. I want to call the function pointers in c# application. I have inserted .h file of c++ application. Please help with this
// program.h //
#ifndef ess_cH
#define ess_cH
unsigned long long __stdcall ess_initialization(unsigned short serial);
unsigned char __stdcall ess_close(unsigned long long handle);
unsigned char __stdcall ess_get_serial(unsigned long long handle, unsigned short *serialunsigneshort* version);
推荐答案
如果要在c#中调用c ++ dll,建议您尝试以下步骤.
If you want to call c++dll in c#, I suggest that you could try the following steps.
首先,创建一个c ++ dll并编写以下代码.
First, create a c++ dll and write the following code.
#include "stdafx.h"
#include <Windows.h>
extern "C"
{
__declspec(dllexport) void HelloWorld()
{
MessageBox(0, L"Hello World from DLL!\n", L"Hi", MB_ICONINFORMATION);
}
__declspec(dllexport) void ShowMe()
{
MessageBox(0, L"How are u?", L"Hi", MB_ICONINFORMATION);
}
}
然后,右键单击dll的属性,将公共语言运行时支持更改为 公共语言运行时支持(/clr) >.如下图:
Then, right click the properties of the dll, change the Common language Run time Support to Common language Run time Support(/clr). Like the following picture:
第三,请创建一个winforms应用程序并编写以下代码.
Third, please create a winforms app and write the following code.
**public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("TestDLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void HelloWorld();
private void button1_Click(object sender, EventArgs e)
{
HelloWorld();
}
}**
下一步,您需要手动添加对应的dll.
Next, you need to add the correspond dll manually.
最后,您可以获得结果.
Finally, you can get the result.
这篇关于在C#Visual Studio应用程序中调用C ++ __stdcall函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!