简体   繁体   中英

Passing a string from a DLL to C#

I have a C# application that accesses functions from an unmanaged C++ libary using a CLI DLL. My problem is that I can't get text from the C++ library to be passed through to the C# application correctly. The code I have thus far is as follows:

C#

[DllImport("Wrapper.dll", EntryPoint = "GetNameLength", CallingConvention = CallingConvention = CallingConvention.Cdecl)]
public static extern bool _GetNameLength( int index, out int size);
[DllImport("Wrapper.dll", CharSet = CharSet.Ansi, EntryPoint = "GetName", CallingConvention = CallingConvention.Cdecl)]
public static extern bool _GetName( out Stringbuilder name, int size, int index);

private void TestFunction()
{
    int size = 0;
    _GetNameLength(2, out size);
    StringBuilder str = new StringBuilder(size + 1);

    _GetName(out str, str.Capacity, 2);

    btnName.Text = str.ToString();
}

CLI

// In the header.
#define DllExport __declspec( dllexport)

extern "C" {
    DllExport bool __cdecl GetNameLength( int index, int& size);
    DllExport bool __cdecl GetName( char* name, int size, int index);
};


// In the '.cpp'.
bool __cdecl GetNameLength( int index, int& size) {
    if( gp_app == nullptr)
        return false;

    gp_app->GetNameLength( index, size);

    return true;
}

bool __cdecl GetName( char* name, int index, int size) {
    if( gp_app == nullptr)
        return false;

    const char* n = "";
    n = gp_app->GetName( index);

    name = new char[size];
    strcpy( name, n);

    return true;
}

C++

// In the header.
class App {
    void GetNameLength( int index, int& size);
    const char* GetName( int index);
};

// In the '.cpp'.
void App::GetNameLength( int index, int& size) {
    if( index >= 0 && index < gs_namesCount)
        size = strlen( gs_names[index]);
}

const char* App::GetName( int index) {
    if( index >= 0 && index < gs_namesCount)
        return gs_names[index];

    return nullptr;
}

I am able to debug into the DLL and I've seen that it does copy the name over correctly, even when I just use name = const_cast<char*>( n) (and when I use a string instead of a StringBuilder), but for some reason that value isn't getting back to C# from the DLL.

I read that a possible reason is because C++ may use ASCII characters while C# uses 16-bit characters, and that the fix for that is to include CharSet = CharSet.Ansi so that it's marshalled correctly when it's returned to C#, but that apparently hasn't helped.

Can anyone tell me what I'm doing wrong?


Solution :
Use a public ref class instead of a namespace and declare functions using void Foo( [Out] String^ %name) ensuring that using namespace System::Runtime::InteropServices; is included.

This tells me you actually have a managed c++ dll.

if( gp_app == nullptr)
    return false;

If so you can export a managed class and use String as parameter to the method.

Check this: http://msdn.microsoft.com/library/windows/apps/hh699870.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM