blah blah blah is here! blah blah » Close

up0down
link

Hello

I need to write a function that will pass 2 parameters to a dll function. The dll function is below:

getADCSamplesFromAllEnabledChannels(int sampleToCapture, double **voltagePointerArray)

it takes a pointer to pointer as a parameter and i cannot think of the syntax to pass the pointer to pointer!

ANy suggestion is welcomed =) I can do this for a parameter that is just a pointer, sth like *voltagePointerArray, but not the **voltagePointerArray!

Thanks
Muhu

last answered 2 years ago

2 answers

up0down
link

The unmanaged function is expecting a double array to be passed by reference so I'd use from C#:

getADCSamplesFromAllEnabledChannels(int sampleToCapture, ref IntPtr voltagePointerArray)

You'll then need to copy your managed double array to an unmanaged buffer and pass the IntPtr to the buffer by reference to the unmanaged function.

Edit (to include sample code)

Suppose the unmanaged function is expecting the double array to be of length 'size'. The C# code to marshal the array to a buffer and call the unmanaged function would then be:

double[] voltages = new double[size];

/*
include code to populate this array unless happy for all elements to be zero
*/

// allocate unmanaged buffer big enough to contain array
IntPtr buffer = Marshal.AllocHGlobal(8 * size);

// copy managed array to this buffer
Marshal.Copy(voltages, 0, buffer, size);

// call unmanaged function
getADCSamplesFromAllEnabledChannels(sampleToCapture, ref buffer);

// if unmanaged code changes array and you want to access from C#
Marshal.Copy(buffer, voltages, 0, size );

// clean up buffer
Marshal.FreeHGlobal(buffer);

up0down
link

You'll then need to copy your managed double array to an unmanaged buffer and pass the IntPtr to the buffer by reference to the unmanaged function.

I am not sure what you meant by the above!
For a function like getSingleDACVoltage(byte dacNumber, double* voltage) in the dll, i use the following code:

unsafe void Get_Single_DAC_Voltage(TextBox tb_OutputNumber_DAC, TextBox tb_Voltage_DAC)
{

byte[] DAC_OutputNumber = new byte[1];
double[] DAC_Voltage = new double[1];

fixed (byte* DAC_OutputNumberPtr = DAC_OutputNumber)
fixed (double* DAC_VoltagePtr = DAC_Voltage)

{
bool got;

got = digitalIODll.getSingleDACVoltage((byte)(DAC_OutputNumberPtr), DAC_VoltagePtr);
if (got == true)
{
//tb_OutputNumber_DAC.Text = String.Format("{0,5:000}", DAC_OutputNumber[0]);
tb_Voltage_DAC.Text = String.Format("{0,5:000}", DAC_Voltage[0]);

}
else
{
MessageBox.Show("Could not get the DAC Voltage");

}
}

}
Thanks
Muhu

vulpes
17279

I've edited my answer to include some sample code.

Feedback