【发布时间】:2012-09-30 08:59:44
【问题描述】:
我正在尝试使用 DLLImport 在 C# 中使用 Win32 dll 方法。
Win32 dll C++ // .h 文件
#ifdef IMPORTDLL_EXPORTS
#define IMPORTDLL_API __declspec(dllexport)
#else
#define IMPORTDLL_API __declspec(dllimport)
#endif
// This class is exported from the ImportDLL.dll
class IMPORTDLL_API CImportDLL {
public:
CImportDLL(void);
// TODO: add your methods here.
int Add(int a , int b);
};
extern IMPORTDLL_API int nImportDLL;
IMPORTDLL_API int fnImportDLL(void);
IMPORTDLL_API int fnMultiply(int a,int b);
// .cpp 文件
// ImportDLL.cpp : 定义 DLL 应用程序的导出函数。 //
#include "stdafx.h"
#include "ImportDLL.h"
// This is an example of an exported variable
IMPORTDLL_API int nImportDLL=0;
// This is an example of an exported function.
IMPORTDLL_API int fnImportDLL(void)
{
return 42;
}
IMPORTDLL_API int fnMultiply(int a , int b)
{
return (a*b);
}
一旦我构建了这个,我就会得到 ImportDLL.dll
现在我创建 Windows 应用程序并将此 dll 添加到调试文件夹中并尝试使用 DLLImport 使用此方法
[DllImport("ImportDLL.dll")]
public static extern int fnMultiply(int a, int b);
我尝试在 C# 中调用它
int a = fnMultiply(5, 6); // 这行给出错误无法找到入口点
任何人都可以告诉我我错过了什么吗? 谢谢。
【问题讨论】:
-
@HansPassant 这不是实例方法。它是 DLL 中的常规公共函数。检查标题声明。事实上,这是 VS 吐出的“用导出的符号为我创建一个 DLL”项目的股票。他不是在尝试调用实例方法。他正在尝试连接一个导出的函数。
-
你是对的,被班级绊倒了。