Marshalling C++ ANSI char*

J

jonfroehlich

How can I marshal C# strings to C++ ANSI char* and vice versa? For
example, I am dealing with the following C++ struct.

/**
* WPS_SimpleAuthentication is used to identify
* the user with the server.
*/
typedef struct
{
/**
* the user's name, or unique identifier.
*/
const char* username;

/**
* the authentication realm
*/
const char* realm;
} WPS_SimpleAuthentication;

I would like to call this C++ function:
extern WPS_ReturnCode SimplifiedFunction(const
WPS_SimpleAuthentication* authentication);

The analog C# struct with the full .NET Framework is:

// simple authentication, only used for sending information out.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct WPS_SimpleAuthentication
{
public String username;
public String realm;

//constructor
public WPS_SimpleAuthentication(String username, String realm)
{
this.username = username;
this.realm = realm;
}
}

However, .NET CF does not have CharSet.Ansi. What should I do?

As a follow up, I also need to be able to Marshal C++ ANSI char* back
to C# string types. For example, how would I do this?

string example =
Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(int_street_addr.address_line))

Oh and forgive me if I am not using the "Marshal" word here in the
right way.

Thanks!

Jon
 
G

Guest

Add the following attribute:
[MarshalAs(UnmanagedType.LPStr)]

Something like this:

public struct WPS_SimpleAuthentication
{

[MarshalAs(UnmanagedType.LPStr)]
public String username;

[MarshalAs(UnmanagedType.LPStr)]
public String realm;
}


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
J

jonfroehlich

Hi Chris,

Thanks for the response. I made the change to all relevant pieces of
code but unfortunately I'm still receiving a NotSupportedException
when I try to P/Invoke a method using this struct. I believe the
[MarshalAs(UnmanagedType.LPStr)] is correct, though, because I made
the same modification to the desktop code and it still worked without
problem.

The actual method I am trying to call has three parameters, so perhaps
the problem exists with Marshalling one of the other two parameters.

The C signature for the method I'm trying to call:
extern WPS_ReturnCode
WPS_location(const WPS_SimpleAuthentication* authentication,
WPS_StreetAddressLookup street_address_lookup,
WPS_Location** location);

The C# signature for the wrapper class I'm trying to build:

[DllImport("wpsapi.dll")]
private static extern WPS_ReturnCode WPS_ip_location(ref
WPS_SimpleAuthentication authentication,

WPS_StreetAddressLookup street_address_lookup,
out IntPtr
location);

Thanks for your help.

Jon


Add the following attribute:
[MarshalAs(UnmanagedType.LPStr)]

Something like this:

public struct WPS_SimpleAuthentication
{

[MarshalAs(UnmanagedType.LPStr)]
public String username;

[MarshalAs(UnmanagedType.LPStr)]
public String realm;

}

--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded communityhttp://community.OpenNETCF.com


How can I marshal C# strings to C++ ANSI char* and vice versa? For
example, I am dealing with the following C++ struct.
/**
* WPS_SimpleAuthentication is used to identify
* the user with the server.
*/
typedef struct
{
/**
* the user's name, or unique identifier.
*/
const char* username;
/**
* the authentication realm
*/
const char* realm;
} WPS_SimpleAuthentication;
I would like to call this C++ function:
extern WPS_ReturnCode SimplifiedFunction(const
WPS_SimpleAuthentication* authentication);
The analog C# struct with the full .NET Framework is:
// simple authentication, only used for sending information out.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct WPS_SimpleAuthentication
{
public String username;
public String realm;
//constructor
public WPS_SimpleAuthentication(String username, String realm)
{
this.username = username;
this.realm = realm;
}
}
However, .NET CF does not have CharSet.Ansi. What should I do?
As a follow up, I also need to be able to Marshal C++ ANSI char* back
to C# string types. For example, how would I do this?
string example =
Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(int_street_addr.address_line))
Oh and forgive me if I am not using the "Marshal" word here in the
right way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top