nice to meet you! this is my first post to stacoverflow. I am studying C++ and LabVIEW for my own education.
LabVIEW is graphical design programming environment and it can build DLL from LabVIEW code. "Creating a DLL from LabVIEW code" https://decibel.ni.com/content/docs/DOC-15556
I built a simple DLL and trying to call it from C++ code. we can sum A and B by the DLL. First, i made a source code as follows.
#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)
*****************************************************************************
it worked fine and the result of "myValue" was 34 (2+32). it was ok.
next, i changed dll configuration and C++ code as foolows.
#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" returned "0". it was not expexted result and it should return 5 (2+3).
I think my code has a problem, but i could not find out what cause this. Please comment if you have any advice.
Thank you!
Assuming the third argument to myFunc
denotes the sum:
The function signatures do not match. The myFunc
returns void, but your function pointer typedef says it returns an int. The behavior is undefined when you do this (I'm surprised the program didn't crash).
typedef void (*FUNC)(int32_t, int32_t, int32_t*);
Also, you should match exactly the types the exported DLL function expects. In the typedef, you say int
, but the function takes int32_t
.
Given the fixes above, the other issues:
1) The call should pass the address of myValue
2) The DLL function returns void, so it makes no sense to use it in cout
.
myFunc(2,3,&myValue);
cout << "myValue = " << myValue << endl;