繁体   English   中英

在 c# Visual Studio 应用程序中调用 c++ __stdcall function 指针

[英]calling a c++ __stdcall function pointer in c# visual studio application

我有一个带有 .h 和 .cpp 文件的 Visual c++ 应用程序。 我想在 c# 应用程序中调用 function 指针。 我已经插入了 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);

如果你想在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?", L"Hi", MB_ICONINFORMATION);
    }
}

然后,右键单击 dll 的属性,将Common language Run time Support更改为Common language Run time Support(/clr) 如下图: 在此处输入图像描述

第三,请创建一个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。

最后,您可以得到结果。

在此处输入图像描述

[DllImport("Native.dll", CallingConvention CallingConvention.StdCall)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM