在c#visual studio应用程序中调用c ++ __stdcall函数指针

我有一个带有.h和.cpp文件的Visual c ++应用程序。我想在c#应用程序中调用函数指针。我已插入c ++应用程序的.h文件。请为此提供帮助

// 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);
songkings 回答:在c#visual studio应用程序中调用c ++ __stdcall函数指针

如果要在c#中调用c ++ dll,建议您尝试以下步骤。

首先,创建一个c ++ dll并编写以下代码。

#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?",MB_ICONINFORMATION);
    }
}

然后,右键单击dll的属性,将公共语言运行时支持更改为 公共语言运行时支持(/ clr) >。 如下图: enter image description here

第三,请创建一个winforms应用并编写以下代码。

**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。

最后,您可以获得结果。

enter image description here

本文链接:https://www.f2er.com/3126297.html

大家都在问