none
C#调用C++的dll的类型问题 RRS feed

  • 问题

  • C#调用C++封装的dll,方法里面有个char**类型的参数,这个参数是个输出类型的字符串数组,请问这个char**类型对应C#中的哪个类型。我试着用string[]类型对应这个数组,但是报错:尝试读取或者写入受保护的内存。

    我用ref string[]也报错,这个char**作为C++的输出数组,到底对应C#哪个类型呢?


    请问这应该对应哪个类型?恳请各位大侠不吝赐教,我不胜感激!

    2013年5月4日 2:29

答案

  • 封送方式成IntPtr,用Marshal类把指向的目标指针char*的值读出来之后再决定怎么处理。



    Visual C++ MVP

    2013年5月6日 1:40
    版主
  • 你好,

    对于这样的 C++类型:

    char *p1 = "A";
    char *p2 = "BB";
    char *p3 = "CCC";
    char *p4 = "DDDD";
    char *p5 = "EEEEE";
    char *p6 = "FFFFFF";
    char *pp1[] = { p1, p2, p3 };
    char *pp2[] = { p4, p5, p6 };
    char **ppp[] = { pp1, pp2 };
    
    PTRPTRPTR_API char ***ptrptrptr(void)
    {
    return ppp;
    }

    用这样的方法可以读出来:

    [DllImport("D:\\IDEProjects\\VisualStudio\\PtrPtrPt r\\Release\\PtrPtrPtr.dll")]
    private static extern IntPtr ptrptrptr();
    public static void Main(string[] args)
    {
    IntPtr ppp = ptrptrptr();
    IntPtr pp1 = Marshal.ReadIntPtr(ppp, 0);
    IntPtr pp2 = Marshal.ReadIntPtr(ppp, 4);
    IntPtr p1 = Marshal.ReadIntPtr(pp1, 0);
    IntPtr p2 = Marshal.ReadIntPtr(pp1, 4);
    IntPtr p3 = Marshal.ReadIntPtr(pp1, 8);
    IntPtr p4 = Marshal.ReadIntPtr(pp2, 0);
    IntPtr p5 = Marshal.ReadIntPtr(pp2, 4);
    IntPtr p6 = Marshal.ReadIntPtr(pp2, 8);
    string s1 = Marshal.PtrToStringAnsi(p1);
    string s2 = Marshal.PtrToStringAnsi(p2);
    string s3 = Marshal.PtrToStringAnsi(p3);
    string s4 = Marshal.PtrToStringAnsi(p4);
    string s5 = Marshal.PtrToStringAnsi(p5);
    string s6 = Marshal.PtrToStringAnsi(p6);
    Console.WriteLine(s1 + " " + s2 + " " + s3 + " " + s4 + " " +
    s5 + " " + s6);
    }

    引用至 http://bytes.com/topic/c-sharp/answers/554501-c-c-interop-returning-char  


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年5月9日 8:57
    版主

全部回复

  • 尝试string[][],因为C++中的指针的指针明显可以动态开辟字符串组。

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年5月4日 11:47
    版主
  • 封送方式成IntPtr,用Marshal类把指向的目标指针char*的值读出来之后再决定怎么处理。



    Visual C++ MVP

    2013年5月6日 1:40
    版主
  • 您好,我试着封送成IntPtr了,但是也不行,报错:尝试读取或者写入受保护的内存。

    2013年5月6日 5:52
  • 你好,

    对于这样的 C++类型:

    char *p1 = "A";
    char *p2 = "BB";
    char *p3 = "CCC";
    char *p4 = "DDDD";
    char *p5 = "EEEEE";
    char *p6 = "FFFFFF";
    char *pp1[] = { p1, p2, p3 };
    char *pp2[] = { p4, p5, p6 };
    char **ppp[] = { pp1, pp2 };
    
    PTRPTRPTR_API char ***ptrptrptr(void)
    {
    return ppp;
    }

    用这样的方法可以读出来:

    [DllImport("D:\\IDEProjects\\VisualStudio\\PtrPtrPt r\\Release\\PtrPtrPtr.dll")]
    private static extern IntPtr ptrptrptr();
    public static void Main(string[] args)
    {
    IntPtr ppp = ptrptrptr();
    IntPtr pp1 = Marshal.ReadIntPtr(ppp, 0);
    IntPtr pp2 = Marshal.ReadIntPtr(ppp, 4);
    IntPtr p1 = Marshal.ReadIntPtr(pp1, 0);
    IntPtr p2 = Marshal.ReadIntPtr(pp1, 4);
    IntPtr p3 = Marshal.ReadIntPtr(pp1, 8);
    IntPtr p4 = Marshal.ReadIntPtr(pp2, 0);
    IntPtr p5 = Marshal.ReadIntPtr(pp2, 4);
    IntPtr p6 = Marshal.ReadIntPtr(pp2, 8);
    string s1 = Marshal.PtrToStringAnsi(p1);
    string s2 = Marshal.PtrToStringAnsi(p2);
    string s3 = Marshal.PtrToStringAnsi(p3);
    string s4 = Marshal.PtrToStringAnsi(p4);
    string s5 = Marshal.PtrToStringAnsi(p5);
    string s6 = Marshal.PtrToStringAnsi(p6);
    Console.WriteLine(s1 + " " + s2 + " " + s3 + " " + s4 + " " +
    s5 + " " + s6);
    }

    引用至 http://bytes.com/topic/c-sharp/answers/554501-c-c-interop-returning-char  


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年5月9日 8:57
    版主
  • 您好,我试着封送成IntPtr了,但是也不行,报错:尝试读取或者写入受保护的内存。


    这个我很少用,感觉unsafe这个关键字能帮助你,你试试!
    • 已建议为答案 吴道德 2013年5月16日 8:03
    2013年5月15日 3:38