当我到达strcpy线时,我正在
An unhandled exception of type 'System.AccessViolationException' occurred.
Additional information: Attempted to read or write protected memory.
This is often an indication that other memory is corrupt.
char* str; char* out;
str = (char*) Marshal::StringToHGlobalAnsi(Parms["AVI"]).ToPointer();
strcpy(out, str);
Marshal::FreeHGlobal(IntPtr(str));
答案 0 :(得分:2)
您需要分配内存并将out
指向该内存。就像现在一样,out
指向一些随机存储位置。
答案 1 :(得分:0)
设置str后,但在将其复制到out之前:
char* out = (char*)malloc((strlen(str) + 1) * sizeof(char));
if (out != NULL) {
strcpy(out, str);
}
因为你需要某种缓冲区来复制。完成后一定要自由(退出)。
答案 2 :(得分:0)
How to convert from System::String* to Char* in Visual C++
“方法3”看起来非常简单。无需手动释放内存。