Pergunta

I'm using WINAPI dll written in C++ using DllImport for accessing USB ADC/DAC converter values. The only problem is:

long ZGetBufferADC(long typeDevice, long numberDSP, void **buffer, long *size)

I've translated it into

[DllImport("Zadc.dll")]
public static extern Int32 ZGetBufferADC(Int32 typeDevice, Int32 numberDSP, out IntPtr    buffer, out Int32 size);

I call this function like that

Int32 err = ZGetBufferADC(typeDevice, numberDSP, out pBuffer, out sizeBufferADC);

Then I need to access resulting pBuffer in C# like that in C++:

short *pBuffer16ADC = (short*) pBuffer
volt0 = resolutionADC0 * (pBuffer16ADC[pointerADC]) / amplifyADC0;

where pointerADC is Int32 pointing to current value of ADC converter, i managed to get it w/o problems correctly

so how could i implement that structure in c#? I tried defining variables like that

IntPtr pBuffer;
Int16 pBuffer16ADC; 
pBuffer16ADC = (Int16)pBuffer;
volt0 = resolutionADC0 * (pBuffer16ADC[pointerADC]) / amplifyADC0;

but that throws an error

Cannot apply indexing with [] to an expression of type 'short'

Any help will be appreciated! If additional info is needed please ask i'll provide ASAP. This issue is driving me nuts for almost a week :( Thanks!

Foi útil?

Solução

Presumably size is measured in bytes. If instead, size is the number of elements, it should be obvious how to adapt the following.

First of all, call the function as you are doing:

Int32 err = ZGetBufferADC(typeDevice, numberDSP, out pBuffer, out sizeBufferADC);

Then declare an array into which you copy the buffer.

short[] buffer = new short[sizeBufferADC/Marshal.SizeOf(typeof(short))];

Finally copy the buffer:

Marshal.Copy(pBuffer, buffer, 0, buffer.Length);

And that should be all you need to do.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top