001
28.08.2015, 07:47 Uhr
~gastDortmund
Gast
|
Hallo Zusammen,
ich habe einen Dummy Projekt (C++) "um DLL zu erstellen" erstellt und dafür einen C_Wrapper geschrieben:
Myclass.h
Code: |
#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
Code: |
#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) { //avg_num ermittelt den einfachen Durchschnitt eines Arrays numerischer Werte 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]); } //numIntegers bestimmt die Anzahl der Integer-Werte in einem String return numberOfNumbers;
}
|
C_Wrapper.h
Code: |
#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
Code: |
// DLL_Test_Labview.cpp : Defines the exported functions for the DLL application. //
#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); }
|
--> Um dieses DLL zu testen habe ich TestProgramm geschrieben und es funktinoiert einwandfrei. Ich habe auch das DLL in Labview importiert und es funktioniert.
Nun habe ich eine Frage m bezug auf meinem TestProgramm, der so aussieht:
Code: |
#include "stdafx.h" //#include <tchar.h> #include "C_DllWrapper.h" #include <iostream> using namespace std;
int _tmain(int argc, _TCHAR* argv[]) { Wrapper wrapper = {}; // Wie soll ich meinen struct initialisieren ? double a1 = 12, a2 = 17; createWrapper(a1,a2); //double ergebnis; //createWrapper(12,13); cout << Add(wrapper,a1,a2) <<"--------" << endl;; cout << Subst(wrapper,a1,a2)<< "+++++++" << endl; cout << Mult(wrapper,a1,a2)<< "########" << endl; //Add(wrapper,12,13); getchar(); return 0; }
|
Wie soll ich meinen struct initialisieren? Hier in der Dummy Projekt könnte mit 0 initialisiert werden, aber was wäre der richtige vorgehen? |