Question

I have the following code

    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);
}

This works when I run the program directly from Visual Studio but when I build .exe file and run as standalone I get the following error

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

I know that it is caused by the following line

ErrorToString(lngErrorcode, errorcode);

This is a call to 3rd party code, I assume the error has to do with the code not dealing with the unmanaged code correctly but I am unsure where. Could someone please point me in the right direction.

Was it helpful?

Solution

I don't know what ErrorToString expects as arguments, but I'd say it is a char* representing a pointer to a buffer where it can store the result string.

In this case your code:

//Obtain error string
char* errorcode = new char;
ErrorToString(lngErrorcode, errorcode);

looks wrong (it is allocating a single char).

Try changing it to:

//Obtain error string
char* errorcode = new char[1024];
ErrorToString(lngErrorcode, errorcode);

and see if this works (in this case don't forget you need to release the memory later).

Hope this helps.

OTHER TIPS

You're making all of this far too complicated. Instead of

    //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);

All you need is:

    //Obtain error string
    char errorString[1024];
    ErrorToString(lngErrorcode, errorString);
    MainForm::textBox_LoadCellError->Text = gcnew System::String(errorString);

You originally had two problems: not having a big enough buffer, and mismatching allocation and deallocation functions. After using new you must use delete, not Marshal::FreeHGlobal. But there was no reason to do any dynamic allocation at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top