×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

wrapper for c++ to c#

wrapper for c++ to c#

wrapper for c++ to c#

(OP)
Hello,

I'm new in c#-c++ marshaling and wrappers. I need to use a c++ dll where there are the next header and struct:

extern "C" __declspec(dllexport) int GiveMeData(LPCTSTR sID, CustomerData* pData)


typedef struct CustomerData
{
LPTSTR customerName
LONG customerItems;
}


In C#, is correct the next wrapper?


[DllImport("Customers.dll")]
public static extern int GiveMeData(string sID, ref CustomerData pData);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CustomerData
{
public string customerName;
public int customerItems;
}


If isn't correct, how is the best way for do it avoiding possibles heap corruptions, etc?

Thank you!!!!

RE: wrapper for c++ to c#

You might have a problem with customerName, because of .NET string's immutable nature. It would seem that the native DLL will be setting the value of customerName. You can try substituting that with StringBuilder.

RE: wrapper for c++ to c#

You probably need to marshal the strings. I've never used C pointers: only fixed sized arrays. Possibly something like

CODE

[DllImport("Customers.dll")]
public static extern int GiveMeData(
   //                                 ,--  Depending on whether you are
   //                                 |    using unicode.  If not use byte
   [MarshalAs(UnmanagedType.LPArray)] char[] sID, 
   ref CustomerData pData);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CustomerData
{
   // If array of known size, say 16 then add something like
   // [MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)]
   public string customerName; 
   public int customerItems;
} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close