CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Feb 2001
    Location
    Virginia
    Posts
    60

    Returning a string from a C++ dll

    Hello,

    I have a C++ dll that needs to return a string to a C# program. What's the best way to do this? I've tried several things, but I can't seem to get it to work.

    I really appreciate any help.

    Thanks,
    Nick

  2. #2
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    In C++
    Code:
    /* Dll export specifier*/ LONG SomeFuncName(BSTR* bstrRetString)
    {
        // some stuff
        // Fill *bstrRetString with appropriate string
        _bstr_t bs("this is some string which I want to return to the c# prog");
        *bstrRetString = bs.copy();
    
        return 0L;
    }
    You can define this function when it is imported into the C# as following:
    Code:
    class DllWrapper
    {
        [DllImport("YourDll.dll")]
        public static extern long SomeFuncName(out string str);
    }
    than you can use it:
    Code:
    void SomeOtherFunc()
    {
        string str;
        long lRet = DllWrapper.SomeFuncName(out str);
        MessageBox.Show("Returned string: " + str);
    }
    Martin
    Last edited by MartinL; March 5th, 2003 at 05:06 AM.

  3. #3
    Join Date
    Feb 2001
    Location
    Virginia
    Posts
    60
    Martin,

    Thank you very much for your help. I've got everything running, but for some reason I only seem to be getting the first letter of the string that I am returning. For testing I used your test string below and I always get "t" returned. What does OL need to be that I return? Anything specific?

    Thanks again for your help.

    Regards,
    Nick

  4. #4
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    try this one out,
    CString s( "abcd" );
    LPTSTR p = s.GetBuffer( 10 ); // LPTSTR will be ur ref string
    strcpy( p, "Hello" ); // directly access CString buffer
    s.ReleaseBuffer( );

    <edit>
    this will be in your C++ code. if CString is not allowed (forexample in case of embeded environment) then just use std::string of STL

    </edit>
    Paresh

  5. #5
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    No paresh... That is not the thing...

    I just trying it... I wrote the answer down and didn't test it...

    I wrote win32 dll which exports just one function. Function returns int and takes BSTR* as parameter...

    Then, I wrote C# client (console app) as I post in my answer. It really gets just the first character of the string I want to pass to the caller of the dll's function...

    Then, I wrote C prog for testing that dll. It works ok...

    I think, the problem is in using unicode string. However I still didn't find solution. I used the same mechanism like that in my answer in several COM objects I wrote. When I used those COM objects from the C#, they worked well... I really don't know why I can't get unicode string from the DLL into this C# app...

    I attached the project to this mail...
    ConsApp is C# app for testing dll
    TryDll is Win32 DLL
    TryCpp is C prog for testing dll

    solution file is in TryDll dir...

    Martin
    Attached Files Attached Files

  6. #6
    Join Date
    Feb 2001
    Location
    Virginia
    Posts
    60
    Hello Paresh,

    Thank you very much for your help. I am using CStrings. It works if I set the CString to a constant, but I am calling a function that returns a CString in my dll.

    Here is some more detail. I've got an open CDatabase object and I want to return the GetConnect() value back to the C# program. Here is what I've got:

    CString getConnectString;
    getConnectString= myDB.GetConnect();

    LPTSTR p = getConnectString.GetBuffer(getConnectString.GetLength());
    strcpy( p, connectStr );
    connectStr.ReleaseBuffer( );

    My definitions still look basically like what Martin suggested, but now I'm using LPTSTR. Would that be correct?

    Thanks again for your help.

    Regards,
    Nick

  7. #7
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    ya, you can use LPTSTR
    in you C# program pass string as a ref
    and take LPTSTR (actually a pointer to char) and then by allocating the size and releasing the buffer is fine.
    check out Martin's sample also.

    tnx
    Paresh

  8. #8
    Join Date
    Feb 2001
    Location
    Virginia
    Posts
    60
    Paresh,

    When I run Martin's sample I only get the first character of the string that is returned (when using the C# client). I think he said the same thing. When I run the C++ program that calls the dll it returns the whole string.

    Thanks,
    Nick

  9. #9
    Join Date
    Nov 2000
    Location
    USA
    Posts
    179
    Hi guys!
    Try this!
    It should be work!

    // Dll C++
    extern "C" DllExport int GetStringFromDLL(WCHAR** ppString)
    {
    LPTSTR p = getConnectString.GetBuffer(getConnectString.GetLength()); // or something else!
    *ppString = (LPTSTR)p;
    return 1;
    }


    // C#
    [DllImport("Your.dll")]
    public static extern int GetStringFromDLL(ref IntPtr p);


    void SomeFunction()
    {
    IntPtr ptr = IntPtr.Zero;
    int res = GetStringFromDLL(ref ptr);
    string MyString = Marshal.PtrToStringUni(ptr);
    }

    Good luck!

    Max

  10. #10
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    and yes marshalling is requred here. i forgot to mention. thanks GMV

    Paresh

  11. #11
    Join Date
    Feb 2001
    Location
    Virginia
    Posts
    60
    You all are geniuses! Thank you very much for your help.

  12. #12
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    Yea... I was working too long yesterday... And even made more mistakes...

  13. #13
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    Martin,
    i don't think so you made mistakes


    Paresh

  14. #14
    Join Date
    Oct 2001
    Posts
    4

    Unhappy

    Hi there,

    I have a 'similar' problem - except I want to return a string from a C# DLL to a C/C++ application as an output parameter. I've just about given up. Help!

    Sarah

  15. #15
    Join Date
    Oct 2001
    Posts
    4

    Smile

    After a lot of trial and error (and much hair pulling) I have finally managed to get a string out of a C# DLL into a C/C++ application. So unless anyone has a better suggestion: use unsafe pointers in the C# code. I used the C# sbyte* type which corresponds to the C/C++ char* type. Note that the C# code has to be compiled with /unsafe.

Page 1 of 2 12 LastLast

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