C#嵌入C++

你的问题应该是怎么在C#里调用C++代码写成的后编译好的dll,对吗?

因为如果C#里直接嵌入C++是不会通过编译的。

下面是一个C#里调用C++代码写成的后编译好的dll 的例子:

C# 部分:

// Sample program to call unmanaged code

using System;

using System.Runtime.InteropServices;

 

class PInvoke1App

{

[DllImport("user32.dll")]

static extern int MessageBoxA(int hWnd, string strMsg, string strCaption, int iType);

 

public static void Main()

{

MessageBoxA(0, "Hello, World!", "This is called from a C# app!", 0);

}

}

C++部分,编译成user.dll

#include <windows.h>

 

BOOL __stdcall DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved) {

return TRUE;

}

 

__declspec(dllexport) void __stdcall Message(char* p_szMessage) {

MessageBox(NULL, p_szMessage, "Message from DLL", MB_OK);

}

 

posted @ 2018-01-22 09:36  奔跑的蒲公英  阅读(757)  评论(0编辑  收藏  举报