Wednesday, November 27, 2013

CPP Series: Load Dynamic-link Library at runtime

I had trouble with this particular software which I couldn't call the .dll using windows 7. I gave up on using .lib method because a lot of error came out and I couldn't solve the link and conversion errors.

Loading the library at runtime require extra efforts but not so much once you get a hold of it.

Let say we have a .dll file which contain listed functions below:

short TUSBSLC_API Tusbs01lc_Device_Open( short id );
void TUSBSLC_API Tusbs01lc_Device_Close( short id );
short TUSBSLC_API Tusbs01lc_Single_Sample( short id ,double *Data);
short TUSBSLC_API Tusbs01lc_Start_Sample( short id ,short SampleRate);
short TUSBSLC_API Tusbs01lc_Get_Datas( short id ,double *Data,unsigned char *SerNum,unsigned char *Size);
short TUSBSLC_API Tusbs01lc_Zero_Adj( short id, double Data );


Firstly we need to declare in your header file (.h) these definitions:

typedef short (*DEVICE_OPEN)(short);
DEVICE_OPEN Tusbs01lc_Device_Open;

typedef void (*DEVICE_CLOSE)(short);
DEVICE_CLOSE Tusbs01lc_Device_Close;

typedef short (*SINGLE_SAMPLE)(short, double*);
SINGLE_SAMPLE Tusbs01lc_Single_Sample;

typedef short (*START_SAMPLE)(short, short);
START_SAMPLE Tusbs01lc_Start_Sample;

typedef short (*GET_DATAS)(short, double*, unsigned char*, unsigned char*);
GET_DATAS Tusbs01lc_Get_Datas;

typedef short (*ZERO_ADJ)(short, double);
ZERO_ADJ Tusbs01lc_Zero_Adj;


You could give any name to DEVICE_OPEN, DEVICE_CLOSE and so on. The colors will give you hints on how to create your own definition. 

Next, just call the function!!

HINSTANCE dllhandle = LoadLibrary("path\\TUSBSLC.dll");
if(dllhandle != NULL) {
        Tusbs01lc_Device_Open = (DEVICE_OPEN)GetProcAddress(dllhandle, "Tusbs01lc_Device_Open");
        Tusbs01lc_Device_Close = (DEVICE_CLOSE)GetProcAddress(dllhandle, "Tusbs01lc_Device_Close");
        Tusbs01lc_Single_Sample = (SINGLE_SAMPLE)GetProcAddress(dllhandle, "Tusbs01lc_Single_Sample");
        Tusbs01lc_Start_Sample = (START_SAMPLE)GetProcAddress(dllhandle, "Tusbs01lc_Start_Sample");
        Tusbs01lc_Get_Datas = (GET_DATAS)GetProcAddress(dllhandle, "Tusbs01lc_Get_Datas");
        Tusbs01lc_Zero_Adj = (ZERO_ADJ)GetProcAddress(dllhandle, "Tusbs01lc_Zero_Adj");
}
    else {
        ShowMessage("Unable to load the DLL");


Finally, you have to release/free the memory at the end of your program.

if(dllhandle) FreeLibrary(dllhandle);

Good luck!!

Friday, November 22, 2013

LaTeX Series: Thesis Template

I found a good website on thesis template. Feel free to go through the site.

How to LaTeX:
Figure

Tuesday, November 12, 2013

CPP Series: Pointer to Pointer

Everytime..... this pointer fella give me headache... fuhhh...

here the reference for future headache [link].

This function modify the variable piStuff inside the function putStuff.

Example pointer to double pointer:

int **piStuff;
putStuff(&piStuff);

void putStuff(int ***ppiStuff)
{
    *ppiStuff = new int* [SIZE_A];
     for(int i=0; i<SIZE_A;i++) {
          (*ppiStuff)[i] = new int [SIZE_B];
     }

     for(int i=0; i
<SIZE_B;i++) {          
          for(int j=0; j<SIZE_B;j++) {
              (*ppiStuff)[i][j] = iSomeVar;
          }
     }
}


Example pointer to pointer:

int *piStuff;
putStuff(&piStuff);

void putStuff(int **ppiStuff)
{
    *ppiStuff = new int [SIZE];

     for(int j=0; j
<SIZE;j++) {         
         (*ppiStuff)[j] = iSomeVar;
     }    
}