见到你很高兴!这是我第一篇堆满的帖子。为了我自己的教育,我正在学习C++和LabVIEW。
LabVIEW是一个图形化的设计编程环境,它可以从LabVIEW代码中构建动态链接库。“从LabVIEW代码创建DLL”https://decibel.ni.com/content/docs/DOC-15556
我构建了一个简单的DLL,并试图从C++代码中调用它。我们可以用DLL把A和B相加。首先,我编写了如下源代码。
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE hDLL = LoadLibrary(TEXT("C:\\sum_dll.dll"));
if(hDLL == NULL){
cout << "error" << endl;
}
FARPROC lpIO = GetProcAddress(HMODULE(hDLL),"Sum_dll");
typedef int (*FUNC)(int a, int b);
FUNC myFunc;
myFunc = FUNC(lpIO);
int myValue = myFunc(2,32);
cout << myValue << endl;
FreeLibrary(hDLL);
return 0;
}
**************************** DLL header file ****************************
#include "extcode.h"
#pragma pack(push)
#pragma pack(1)
#ifdef __cplusplus
extern "C" {
#endif
/*!
* Sum_dll
*/
int32_t __cdecl Sum_dll(int32_t A, int32_t B);
MgErr __cdecl LVDLLStatus(char *errStr, int errStrLen, void *module);
#ifdef __cplusplus
} // extern "C"
#endif
#pragma pack(pop)
*****************************************************************************
它工作良好,"myValue“的结果为34(2+32)。一切都好。
接下来,我将dll配置和C++代码更改为“傻瓜”。
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std;
typedef int (*FUNC)(int, int,int *);
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE hDLL = LoadLibrary(TEXT("C:\\sum_dll.dll"));
if(hDLL == NULL){
cout << "error" << endl;
}
FARPROC lpIO = GetProcAddress(HMODULE(hDLL),"Sum_dll");
FUNC myFunc;
myFunc = FUNC(lpIO);
cout << myFunc(2,3,0) << endl;
//cout << "myValue = " << myValue << endl;
FreeLibrary(hDLL);
return 0;
}
**************************** DLL header file ****************************
#include "extcode.h"
#pragma pack(push)
#pragma pack(1)
#ifdef __cplusplus
extern "C" {
#endif
/*!
* Sum_dll
*/
void __cdecl Sum_dll(int32_t A, int32_t B, int32_t *C);
MgErr __cdecl LVDLLStatus(char *errStr, int errStrLen, void *module);
#ifdef __cplusplus
} // extern "C"
#endif
#pragma pack(pop)
*****************************************************************************
"myValue“返回"0”。它不是删除的结果,它应该返回5 (2+3)。
我认为我的代码有问题,但我找不出是什么原因造成的。如果你有任何建议,请评论。
谢谢!
发布于 2014-12-27 07:33:31
假设myFunc
的第三个参数表示和:
函数签名不匹配。myFunc
返回void,但是函数指针typedef表示它返回一个int。当您这样做时,行为是没有定义的(我很惊讶程序没有崩溃)。
typedef void (*FUNC)(int32_t, int32_t, int32_t*);
此外,您应该与导出的DLL函数所期望的类型完全匹配。在typedef中,您可以说int
,但是函数接受int32_t
。
鉴于上述解决办法,其他问题如下:
1)呼叫应传递myValue
地址
2) DLL函数返回void,因此在cout
中使用它是没有意义的。
myFunc(2,3,&myValue);
cout << "myValue = " << myValue << endl;
发布于 2014-12-27 08:39:48
谢谢你的评论。
我更改了我的C++源代码如下。没有崩溃,并返回正确的值。
我相信我应该学习C++指针和变量类型。非常感谢!
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <cstdint> //shoud be called to use int32_t
using namespace std;
//define the type of function
typedef void (*FUNC)(int32_t, int32_t,int32_t *);
int _tmain(int argc, _TCHAR* argv[])
{// Loads the specified module into the address space of the calling process.
HINSTANCE hDLL = LoadLibrary(TEXT("C:\\sum_dll.dll"));
if(hDLL == NULL){
cout << "error" << endl; // error check
}
//Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).
FARPROC lpIO = GetProcAddress(HMODULE(hDLL),"Sum_dll");
//define type
FUNC myFunc;
myFunc = FUNC(lpIO);
//define returned variable
int32_t myValue;
//call function
myFunc(2,3,&myValue);
cout << myValue << endl;
FreeLibrary(hDLL);
return 0;
}
发布于 2015-05-21 13:21:59
直到我将.lib文件包含在项目属性->linker->Input->延迟加载的Dlls中,它才起作用。
https://stackoverflow.com/questions/27665174
复制相似问题