2015-09-14 106 views
0

我創建了一個虛擬項目(C++) 「創建DLL」 爲C_Wrapper和 寫着: Myclass.hLabview的DLL導入

#pragma once 
//#include <QString> 
class MyClass 
{ 
public: 
MyClass(double, double); 
~MyClass(); 
double Add(double,double); 
double Subst(double, double); 
double Mult(double, double); 
double Divid(double, double); 
bool Vergleich(double,double); 
long Avg_num(float *, long, float *); 
unsigned int NumInteger(char * inputString); 
//QString GibEinString(double, double); 
private: 
double m_a, m_b; 
}; 

MyClass.cpp

#include "stdafx.h" 
#include "MyClass.h" 


MyClass::MyClass(double a,double b) 
{ 
this->m_a = a; 
this->m_b = b; 
} 

MyClass::~MyClass(){ } 

double MyClass::Add(double a, double b) 
{ 
    return a+=b; 
} 
double MyClass::Mult(double a, double b) 
{ 
    return a*=b; 
} 
double MyClass::Subst(double a, double b) 
{ 
    return b-=a; 
} 
double MyClass::Divid(double a, double b) 
{ 
    return (b/a); 
} 
bool MyClass::Vergleich(double a,double b) 
{ 
    if(a>=b) return true; 
    else return false; 
} 

long MyClass::Avg_num(float *a, long size, float *avg) 
{ 
float sum = 0; 
if(a != NULL) 
{ 
    for(int i=0;i < size; i++) 
    sum = sum + a[i]; 
} 
    else 
    return (1); 
    *avg = sum/size; 
    return (0); 
    } 
    unsigned int MyClass::NumInteger(char * inputString) 
    { 
    int lastDigit = 0; 
     int numberOfNumbers = 0; 
     int stringSize; 

     stringSize = strlen(inputString); 
    for(int i = 0; i < stringSize; i++) 
     { 
     if (!lastDigit && isdigit(inputString[i])) 
     numberOfNumbers++; 
     lastDigit = isdigit(inputString[i]); 
     } 
     return numberOfNumbers; 

    } 

C_Wrapper .H

#pragma once 
#define DLLIMPORT __declspec (dllexport) 
#ifdef __cplusplus 
extern "C" { 
#endif 

typedef struct Wrapper 
{ 
    void *MYClass; 
}Wrapper; 

DLLIMPORT Wrapper createWrapper(double a, double b); 
DLLIMPORT void destoryWrapper(Wrapper LV_ref); 

DLLIMPORT double Add(Wrapper LV_ref, double a, double b); 
DLLIMPORT double Subst(Wrapper LV_ref ,double a, double b); 
DLLIMPORT double Mult(Wrapper LV_ref, double a, double b); 
DLLIMPORT double Divid(Wrapper LV_ref, double a, double b); 
DLLIMPORT bool Vergleich(Wrapper LV_ref, double a, double b); 
DLLIMPORT long Avg_num(Wrapper LV_ref,float *a, long size, float * avg); 
DLLIMPORT unsigned int NumInteger(Wrapper LV_ref, char * inputString); 


#ifdef __cplusplus 
} 
#endif 

C_Wrapper.cpp

#include "stdafx.h" 
#include "MyClass.h" 
#include "C_DllWrapper.h" 


DLLIMPORT Wrapper createWrapper(double a, double b) 
{ 
    Wrapper wrapper = {static_cast<void*>(new MyClass(a,b))}; 
    return wrapper; 
} 

