2010-04-20 56 views
1

這是我的C++結構(使用多字節字符集)元帥管理的String []非託管的char **

typedef struct hookCONFIG { 
    int threadId; 
    HWND destination; 

    const char** gameApps; 
    const char** profilePaths; 
} HOOKCONFIG; 

和.NET結構

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] 
public struct HOOKCONFIG { 
    public int threadId; 
    public IntPtr destination; 

    // MarshalAs? 
    public string[] gameApps; 

    // MarshalAs? 
    public string[] profilePaths; 
} 

我得到了一些問題,即如何我是否編組字符串數組? 當我訪問結構變量「profilePaths」在C++我得到這樣的錯誤:

類型「System.AccessViolationException」的未處理的異常發生在APP.EXE

其他信息:嘗試讀取或寫保護的內存。這通常是指示其他內存已損壞的 。

MessageBox(0, cfg.profilePaths[0], "Title", MB_OK); // error ... Orz 
+0

你可以試試嗎評論HWND/IntPtr? – 2010-04-20 02:08:57

回答

1

簡單的方法:更改原型IntPtr的[]:

public IntPtr[] gameApps; 
public IntPtr[] profilePaths; 

現在,當你打電話,你需要大致有以下psudo代碼:

GCHandle handle = GCHandle.Alloc(string); 
gameApps = new IntPtr[] { GCHandle.ToIntPtr(handle) }; 

// Unmanaged call 

handle.Free();