Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!
", 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: enter image description here

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.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...