我有以下代碼。混合託管和非託管代碼的問題
System::Void MainForm::initLoadCell(){
//Open the first found LabJack U3 over USB.
lngErrorcode = OpenLabJack (LJ_dtU3, LJ_ctUSB, "1", TRUE, &lngHandle);
//Load defualt config
lngErrorcode = ePut (lngHandle, LJ_ioPIN_CONFIGURATION_RESET, 0, 0, 0);
//Setup FIO0 as an analogue input port
lngErrorcode = ePut (lngHandle, LJ_ioPUT_ANALOG_ENABLE_BIT,0,1,0);
//Obtain error string
char* errorcode = new char;
ErrorToString(lngErrorcode, errorcode);
// Convert the c string to a managed String.
String^errorString = Marshal::PtrToStringAnsi((IntPtr) (char *) errorcode);
MainForm::textBox_LoadCellError->Text = errorString;
Marshal::FreeHGlobal((IntPtr)errorcode);
}
,當我直接從Visual Studio運行程序,但是當我生成.exe文件,並運行作爲獨立的,我得到以下錯誤
這工作Problem signature:
Problem Event Name: APPCRASH
Application Name: BenchTester.exe
Application Version: 0.0.0.0
Application Timestamp: 52f4c0dd
Fault Module Name: ntdll.dll
Fault Module Version: 6.1.7601.18247
Fault Module Timestamp: 521ea8e7
Exception Code: c0000005
Exception Offset: 0002e3be
OS Version: 6.1.7601.2.1.0.256.1
Locale ID: 3081
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
Read our privacy statement online:
http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409
If the online privacy statement is not available, please read our privacy statement offline:
C:\Windows\system32\en-US\erofflps.txt
我知道,它是由以下行
ErrorToString(lngErrorcode, errorcode);
這是第3次打電話引起的我假設錯誤與代碼沒有正確處理非託管代碼有關,但我不確定在哪裏。有人能請我指出正確的方向嗎?
謝謝,這將實際上幫助,但沒有解決問題,我想通過使用只是新的字符它會創建所需的大小。無論如何,不幸的是我仍然有錯誤。我發現周圍的工作第三方硬件實際上帶有.net包裝,所以現在好了,謝謝。 – codem
甚至更好:'char errorCode [1024];'當不需要時避免動態分配。 –