CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2010
    Posts
    1

    send string from C++ dll to C#

    Hi,

    I have a C++ dll that needs to send a string to C#. I know that C++ uses ASCII for storing characters and C# uses UNICODE instead. I like to know what would be the best approach to receive the string on the c# end and how should it be implemented. This is what i have now but i'm getting garbage on the c# end because of the different charcter coding.


    I'm getting "D¹esult = 1.234" from the C# end while i send "My Result = 1.234" from c++ dll.


    Thanks

    ////////////////////////////////////////
    //c++
    ////////////////////////////////////////
    //Test.h
    extern "C" __declspec( dllexport ) const char* TestStringReturn();

    #include "Test.h"
    extern "C" __declspec( dllexport ) const char* TestStringReturn()
    {
    std::stringstream ReturnServiceResult;

    ReturnServiceResult << "My Result = 1.234";

    return ReturnServiceResult.str().c_str();
    }

    ////////////////////////////////////////
    //c#
    ////////////////////////////////////////

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    using System.IO;


    namespace Test
    {
    static class Program
    {
    [DllImport(@"D:\TestStringReturn.dll")]

    static extern string TestStringReturn();

    [StructLayout(LayoutKind.Sequential)]
    struct Args
    {
    public string ip;
    public string argparams;
    }

    static void Main()
    {
    string ReturnString;

    ReturnString = TestStringReturn();

    }
    }
    }

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: send string from C++ dll to C#

    I like to know what would be the best approach to receive the string on the c# end and how should it be implemented.
    BSTR is system wide known string type.
    Best regards,
    Igor

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: send string from C++ dll to C#

    Returning a pointer to a string in this manner is problematic - that's why no Win32 api's do it this way. Your C code shouldn't either.

    To fix your problem. See how windows prototypes the method call for passing a string back to the client. A good example of these api's are GetModuleFileName or GetComputerName. Each take a pointer to a string buffer and size of the buffer as parameters. Clients create a buffer, pass in the size and the api copies the data to the buffer. Notice the clients are responsible for allocating the buffer and cleaning it up (if necessary). The server (the api in the dll in this case) is not responsible for string allocation or cleanup - the reason is that the api has know idea when the client is finished with the string.

    At any rate, use the two aforementioned api's as patterns, and change your api to look just like them.

    Now for the C# side. After you've changed the C api, go to www.pinvoke.com and search for the correct pinvoke signature for GetModuleFileName or GetComputerName to see how to make the pinvoke call to your api.

    It's pretty easy once you've done it, but you have to start out with the proper C function prototype.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: send string from C++ dll to C#

    Quote Originally Posted by Igor Vartanov View Post
    BSTR is system wide known string type.
    BSTR implies COM is being used. So I wouldn't use it unless your api is exposed as a COM server.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: send string from C++ dll to C#

    Btw, most Win32 api's have an A and W version (e.g. GetComputerNameA and GetComputerNameW).

    The pinvoke signature has the ability to specify a charset. Normally with an api with an A and W version, pinvoke handles this by default.

    Since your api only exposes an ANSI version, you'll want to use CharSet.Ansi.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured