Re: Marshaling Value Types

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Mike Hildner (mhildner_at_afweb.com)
Date: 02/23/04


Date: Mon, 23 Feb 2004 08:37:32 -0700

That's good news. Here's the relevent evc++ v3 code:

 BioAPI_VERSION Version;

 if (BioAPI_Init(&Version, 0, NULL, 0, NULL) != BioAPI_OK)
 {
  m_Message.SetWindowText(_T("BioAPI_Init failed."));
 }
 else
 {
  WCHAR printbuf[255];
  swprintf(printbuf, L"Version %lu %lu", Version.Major, Version.Minor);
  m_Message.SetWindowText(printbuf);
}

BioAPI_OK is defined as 0x0000000 and BioAPI_VERSION is
typedef struct bioapi_version {
 UINT32 Major;
 UINT32 Minor;
} BioAPI_VERSION, *BioAPI_VERSION_PTR;

and the prototype for BioAPI_INIT is
BioAPI_RETURN BioAPI BioAPI_Init (
 const BioAPI_VERSION *Version,
 UINT32 Reserved1,
 const void *Reserved2,
 UINT32 Reserved3,
 const void *Reserved4);

My attempt at C# (code is from various parts of the class):
public struct BVersion

{

public UInt32 major;

public UInt32 minor;

}

[DllImport("BioAPI.dll")]

unsafe public static extern int BioAPI_Init(BVersion *version, UInt32 r1,
void *r2, UInt32 r3, void *r4);

unsafe

{

BVersion version;

int rv;

rv = BioAPI_Init(&version, 0, null, 0, null);

MessageBox.Show(rv.ToString());

MessageBox.Show(version.major.ToString());

MessageBox.Show(version.minor.ToString());

}

At the risk of being long winded, here's some relevent information. I'm no
expert at c++ or c#. The C# code give me a return value of zero for the
BioAPI_Init call, and both version.major and version.minor are zero. The C++
code returns zero as well, but consitently, the first time I call it, major
is 2349634176 and minor is zero. The second and subsequent times, major is 3
and minor is 21188696. Lastly, the dll vendor says they need to port their
code to windows mobile 2003, it only works on 2002, yet I'm running this on
2003.

Thanks,

Mike

"Chris Tacke, eMVP" <ctacke[at]Open_NET_CF[dot]org> wrote in message
news:Or%23HtIb%23DHA.4088@tk2msftngp13.phx.gbl...
> It's got to be a code problem - uint's marshal just fine. Post your code
> for both ends.
>
> -Chris
>
> "Mike Hildner" <mhildner@afweb.com> wrote in message
> news:#H7ljNa#DHA.3488@tk2msftngp13.phx.gbl...
> > I've read that, with the compact framework, MarshalAs is not available
and
> > the only automaticly marshaled types when using P/Invoke is:
> >
> > C# C++
> > === ===
> > int int
> > short short
> > bool BYTE
> > char WCHAR
> >
> > I have a UINT32 in C++ and UInt32 in C#. Testing a function call (a .dll
> > supplied by the vendor) in both C++ and C# gives different results when
> UINT
> > is used.
> >
> > Might be bad code on my part, or might have something to do with the
> above.
> >
> > This particular book (Microsoft .NET Compact Framework, Microsoft Press,
> by
> > Wigley and Wheelwright) says that to marshal other value types it must
be
> > "performed explicitly in managed code."
> >
> > Now my questions:
> > - I'm wondering if I understand this correctly - would it be technically
> > correct to ask the .dll vendor if they plan to ship a "C#.NET Compact
> > Framework compatible version"?
> >
> > - If they don't and I still want to wrap the .dll, I'm thinking of
> creating
> > another .dll that does the marshaling, and acts as an intermediary
between
> > their .dll and C# - does this idea have merit?
> >
> > TIA,
> > Mike
> >
> >
>
>