2014-12-28 126 views
-1

我正在做關於LabVIEW和DLL的新研究。 我創建了一個由LabVIEW構建的DLL。訪問DLL中的LStrHandle

這是一個頭文件。

#include "extcode.h" 
#pragma pack(push) 
#pragma pack(1) 

#ifdef __cplusplus 
extern "C" { 
#endif 
typedef struct { 
    int32_t numeric_input; 
    LStrHandle string_input; 
} Cluster; 

/*! 
* Cluster_dll 
*/ 
void __cdecl Cluster_dll(Cluster *Input, int32_t *numeric_output, 
    char string_output[], int32_t length); 

MgErr __cdecl LVDLLStatus(char *errStr, int errStrLen, void *module); 

#ifdef __cplusplus 
} // extern "C" 
#endif 

#pragma pack(pop) 

要調用它,我創建了Visual C++程序。

#include "stdafx.h" 
#include <Windows.h> 
#include <iostream> 
#include <cstdint> 
#include "C:\Program Files\National Instruments\LabVIEW 2014\cintools\extcode.h" 
using namespace std; 

struct Input_t{ 
    int32_t A; 
    LStrHandle B; 
} variable_t; 

typedef void (*FUNC)(Input_t,int32_t *,char [],int32_t); 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    HINSTANCE hDLL = LoadLibrary(TEXT("C:\\Cluster_dll.dll")); 
     if(hDLL == NULL){ 
     cout << "error" << endl; // error check 
    } 


    FARPROC lpIO = GetProcAddress(HMODULE(hDLL),"Cluster_dll"); 

    //define type 
    FUNC myFunc; 
    myFunc = FUNC(lpIO); 

    //define returned variable 

    Input_t Cluster; 
    Cluster.A = 1; 
    Cluster.B; 

    int32_t myValue = 0; 
    char str[10]; 

    //call function 
    myFunc(Cluster,&myValue,str,10); 


    FreeLibrary(hDLL); 
    return 0; 
} 

我建立它並跑了。那麼應用程序崩潰。 我想我不能正確對待「LStrHandle」。 它是由LabVIEW定義的原始數據類型。

typedef struct { 
    int32 cnt; /* number of bytes that follow */ 
    uChar str[1]; /* cnt bytes */ 
} LStr, *LStrPtr, **LStrHandle; 

我查找了NI網頁和一些網絡資源,但無法解決崩潰問題。

如果您有任何建議,請發表評論。

+0

。你也不應該傳遞一個不確定的指針。至少它是空的。 –

回答

1
  1. 如果不使用所生成的報頭,至少插入#pragma pack(1)
  2. 你的生成函數接受指針到羣集,但是重新定義它作爲一個接受簇本身。這是個錯誤。可能的更正:

    typedef void (*FUNC)(Input_t*,int32_t *,char [],int32_t); 
    
  3. 所有內部LabVIEW數據句柄都分配在具有LabVIEW內存管理器函數的堆上。即您不能在堆棧上分配Input_t Cluster並將其傳遞給LabVIEW生成的函數。

你必須使用類似的東西:

Input_t *Cluster(DSNewPtr(sizeof(Input_t)); 
Cluster->A = 1; 
Cluster->B = DSNewHandle(sizeof(LStr)); 

你也應該設定您的邏輯來檢查你爲什麼不使用生成的頭內存分配錯誤和正確解除分配