VM140 Error: 5

Hi,

I found a problem when I call the DLL function Connected.

This is the secuence:

I Open correctly the card 0 wiht OpenDevice.

I disconnect the USB wire.

When I try to call any function after this, the DLL display “Error: 5”, why??

If I call the “Connected” function , the return value is 1 instead of 0, why?? (PowerGood fnction works properly)


If The USB is disconnect before call OpenDevice, the Connected function works properly.

My application is based on the C example but with a Connected and PowerGood functions call (why the example program not use the Connected function ?)

I’m using k8061_softwarepack_2011.zip package

can you help me, thanks.

I tested the same with the example of VC + + 2008 and works properly, the Connected function returns 0 when the USB is disconneted, but I need use mingw at windows SO (MinGW-gcc440_1.zip)

[code]#include
#include <windows.h>
#include

using namespace std;

typedef void(WINAPI *t_func0)();
typedef int(WINAPI *t_func5)();
typedef bool(WINAPI *t_func9)(int);

t_func5 OpenDevice;
t_func9 PowerGood;
t_func9 Connected;
t_func0 CloseDevices;

int init();

HINSTANCE hDLL;
int foundDLL = 0;

int main(void)
{
int CardAddress;

int h = init();
    if (!h)
    {
            cout << "DLL found" << endl;
            foundDLL = 1;
    }
    else
    {
            cout << "DLL not found" << endl;
            cout << "Error code: " << h << endl;
    cout << "Press a key to exit." << endl;
    cin.get();
    }
    if (foundDLL)
    {
            CardAddress = OpenDevice();
            if (CardAddress >= 0)
            {
                    cout << "Card #"<< CardAddress << " opened." << endl;
            }
            else
            {
                    cout << "Card not found" << endl;
            }
    }
    if (foundDLL)
    {

        bool ret;
        cout << "Press Enter to \"PowerGood\"" << endl;
        cin.get();
        ret  = true;
        ret = PowerGood(CardAddress);
        cout << "--------------  PowerGood " << ret << endl << "-------------"<< endl;

        cout << "Press Enter to \"Connected\"" << endl;
        cin.get();
        ret = true;
        ret = Connected(CardAddress);
        cout << "--------------  Connected " << ret << endl << "-------------"<< endl;

        cout << "Press Enter to \"CloseDevices\" and to \"FreeLibrary\"" << endl;
        cin.get();
        CloseDevices();
        FreeLibrary(hDLL);
}
return EXIT_SUCCESS;

}

int init()
{
hDLL = LoadLibrary(TEXT(“K8061.DLL”));
if (hDLL != NULL)
{
OpenDevice = (t_func5) GetProcAddress(hDLL, “OpenDevice”);
if (!OpenDevice)
{ // handle the error
FreeLibrary(hDLL);
return -2;
}
CloseDevices = (t_func0) GetProcAddress(hDLL, “CloseDevices”);
if (!CloseDevices)
{ // handle the error
FreeLibrary(hDLL);
return -3;
}
Connected = (t_func9) GetProcAddress(hDLL, “Connected”);
if (!Connected)
{ // handle the error
FreeLibrary(hDLL);
return -4;
}
PowerGood = (t_func9) GetProcAddress(hDLL, “PowerGood”);
if (!PowerGood)
{ // handle the error
FreeLibrary(hDLL);
return -5;
}
return 0; // OK
}
return -1; // error load DLL
}
[/code]

The error 5 seems to be a timeout error. The .EXE doesn’t wait for the delayed result from the disconnected card long enough.

Here is a link to download modified DLL version 3.0.0.3:
box.net/shared/n8xbsvghda

You’ll get now correct results to the Connected function.
The first call to the card after disconnecting it, gives anyhow also the error 5.

[quote=“VEL255”]The error 5 seems to be a timeout error. The .EXE doesn’t wait for the delayed result from the disconnected card long enough.

Here is a link to download modified DLL version 3.0.0.3:
box.net/shared/n8xbsvghda

You’ll get now correct results to the Connected function.
The first call to the card after disconnecting it, gives anyhow also the error 5.[/quote]

[color=#0000BF]I found a new problem.
When I call CloseDevice(0) (The device was successfully opened with OpenDevice) a SIGSEV signal is received.

Is this corrected in the new version?[/color]
I really was making a mistake, sorry :wink:

thanks by all the answers, I’ll try to check all this afternoon.

[quote=“pablodurcas”]Hi,
I Open correctly the card 0 wiht OpenDevice.

I disconnect the USB wire.
[/quote]

That is like disconnecting the red wire of a bomb, except that in this case it will nearly always blow up. Use the Windows API function RegisterDeviceNotification to find out when a device is being unplugged and disconnect the card gracefully before it happens so the kernel can free up its handles. Or simply do not disconnect the cable of a device that is in use.