2011-04-25 5 views
0

System :: String^문자를 직사각형 문자 배열로 복사하려고합니다.String ^을 2 차원 char 배열로 가져 오는 방법은 무엇입니까?

char name[25][21]; 

... 

void savedata(int x, System::String^a){ //x is the student #, a is the name 
    int b; 
    using namespace System::Runtime::InteropServices; // for class Marshal 
    char* buffer((char*)(void*)Marshal::StringToHGlobalAnsi(a)); 
    x--; //So we write buffer[b] at data[0][b] when int x is 1 
    for(b = 0; b < 21; b++){ 
     data[x][b] = buffer[b]; 
    }; 
} 

(질문과 관련이없는 다른 코드와 함께.) 나는 그것을 실행하고 디버깅하려고 할 때, "유형 'System.AccessViolationException'처리되지 않은 예외"

먼저 나는 시도 발생했습니다.

String ^을 (2 차원) char 배열에 넣는 더 쉽고/좋은 방법이 있습니까? 그렇지 않다면 여기서 내가 뭘 잘못하고 있습니까?

답변

0

StringToHGlobalAnsi의 결과를 char*으로 변환 할 수 있도록하려면 .ToPointer()으로 전화해야합니다.

또한 StringToHGlobalAnsi의 결과에 FreeHGlobal를 호출해야합니다 (또는 당신은 당신의 char*에서 IntPtr을 다시 만들 수 있습니다).

관련 문제