C#怎样嵌入C++代码?
答案:3 悬赏:40
解决时间 2021-01-14 05:20
- 提问者网友:像風在裏
- 2021-01-13 18:15
C#怎样嵌入C++代码?
最佳答案
- 二级知识专家网友:时间的尘埃
- 2021-01-13 18:29
你的问题应该是怎么在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
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);
}
因为如果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
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);
}
全部回答
- 1楼网友:一秋
- 2021-01-13 20:22
dllimport
- 2楼网友:我住北渡口
- 2021-01-13 19:25
dllimport
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息