4
Answers

[SOLVED] C++ void* to C# throught structure!

Ask a question
Eduardo

Eduardo

13y
3.1k
1
Hi,
I need to call a C++ DLL, but the out parameter is inside a structure and it is void*
The parameter in question is:  void* dst
After calling the function in the sample bellow, it converts the void* to (INT16).
How can i implement this in C# ?
This is de struct:
-------------------------------------------------------------------------------------------------------

WDFAccessParam64
Function:
Acquired Waveform Data Parameter Type
Format: typedef struct{
UINT version;
WDFTrace trace;
WDFHistoryBlock block;
WDFDataBlock start;
WDFDataBlock count;
INT64 ppRate;
INT waveType;
INT dataType;
WDFDataBlock cntOut;
void* dst;
INT box;
INT compMode;
INT rsv1;
INT rsv2;
INT rsv3;
INT rsv4;
}WDFAccessParam64;
  This is the Function i need to call:
-------------------------------------------------------------------------------------------------------
WdfGetScaleWave64
Function: Get waveform raw data.
Format: WDFResult WdfGetScaleWave64(WDFHandle handle, WDFAccessParam64* param);

Argument:
[IN] handle API File Handle
[IN] param Acquiring Parameter
[IN] version Constant Value(WDF_DEFAULT_ACSPRM64_VERSION)
[IN] trace Waveform Trace Number
[IN] block History Block Number
[IN] start Data Block Start Position
[IN] count Required Data Points
[IN] ppRate Constant Value(WDF_DEFAULT_ACSPRM64_PPRATE)
[IN] waveType Constant Value(WDF_DEFAULT_ACSPRM64_WAVETYPE)
[IN] dataType Constant Value(WDF_DEFAULT_ACSPRM64_DATATYPE)
[OUT] cntOut Acquired Data Points
[IN] dst Data Output Buffer
[OUT] box Not in Use
[IN] compMode Constant Value(WDF_DEFAULT_ACSPRM64_COMPMODE)


-------------------------------------------------------------------------------------------------------
This is the sample (it uses malloc before calling the procedure, and converts do INT16 after):

// Get waveform raw-data.
WDFAccessParam64 param;
param.version = WDF_DEFAULT_ACSPRM64_VERSION; // fixed
param.trace = TargetTrace; // given
param.block = TargetBlcok; // given
param.start = StartCount; // given
param.count = ReadCount; // given
param.ppRate = WDF_DEFAULT_ACSPRM64_PPRATE; // fixed
param.waveType = WDF_DEFAULT_ACSPRM64_WAVETYPE; // fixed
param.dataType = WDF_DEFAULT_ACSPRM64_DATATYPE; // fixed
param.compMode = WDF_DEFAULT_ACSPRM64_COMPMODE; // fixed
param.dst = malloc((size_t)param.count * 4); // MaxDataSize:4byte
if(!param.dst){
return; // Error, Not Enough Memory
}
WDFResult result = WdfGetScaleWave64(wdfHandle, &param);
if(result != 0){
free(param.dst);
return; // Error
}
// Get Horizontal waveform Infomations.
WDFDataParameter hOffset;
WdfGetHOffset(wdfHandle, param.trace, param.block, &hOffset);
WDFDataParameter hResolution;
WdfGetHResolution(wdfHandle, param.trace, param.block, &hResolution);
// Get Vertical waveform Infomations.
WDFDataParameter vResolution;
WdfGetVResolution(wdfHandle, param.trace, param.block, &vResolution);
WDFDataParameter vOffset;
WdfGetVOffset(wdfHandle, param.trace, param.block, &vOffset);
WDFDataParameter vIllegalData;
WdfGetVIllegalData(wdfHandle, param.trace, param.block, &vIllegalData);
// Convert raw data and save text.
FILE fp = fopen(" C:\\Output.csv", " w");
WDFVDataType vDataType;
WdfGetVDataType(wdfHandle, param.trace, param.block, &vDataType);
switch(vDataType){
case wdfDataTypeSINT16:
for(int i=0;i<param.cntOut/* read count */;i++){
double outDataX = (param.start + i) * hResolution + hOffset;
INT16 rawData = ((INT16*)param.dst)[i];
if(rawData == vIllegalData){
fprintf(fp, " %f, nan\r\n", outDataX); // Hidden Data
}
else{
double outDataY = rawData * vResolution + vOffset;
fprintf(fp, " %f, %f\r\n", outDataX, outDataY);
}
}
break;
case wdfDataTypeLOGIC16:
for(int i=0;i<param.cntOut/* read count */;i++){
UINT16 outDataY = ((UINT16*)param.dst)[i];
double outDataX = (param.start + i) * hResolution + hOffset;
fprintf(fp, " %f, %f\r\n", outDataX, outDataY);
}
break;
case wdfDataTypeUINT16:
for(int i=0;i<param.cntOut/* read count */;i++){
double outDataX = (param.start + i) * hResolution + hOffset;
UINT16 rawData = ((UINT16*)param.dst)[i];
if(rawData == vIllegalData){
fprintf(fp, " %f, nan\r\n", outDataX); // Hidden Data
}
else{
double outDataY = rawData * vResolution + vOffset;
fprintf(fp, " %f, %f\r\n", outDataX, outDataY);
}
}
break;
default:
break; // Unknown data type
}
fclose(fp);
free(param.dst);


Answers (4)