2014-12-27 3 views
2

만나서 반가워요! 이것은 stacoverflow에 대한 첫 번째 게시물입니다. 저는 제 자신의 교육을 위해 C++과 LabVIEW를 공부하고 있습니다.C++로 LabVIEW에서 빌드 한 DLL을 호출하는 방법

LabVIEW는 그래픽 디자인 프로그래밍 환경이며 LabVIEW 코드에서 DLL을 빌드 할 수 있습니다. "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[]) 
 
{ 
 
\t HINSTANCE hDLL = LoadLibrary(TEXT("C:\\sum_dll.dll")); 
 
\t \t if(hDLL == NULL){ 
 
\t \t cout << "error" << endl; 
 
\t } 
 
\t 
 
\t FARPROC lpIO = GetProcAddress(HMODULE(hDLL),"Sum_dll"); 
 

 
\t typedef int (*FUNC)(int a, int b); 
 

 
\t FUNC myFunc; 
 
\t myFunc = FUNC(lpIO); 
 

 
\t int myValue = myFunc(2,32); 
 

 
\t cout << myValue << endl; 
 

 
\t FreeLibrary(hDLL); 
 
\t 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 (32 + 2)이었다. 괜찮 았어.

다음으로 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[]) 
 
{ 
 
\t HINSTANCE hDLL = LoadLibrary(TEXT("C:\\sum_dll.dll")); 
 
\t \t if(hDLL == NULL){ 
 
\t \t cout << "error" << endl; 
 
\t } 
 
\t 
 
\t FARPROC lpIO = GetProcAddress(HMODULE(hDLL),"Sum_dll"); 
 
\t 
 
\t FUNC myFunc; 
 
\t myFunc = FUNC(lpIO); 
 

 
\t 
 
\t cout << myFunc(2,3,0) << endl; 
 

 
\t //cout << "myValue = " << myValue << endl; 
 

 
\t FreeLibrary(hDLL); 
 
\t 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"을 반환. 결과가 expexted 아니므로 5 (2 + 3)을 반환해야합니다.

내 코드에 문제가 있다고 생각하지만이 원인을 찾을 수 없습니다. 조언이 있으면 의견을 말하십시오.

감사합니다. 함수 서명이 일치하지 않는

: myFunc 세 번째 인수를 가정

+1

로드 myValue' 당신이 가진 코드에서. 어떻게 0을 돌려 줄 수 있을까요? – PaulMcKenzie

+0

두 번째 버전의 'myFunc'에서 세 번째 인수는 무엇을 의미합니까? 그게 합계가되어야합니까? – PaulMcKenzie

답변

1

는 합계를 의미한다. myFunc은 void를 반환하지만 함수 포인터 typedef는 int를 반환한다고 말합니다. 이 작업을 수행 할 때 동작이 정의되지 않았습니다 (프로그램이 중단되지 않은 것이 놀랍습니다).

typedef void (*FUNC)(int32_t, int32_t, int32_t*);

또한, 당신은 내 보낸 DLL 함수가 기대 정확히 유형과 일치해야합니다. typedef에서는 int이라고하지만 함수는 int32_t이됩니다. 위의 수정, 다른 문제 감안할 때

: 그것은 cout에서 사용하는 말도 안돼 있도록

1) 호출은 DLL 함수가 무효 반환) myValue

2의 주소를 전달해야합니다.

myFunc(2,3,&myValue); 
cout << "myValue = " << myValue << endl; 
0

귀하의 의견에 감사드립니다.

다음과 같이 C++ 소스를 변경했습니다. 크래시가없고 올바른 값이 반환되었습니다.

나는 shoud가 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. 
 
\t HINSTANCE hDLL = LoadLibrary(TEXT("C:\\sum_dll.dll")); 
 
\t \t if(hDLL == NULL){ 
 
\t \t cout << "error" << endl; // error check 
 
\t } 
 
\t 
 
\t //Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL). 
 
\t FARPROC lpIO = GetProcAddress(HMODULE(hDLL),"Sum_dll"); 
 
\t 
 
\t //define type 
 
\t FUNC myFunc; 
 
\t myFunc = FUNC(lpIO); 
 

 
\t //define returned variable 
 
\t int32_t myValue; 
 

 
\t //call function 
 
\t myFunc(2,3,&myValue); 
 

 
\t cout << myValue << endl; 
 

 
\t FreeLibrary(hDLL); 
 
\t return 0; 
 
}

0

내가 프로젝트의 lib 디렉토리 파일을 포함 할 때까지 작동하지 않았다 속성 -> 링커 -> 입력 -> 당신은`주석 한 지연 DLL이