2010-04-20 5 views
1

이것은 내가 어떻게 몇 가지 문제를 가지고원수는 관리되지 않는 문자로 문자열 []를 관리 **

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

    // MarshalAs? 
    public string[] gameApps; 

    // MarshalAs? 
    public string[] profilePaths; 
} 

내 C++ 구조체 (사용 멀티 바이트 문자 집합)

typedef struct hookCONFIG { 
    int threadId; 
    HWND destination; 

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

및 .NET 구조체입니다 문자열 배열을 마샬링합니까?

유형 'System.AccessViolationException'의 처리되지 않은 예외가 App.exe가 발생

추가 정보 : 시도가 읽을 수있는 내가 C에서 구조체 변수 "profilePaths"를 액세스 할 때 는 ++ 나는이 같은 오류가 발생했습니다 또는 쓰기 방지 된 메모리. 다른 메모리가 손상되었다는 표시 인 경우가 종종 입니다.

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

당신이 그것을 시도 할 수

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

이제 당신을 호출 할 때 대략 다음 psudo-코드가 필요합니다 HWND/IntPtr을 주석 처리 하시겠습니까? –

답변

1

쉬운 방법 : IntPtr입니다 []로 변경 프로토 타입 :

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

// Unmanaged call 

handle.Free(); 
관련 문제