Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...