元帅C ++"char **"在C#中 [英] Marshal C++ "char**" in C#

查看:95
本文介绍了元帅C ++"char **"在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从C ++调用C#方法,并将char**作为参数传递.它必须为char**,因为我需要通过参数返回值.

I'm calling C# method from C++ and passing char** as argument. It has to be char** because I need to return value through parameter.

C#代码:

[ExportDll("test", System.Runtime.InteropServices.CallingConvention.StdCall)]

public static int test([MarshalAs(UnmanagedType.AnsiBStr)] ref string p)
{
    Console.WriteLine(p);
}

调用函数的C ++代码:

C++ code to invoke function:

typedef int (__stdcall *MYPROC)(char **); 

VOID main(VOID) 
{ 
    HINSTANCE hinstLib; 
    MYPROC MyProc; 
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 

    hinstLib = LoadLibrary(TEXT("mydll.dll")); 

    if (hinstLib != NULL) 
    { 
        ProcAdd = (MYPROC) GetProcAddress(hinstLib, "test"); 

        if (NULL != ProcAdd) 
        {
            fRunTimeLinkSuccess = TRUE;
            char s1[] = "test"; 
            char *s2 = s1;
            char **s3 = &s2;    
            (MyProc) (s3); 
            cout << s3;
        }
        fFreeResult = FreeLibrary(hinstLib); 
    } 
}

传递char *很简单(在c#中删除ref,在c ++中使用char *),但是当尝试传递char **时,我在调用函数:(

It's simple to pass char* (remove ref in c#, and use char* in c++), but when trying to pass char** i get a runtime error on line where I call the function :(

在c#中,Console.WriteLine打印出正确的值,但是此后,我得到一个错误:

in c#, Console.WriteLine prints out correct value, but after that, I get an error:

Windows has triggered a breakpoint in COMDynamicLoad.exe.

This may be due to a corruption of the heap, which indicates a bug in COMDynamicLoad.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while COMDynamicLoad.exe has focus.

The output window may have more diagnostic information.

我应该怎么做?

推荐答案

您声明了ref UnmanagedType.AnsiBStr,但您期望使用char**.这是行不通的,因为对BSTR的引用不是char **.有关封送处理声明的示例,请参见字符串的默认封送处理.这些是输入输出字符串的可能声明:

You declare ref UnmanagedType.AnsiBStr but you expect a char**. This cannot work, since a ref to a BSTR is not a char**. See Default Marshaling for Strings for examples of marshaling declarations. These are possible declarations for an input-output string:

PassStringRef2([in, out] BSTR *s);
PassStringRef3([in, out] LPStr *s);
PassStringRef4([in, out] LPWStr *s);

和等效的C#封送声明为:

and the equivalent C# marshaling declarations are:

PassStringRef2([MarshalAs(UnmanagedType.BStr)]ref String s);
PassStringRef3([MarshalAs(UnmanagedType.LPStr)]ref String s);
PassStringRef4([MarshalAs(UnmanagedType.LPWStr)]ref String s);

您的char**声明与LPStr *s等效,因此正确的封送处理为[MarshalAs(UnmanagedType.LPStr)]ref String s.但更好的选择是使用BSTR,因为它有显式的长度声明,并使用

Your char** declaration is the equivalent of LPStr *s, so the correct marshaling is [MarshalAs(UnmanagedType.LPStr)]ref String s. But a better option is to use BSTR because of the explicit length declaration, and manipulate it in C++ with the BSTR helpers.

这篇关于元帅C ++"char **"在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