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

Re: Marshaling Value Types by Chris

Chris
Sun Feb 22 20:10:09 CST 2004

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
>
>



Re: Marshaling Value Types by Mike

Mike
Mon Feb 23 09:37:32 CST 2004

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
> >
> >
>
>



Re: Marshaling Value Types by Chris

Chris
Mon Feb 23 10:54:16 CST 2004

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
> > >
> > >
> >
> >
>
>



Re: Marshaling Value Types by Mike

Mike
Mon Feb 23 10:59:51 CST 2004

Thanks for the info, I will give it a try. From a recent thread I've learned
about pinning. In general, can you explain/point me to a reference what you
mean by "all the pinning and stuff required"?

Thanks,
Mike



Re: Marshaling Value Types by Chris

Chris
Mon Feb 23 11:31:55 CST 2004

Meaning that if the void* params actually take pointers to data, you need to
"pin" the memory to which they point, otherwise the GC may move the data,
making your pointer invalid.

--
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:ep7Rv5i%23DHA.2824@tk2msftngp13.phx.gbl...
> Thanks for the info, I will give it a try. From a recent thread I've
learned
> about pinning. In general, can you explain/point me to a reference what
you
> mean by "all the pinning and stuff required"?
>
> Thanks,
> Mike
>
>