Hello, I have a few K8061 boards and have written several programs in VB using the supplied dll files, no problems what so ever, works perfect!
But now I am trying to write an application in C++ with Eclipse CDT as environment using a mingw compiler…
I still use the K8061.dll and my test code looks like this:
#include <iostream>
#include <windows.h>
using namespace std;
#define NUMBER_OF_RUNS 1
#define LEDS_PER_RUN 3
HINSTANCE dll;
int cardAddress;
typedef int (*OpenDevice)();
typedef void (*CloseDevices)();
typedef void (*SetDigitalChannel)(int, int);
typedef void (*ClearAllDigital)(int);
OpenDevice openDevice;
CloseDevices closeDevices;
SetDigitalChannel setDigitalChannel;
ClearAllDigital clearAllDigital;
void loadDll()
{
dll = LoadLibrary("K8061.dll");
if(!dll)
cardAddress = -3;
else
{
openDevice = (OpenDevice) GetProcAddress(dll, "OpenDevice");
closeDevices = (CloseDevices) GetProcAddress(dll, "CloseDevices");
setDigitalChannel = (SetDigitalChannel) GetProcAddress(dll, "SetDigitalChannel");
clearAllDigital = (ClearAllDigital) GetProcAddress(dll, "ClearAllDigital");
cardAddress = openDevice();
}
}
void loopLeds()
{
for(int i=0 ; i<NUMBER_OF_RUNS ; i++)
{
for(int j=0 ; j<LEDS_PER_RUN ; j++)
{
setDigitalChannel(cardAddress, j + 1);
Sleep(200);
}
Sleep(1000);
clearAllDigital(cardAddress);
}
}
void closeDll()
{
closeDevices();
FreeLibrary(dll);
}
int main()
{
loadDll();
switch(cardAddress)
{
case -3:
cout << endl << "DLL failed to load." << endl;
break;
case -2:
cout << endl << "No card found." << endl;
break;
case -1:
cout << endl << "All cards opened." << endl;
break;
default:
cout << endl << "Card " << cardAddress << " connected." << endl;
loopLeds();
break;
}
closeDll();
return 0;
}
When running this as it is it seems to work, I get console output “Card 0 connected.” and digital outputs 1-3 lights up in order.
When changing define NUMBER_OF_RUNS from 1 to 2, it first seem to work, the console output is still “Card 0 connected.” and the leds light up as before but twice as intended, but this time i also get an access violation error (code 0xc000000005) and application terminates.
And when changing define NUMBER_OF_RUNS to 3 and LEDS_PER_RUN to 8 which should make all digital output leds light up from 1 to 8 three times in a row, the first two runs work OK, but on the third just the first 5 leds light up and then the application justs abort, this time no error message…
Since I have never really programmed in an unmanaged programming language before maybe my program somehow crashes because of bad memory management or maybe the dll is not suited for c++ programming?
Would be really thankful if someone could help me get this board running without problems in c++!
EDIT: I use the dll from the “vista pack” but have also tried original one and mpusbapi.dll.
Regards,
/Simon

