PC Review


Reply
Thread Tools Rate Thread

Call Native Dll Function

 
 
Charles Wang
Guest
Posts: n/a
 
      3rd Mar 2004
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!


 
Reply With Quote
 
 
 
 
Peter Foot [MVP]
Guest
Posts: n/a
 
      3rd Mar 2004
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

--
Peter Foot
Windows Embedded MVP
OpenNETCF.org Senior Advisor
www.inthehand.com | www.opennetcf.org

"Charles Wang" <(E-Mail Removed)> wrote in message
news:u#81$$(E-Mail Removed)...
> 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!
>
>



 
Reply With Quote
 
Dick Grier
Guest
Posts: n/a
 
      3rd Mar 2004
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.


 
Reply With Quote
 
Charles Wang
Guest
Posts: n/a
 
      6th Mar 2004
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.

"Dick Grier" <(E-Mail Removed)> 写入消息新闻
:(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can P/Invoke call a native method of a native class? blackbiscuit Microsoft C# .NET 9 4th Jun 2009 11:31 AM
Can P/Invoke call a native method of a native class? blackbiscuit Microsoft C# .NET 0 11th May 2009 02:04 PM
How to call this native function with a struct in a struct ? Peter Microsoft Dot NET Compact Framework 1 22nd Mar 2007 10:06 PM
DefinePInvokeMethod causing PInvokeStackImbalance when returning a value from the native function call Marek Microsoft C# .NET 2 9th Jan 2006 03:04 PM
Call to native function from C# =?Utf-8?B?RG9taW5payBIYW5lYmVyZw==?= Microsoft Dot NET Compact Framework 2 17th Feb 2004 02:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:06 PM.