可以从.net调用的非常简单的C ++ DLL
时间:2020-03-05 18:46:19 来源:igfitidea点击:
我正在尝试从vb.net 2005调用第三方供应商的C DLL,并且遇到P / Invoke错误。我已经成功调用了其他方法,但是遇到了其中一种比较复杂的瓶颈。涉及的结构太可怕了,为了简化故障排除过程,我想创建一个C ++ DLL来复制问题。
有人可以为可以从.Net调用的C ++ DLL提供最小的代码段吗?我的C ++ dll中出现"无法在DLL中找到名为XXX的入口点"错误。解决起来应该很简单,但是我不是C ++程序员。
我想对.NET的DLL使用.net声明
Declare Function Multiply Lib "C:\MyDll\Debug\MyDLL.DLL" Alias "Multiply" (ByVal ParOne As Integer, ByVal byvalParTwo As Integer) As Integer
解决方案
回答
尝试在C ++函数声明中使用__decspec(dllexport)魔术像素尘。此声明设置了成功从DLL导出函数所需的几件事。我们可能还需要使用WINAPI或者类似的东西:
__declspec(dllexport) WINAPI int Multiply(int p1, int p2) { return p1 * p2; }
WINAPI设置了函数调用约定,以便适合从VB.NET等语言进行调用。
回答
我们可以尝试查看导出的函数(通过DumpBin或者Dependency Walker),并查看名称是否乱码。
回答
根据格雷格的建议,我发现了以下作品。如前所述,我不是C ++程序员,只是需要一些实用的东西。
myclass.cpp
#include" stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } int _stdcall multiply(int x , int y) { return x*y; }
myclass.def
图书馆myclass
EXPORTS multiply @1
stdafx.cpp
#include" stdafx.h"
stdafx.h
// stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently // #if !defined(AFX_STDAFX_H__5DB9057C_BAE6_48D8_8E38_464F6CB80026__INCLUDED_) #define AFX_STDAFX_H__5DB9057C_BAE6_48D8_8E38_464F6CB80026__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // Insert your headers here #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers #include <windows.h> //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_STDAFX_H__5DB9057C_BAE6_48D8_8E38_464F6CB80026__INCLUDED_)