Getting the size of the array pointed to by IntPtr

后端 未结 1 380
遥遥无期
遥遥无期 2021-01-26 05:17

I have a native C++ function that I call from a C# project using pinvoke.

extern \"C\" _declspec(dllexport) void GetCmdKeyword( wchar_t** cmdKeyword, uint  pCmd         


        
相关标签:
1条回答
  • 2021-01-26 05:39

    You cannot infer the length from the pointer. The information must be passed as a separate value alongside the pointer to the array.

    I wonder why you use raw IntPtr rather than C# arrays. I think the answer you accepted to your earlier question has the code that you need: Pinvoking a native function with array arguments.


    OK, looking at the edit to the question, the actual scenario is a little different. The function returns a pointer to a null-terminated array of wide characters. Your pinvoke should be:

    [DllImport(...)]
    static extern void GetCmdKeyword(out IntPtr cmdKeyword, uint pCmdNum);
    

    Call it like this:

    IntPtr ptr;
    GetCmdKeyword(ptr, cmdNum);
    string cmdKeyword = Marshal.PtrToStringUni(ptr);
    
    0 讨论(0)
提交回复
热议问题