C++ / C# Interop

Here is code for basic dllExport in C++  and dllImport in C#
C++
#include <windows.h>
#define EXPORT __declspec(dllexport)
extern "C"
{
EXPORT int dllStartClient()
{
return (WinMain((HINSTANCE)0x500000,(HINSTANCE)0x500000,"",0));
}
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
MessageBox(NULL,L"Hello",L"Hello",MB_ICONEXCLAMATION | MB_OK);
return 0;
}
--------------------------------------------------------------------------------------------
C#
using System.Runtime.InteropServices;
namespace Test1
{
public partial class Form1 : Form
{
[DllImport("Client.dll")]
private static extern int dllStartClient();

public Form1()
{
InitializeComponent();
}

private void BtnStartClient_Click(object sender, EventArgs e)
{
StartClient();
}
private void StartClient()
{
dllStartClient();
}
}
}


No comments: