【发布时间】:2013-03-05 03:44:17
【问题描述】:
我正在尝试将用 C++ 制作的本机 DLL 导入到 C#。我有一个小问题。
这是我的 C# 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace test
{
class Program
{
[DllImport("hello2dll.dll")] //I didn't know what to name it :'(
private static extern void SayHi();
static void Main(string[] args)
{
while (true)
{
Console.ReadKey();
SayHi();
}
}
}
}
这是来自 DLL 的 main.h:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
/* To use this exported function of dll, include this header
* in your project.
*/
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
void DLL_EXPORT SayHi();
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__
然后这里是来自 DLL 的 main.cpp:
#include "main.h"
#include<windows.h>
void SayHi()
{
MessageBox(HWND_DESKTOP, "Hello!", "Hello!", 0);
}
所以我尝试通过将 DLL 放入 system32 来访问它,然后我尝试通过将其复制并粘贴到 Visual c# 中来将其添加到项目中,但到目前为止我还没有成功。我有点失望,它没有用,但谁知道呢。
【问题讨论】:
-
你得到什么错误?或者你只是没有消息框?
-
1. 32 位还是 64 位窗口?很多问题都是因为缺少适合平台的.dll造成的。
标签: c# c++ native native-code