MSDOTnet.org Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Marshalling C++ ANSI char*

 
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> Compact Framework
Author Message
jonfroehlich



Joined: 08 Aug 2007
Posts: 2

PostPosted: Tue Feb 05, 2008 9:07 pm    Post subject: Marshalling C++ ANSI char* Reply with quote

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

---
http://csharponphone.blogspot.com

Archived from group: microsoft>public>dotnet>framework>compactframework
Back to top
View user's profile Send private message
user



Joined: 08 Aug 2007
Posts: 286

PostPosted: Wed Feb 06, 2008 4:09 am    Post subject: Re: Marshalling C++ ANSI char* Reply with quote

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



"jonfroehlich" wrote in message @d70g2000hsb.googlegroups.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.
>
> Thanks!
>
> Jon
>
> ---
> http://csharponphone.blogspot.com
Back to top
View user's profile Send private message
jonfroehlich



Joined: 08 Aug 2007
Posts: 2

PostPosted: Wed Feb 06, 2008 5:11 am    Post subject: Re: Marshalling C++ ANSI char* Reply with quote

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


On Feb 5, 9:09 pm, "" wrote:
> 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
>
> "jonfroehlich" wrote in message
>
> @d70g2000hsb.googlegroups.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.
>
> > Thanks!
>
> > Jon
>
> > ---
> >http://csharponphone.blogspot.com
Back to top
View user's profile Send private message
shahalh



Joined: 06 Feb 2008
Posts: 1

PostPosted: Wed Feb 06, 2008 6:27 am    Post subject: Re: Marshalling C++ ANSI char* Reply with quote

Here is a nice link that explains the conversions:
http://www.codeproject.com/KB/dotnet/Win32APICPlusPlustoDotNET.aspx
I hope this does the trick Wink

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
Marshalling Arrays from VB.NET TO ASP Hello All, What attributes do I specify to marshal an array of objects from VB.NET To ASP. I have already tried and in vain!! LPArray works in VB in the Late binding mode, but does not work for ASP or in the e

Substitute for marshalling interfaces Hi there. A method of my of a COM interface takes an interface as parameter, which I never use. I could save the code for the definition of that interface, but what would be a substitute for that parameter ?? Maybe IntPtr ?? Thanks Daniel

Marshalling Nested Structures Hi there, I have a wiered scenario. I have two structures defined this way in unmanaged dll. struct A{ char * name; int ID; }; struct B{ struct A* record; }; Then I have a function in the dll that consumes a pointer to struct B. like this. int InitRecord(

Marshalling a self-referring structure (from C++ to C#) Hello there. I'll try to describe shortly my problem, hoping you can help me to find a solution to it. I have a C function like this: static extern message It returns a structure of this kind: struct message { char

Marshalling DEV_BROADCAST_PORT: C# P/Invoke declaration? Hello, I have some VS 2005 code in which I am setting up a C# declaration of the Platform SDK header That structure is declared in the Platform SDK header file dbt.h as follows: typedef struct { DWORD dbcp_
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> Compact Framework All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group