DLLIMPORT void destoryWrapper(Wrapper LV_ref) 
{ 
    MyClass *myClass = static_cast<MyClass*>(LV_ref.MYClass); 
    delete myClass; 
    } 
    DLLIMPORT double Add(Wrapper LV_ref, double a, double b) 
    { 
    MyClass *myClass = static_cast<MyClass*>(LV_ref.MYClass); 
    return myClass->Add(a, b); 
    } 

    DLLIMPORT double Mult(Wrapper LV_ref, double a, double b) 
    { 
    MyClass *myClass = static_cast<MyClass*>(LV_ref.MYClass); 
    return myClass->Mult(a, b); 
    } 

    DLLIMPORT double Subst(Wrapper LV_ref, double a, double b) 
    { 
     MyClass *myClass = static_cast<MyClass*>(LV_ref.MYClass); 
     return myClass->Subst(a, b); 
    } 

    DLLIMPORT double Divid(Wrapper LV_ref, double a, double b) 
    { 
     MyClass *myClass = static_cast<MyClass*>(LV_ref.MYClass); 
     return myClass->Divid(a, b); 
     } 
     DLLIMPORT bool Vergleich(Wrapper LV_ref, double a, double b) 
     { 
     MyClass *myClass = static_cast<MyClass*>(LV_ref.MYClass); 
     return myClass->Vergleich(a,b); 
     } 
     DLLIMPORT long Avg_num(Wrapper LV_ref,float *a, long size, float * avg) 
     { 
     MyClass *myClass = static_cast<MyClass*>(LV_ref.MYClass); 
      return myClass->Avg_num(a,size,avg); 

      } 
     DLLIMPORT unsigned int NumInteger(Wrapper LV_ref, char * inputString) 
      { 
      MyClass *myClass = static_cast<MyClass*>(LV_ref.MYClass); 
      return myClass->NumInteger(inputString); 
      } 

當我在Labview中導入DLL時,幾乎做了所有事情。 我的問題是指功能主要是:

DLLIMPORT Wrapper createWrapper(double a, double b) 
{ 
Wrapper wrapper = {static_cast<void*>(new MyClass(a,b)) }; 
return wrapper; 
} 

我怎麼能在Labview中初始化?

謝謝

回答

1

你不能真正做到與包含指針結構,而不是直接使用鼠標指針在包裝功能,並在LabVIEW

使用PTR型

它成爲

typedef void* Wrapper; 

DLLIMPORT double Mult(Wrapper LV_ref, double a, double b) 
{ 
    MyClass *myClass = reinterpret_cast<MyClass*>(LV_ref); 
    return myClass->Mult(a, b); 
} 

DLLIMPORT Wrapper createWrapper(double a, double b) 
{ 
    Wrapper wrapper = reinterpret_cast<void*>(new MyClass(a,b)); 
    return wrapper; 
} 
+1

這實際上就是名稱InstanceDataPtr在extcode.h中使用的確切typedef,例如用於回調函數中的任意數據類型。就我個人而言,我主張使用intptr_t/uintptr_t類型,這是在CLFN嚮導在數字下選擇相應類型時生成的。還要注意,使用這些類型的原因是,代碼可以跨LabVIEW位(32/64位)移植。 – oysstu

0

頭文件

#pragma once 
#ifdef __cplusplus 
#endif 

extern "C"__declspec(dllexport) void* createWrapper(double a, double b); 
extern "C"__declspec(dllexport) void destoryWrapper(void *instance); 
extern "C"__declspec(dllexport) double Add(void *instance, double a, double b); 

......

#ifdef __cplusplus 
#endif 

源文件:

#include "stdafx.h" 
#include "MyClass.h" 
#include "C_DllWrapper.h" 


extern "C"__declspec(dllexport) void* createWrapper(double a, double b) 
{ 
    return new MyClass(a, b); 
} 

extern "C"__declspec(dllexport) void destoryWrapper(void *instance) 
{ 
    MyClass *myClass = static_cast<MyClass*>(instance); 
    delete myClass; 
} 
extern "C"__declspec(dllexport) double Add(void *instance, double a, double b) 
{ 
    MyClass *myClass = static_cast<MyClass*>(instance); 
    return myClass->Add(a, b); 
} 

.... 所以是好的!