我正在尝试将C++制作的本地动态链接库导入到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);
}因此,我尝试通过将其放入system32中来访问该动态链接库,然后尝试通过将其复制并粘贴到visual c#中来将其添加到项目中,但到目前为止我还没有成功。我有点失望,它没有工作,但谁知道呢。
发布于 2013-03-05 11:48:09
我不知道它为什么会失败,我宁愿创建一个与CLR兼容的程序集
您可以使用tblimp创建与CLR兼容的程序集,然后可以添加对该程序集的引用,而不是导入它。
https://stackoverflow.com/questions/15215448
复制相似问题