locked
Marshall CString ? RRS feed

  • Question

  • I have an exported function in myfunc.dll

    MYDLL_API bool myfunc(LPCWSTR outString)

    {

          CString res = L"myspecialstring I want to return";

         // I'm trying to

        CAtlString s = res;

     outString = s.Detach();

    return true;

    }

     

    in C# I want to get value of string res:

    [DllImport("myfunc.dll")]

    [return: MarshalAs(UnmanagedType.Bool)]

    private static extern bool myfunc(ref string outString);

     

    String outString = "";

    bool res = myfunc(ref outString)

    I want to get CString from myfunc. I could change function signature. I've tried to use different myfunc-signatures and marshall-signatures. With boolean value it's OK, but outString is allways null.

     

    Wednesday, January 24, 2007 12:23 PM

Answers

  • Solution:

    MYDLL_API bool myfunc(BSTR outString);

     

    [DllImport("selectuser.dll")]

    [return: MarshalAs(UnmanagedType.Bool)]

    private static extern bool myfunc(ref IntPtr userInfo);

     

    IntPtr userInfo = IntPtr.Zero;

    bool res = myfunc(ref userInfo);

    String str = Marshal.PtrToStringBSTR(userInfo);

    Marshal.FreeBSTR(userInfo);

    Wednesday, January 24, 2007 12:38 PM