Call Native Dll Function

  • Thread starter Thread starter Charles Wang
  • Start date Start date
C

Charles Wang

Hello Everyone!

I want to get data from a A/D card.

All functions that control this A/D card are included in ADCard.dll, native
code;

I import functions using DllImport. Other functions, such as Initial() and
StarIntr() etc, work fine except ADResult();

ADResult() is defined as int __stdcall ADResult(ADCard_Result * Buf).

ADCard_Result is a struct:
typedef unsigned short ADValueType;
typedef struct structADResult {
ADValueType SeqNo;
ADValueType ADValue[1];
} ADCard_Result;

I import this function like this:
public struct ADCard_Result
{
ushort SeqNo;
ushort ADValue;
}
[DllImport("ADCARD.DLL", EntryPoint="ADResult", SetLastError=true)]
private extern static int ADResult(ADCard_Result[] Buf);

In main progress I call this function like this:
ADCard_Result[] pResult = new ADCard_Result[10000];
pResult.Initialize();
int r = ADCard(pResult);

But all the pResult[0-10000] is 0. I alse use out and ref but they take no
effect.

Any suggestion is welcome! Thanks!
 
You might have more success passing a byte array as the buffer into the
function and then extract the result out into your array of custom classes
e.g.

[DllImport("ADCARD.DLL", EntryPoint="ADResult", SetLastError=true)]
private extern static int ADResult(byte[] Buf);

byte[] buffer = new byte[4000];

int result = ADResult(buffer);

If the function is failing (returning no data into the buffer) what is the
return value from the function - place a breakpoint after calling the
function and look at the value of result (or r in your code).

Peter
 
Hi,

I assume that your DLL is compiled for the target processor? Naturally, you
cannot use an x86 DLL, unless the target is x86.

What hardware are you using? I'd like to look at it.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
Thanks Peter and Dick!

My Dll is actually compiled of x86 processor. Program created by EVC could
get the data from my hardware, a A/D sample card with USB interface. My
platform is PCM-3350 with WinCE4.2.

I think that maybe because of marshalling struct after reading two P/Invoke
marshalling artiles on MSDN.

The sampled result is sotred in a buffer in EVC after execute below code:
ADBuf = (WORD*) malloc( (NumChn*NumSamp+1)*sizeof(WORD) );
ADResult((struct ADCard_Result *)ADBuf);

The result buffer becomes {SeqNo, ADValue1, ADValue2, ADValue3...ADValueN}.

So how can I marshal such stuct? Thanks.
 
Back
Top