我创建了一个虚拟项目(C ++)"来创建DLL"和 对于编写的C_Wrapper: Myclass.h
#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中初始化?
谢谢
答案 0 :(得分: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 :(得分: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);
}
.... 好的!!好的!!