简体   繁体   中英

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

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);

If you want to call c++dll in c#, I suggest that you could try the following steps.

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);
    }
}

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: 在此处输入图像描述

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();
    }
}**

Next, you need to add the correspond dll manually.

Finally, you can get the result.

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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