Wrapping MS P2P sdk

Hey all, I'm writing a wrapper for XP's peer2peer sdk so that it can run under managed code, and I can use it as a library in some apps I'm working on.  I'm not a huge C guru (which is what the SDK is written in) but I have some decent knowledge of C++, which is what I'm writing the wrapper in.  I have a question, I'm abstracting the SDK to a much higher level, and I am wondering how I can convert a System::String (managed string in c++) to a PWSTR, the "string" class that they are using in the SDK.  I just can't figure it out other than some realy brute force methods....is there a function that will help me convert these things   Any thoughts/ideas would be greatly appreciated!

Answer this question

Wrapping MS P2P sdk

  • Monarghel

    Hmmm, I think I may have found it out...do you think this will cause problems   It complies at least  :)


    String *peer = something that we get passed

    wchar_t peerChar = peer->get_Chars(peer->Length);

    PWSTR pwzFriendlyName = &peerChar;

  • zobrakster

    For const whar_t* parameters you can use the PtrToStringChars
     utility function in vcclr.h. For non-const parameters you'll have to create a copy of the string, since managed strings are immutable.


  • Kiran Suthar

    Tried that...here's my code....


    PWSTR pwzFriendlyName = PtrToStringChars(peer);


    where peer is of type System::String

    The error I'm now getting is:
    error C2440: 'initializing' : cannot convert from 'const __wchar_t __gc *' to 'PWSTR'

    I see that __gc is what it's returning so it's garbage collected.....I think that may cause a problem to start with...but I still can't figure out how to get my desired PWSTR.

  • RatOmeter

    Got it all figured out....

    IntPtr tmp= Marshal::StringToCoTaskMemUni(peer);

    PWSTR fName = (PWSTR)tmp.ToPointer();

    I belive this code does the same as the above posted code does....

  • shy_man

    You must first assign the return value to a pinning pointer:

    const wchar_t __pin *tmp = PtrToStringChars(peer);
    PWSTR pwzFriendlyName = (PWSTR)tmp;

    The other code you posted doesn't do what you want.


  • Wrapping MS P2P sdk