views:

67

answers:

2

Hi,

I've got an unmanaged c++ DLL that I need to call from a Windows Mobile C# app.

I've got the C# wrapper and it works nicely in desktop. I can call the DLL functions from a C# desktop program and pass strings around with no problem.

However, when I compile the lib and the wrapper for the mobile platform, I get an error in the DllImport lines saying that the CharSet.ANSI is not recognized. The only options I'm allowed to write are CharSet.Auto and CharSet.Unicode.

The problem is that, regardless of this setting, the strings that are received in the c++ functions are wide char strings, and not plain char* strings that is what they expect.

We can use wcstombs() to translate all strings at the beginning of each c++ function, but I'd rather not modify the lib to such an extent...

Is there a way to fix the marshalling between C# and C that works with the .NET Compact Framework?

+1  A: 

Windows CE is heavily biased toward Unicode (most Win32 APIs don't even have ANSI equivalents). As such, the CF doesn't really do well with ANSI either and it needs a little "help" in getting it right.

You can tell the marshaler that you want to pass the data as single-byte, null terminated values by using the MarshalAs attribute (the MSDN docs clearly show it is supported in the CF), something along these lines:

[DllImport("mydll.dll", SetLastError = true)]
public static extern void Foo([MarshalAs(UnmanagedType.LPStr)]string myString);
ctacke
unfortunately, MarshalAs is not supported under Compact Framework
tato
According to who? MarshalAs most certainly *is* supported. I use it quite regularly and I do almost nothing but CF work.
ctacke
MSDN says supported since 3.5 SP1
VirtualBlackFox
A: 

I find this marshal compiler useful even thou it is a bit buggy.