Re: Marshaling Value Types

From: Chris Tacke, eMVP (ctacke_at_spamfree-opennetcf.org)
Date: 02/23/04


Date: Mon, 23 Feb 2004 11:54:16 -0500

First, the void* params are concerning if they are pointers to actual data,
as stuff might get moved around. But assuming you do all the pinning and
stuff required, you should be able to use this:

[DllImport("BioAPI.dll")]
public static extern int BioAPI_Init(BVersion version, uint r1 IntPtr r2,
uint r3, IntPtr r4);
...
rv = BioAPI_Init(version, 0, IntPtr.Zero, 0, IntPtr.Zero);

-- 
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net
"Mike Hildner" <mhildner@afweb.com> wrote in message
news:OH5TvLi%23DHA.1452@TK2MSFTNGP09.phx.gbl...
> 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
> > >
> > >
> >
> >
>
>


Relevant Pages

  • Re: Marshaling Value Types
    ... UINT32 Major; ... UINT32 Minor; ... and both version.major and version.minor are zero. ... >> I've read that, with the compact framework, MarshalAs is not available ...
    (microsoft.public.pocketpc.developer)
  • Re: Marshalling LPSTR [SOLVED]
    ... Another option you have is the MarshalAs attribute and SizeConst.You can do ... > .method public hidebysignewslot abstract virtual instance void Item( ... >> I believe you chould just change the datatype of the pszName parameter ... >>> Some time ago I read something about using a StringBuilder... ...
    (microsoft.public.dotnet.framework.clr)
  • Re: void vs void* (philosophical question)
    ... ordinary object type whose size is zero, ... its value, upon conversion to any other scalar type, would be ... void retval; ... I think your not-quite-C language is consistent, ...
    (comp.lang.c)
  • Re: Obvious bug in /sys/i386/include/bus.h (was: bus_at386.h)
    ... >> Adding that extra check for zero transfer length is not going to affect ... static __inline void ...
    (freebsd-hackers)
  • Re: ACBL Bulletin "Bidding Box" scoring?
    ... Are you going to give them zero for not playing SA? ... What do you bid? ... Did you mean to give us an example hand with a spade void instead of a ...
    (rec.games.bridge)