繁体   English   中英

将字符串从DLL传递给C#

[英]Passing a string from a DLL to C#

我有一个C#应用程序,它使用CLI DLL从非托管C ++库中访问函数。 我的问题是我无法将C ++库中的文本正确传递给C#应用程序。 我到目前为止的代码如下:

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;
}

我能够调试DLL并且我已经看到它确实正确地复制了名称,即使我只使用name = const_cast<char*>( n) (当我使用字符串而不是StringBuilder时),但由于某种原因,该值不会从DLL返回到C#。

我读到一个可能的原因是因为C ++可能使用ASCII字符,而C#使用16位字符,并且修复它是为了包括CharSet = CharSet.Ansi以便它在返回C#时正确编组,但显然没有没有帮助。

谁能告诉我我做错了什么?


方案
使用public ref class而不是命名空间,并使用void Foo( [Out] String^ %name)声明函数,确保using namespace System::Runtime::InteropServices; 已经包括了。

这告诉我你实际上有一个托管的c ++ dll。

if( gp_app == nullptr)
    return false;

如果是这样,您可以导出托管类并使用String作为方法的参数。

请检查: http//msdn.microsoft.com/library/windows/apps/hh699870.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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