LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to use a DLL function in a file which is not the "LoadLibrary" File

I 've got one mainfile.c file to initialise my pci device, in this file i use "LoadLibrary" function and all the things for the device to work.
The DLL is given with the pci card.

I can use the DLL's functions correctly in my mainfile.c, but i want to use its in another .c file of my project.
Even if I include the necessary files for the DLL in the source, I can't use the functions.

How can I do that?
0 Kudos
Message 1 of 6
(3,395 Views)
Is the handle returned from LoadLibrary a global? Have you verified that the handle is valid in your other .c files? Are you using GetProcAddress?

You can avoid using LoadLibrary and GetProcAddress if you statically link your DLL (add the DLL's .LIB file to your project and #include the .h file for the exported functions). If you #include the .h file for the exported functions in all your .c files, you can call a DLL function directly from any .c file.

The benefit of using LoadLibrary and FreeLibrary is conserving memory. You can LoadLibrary when you need the functions, then FreeLibrary when you don't. But if you're not using a bunch of different DLL's and you want the functions available all the time, static linking can be a lot easier.

Look at the sample programs that ship with CVI. They don't use LoadLibrary.
...\CVI\samples\dll\simple\cvi\readme.txt
...\CVI\samples\dll\simple\cvi\mydll.prj
...\CVI\samples\dll\simple\cvi\simple.prj
Message 2 of 6
(3,386 Views)
your description of your problem could use some better explanation but my guess is that you are declaring your function pointers like this in a header somewhere

void (__cdecl* foo)(int param) ;

and in the c file that you call GetProcAddress the functions work fine but in another c file the functions crash. If this is right declare the functions pointers as static so it would be:

static void (__cdecl* foo)(int param)
0 Kudos
Message 3 of 6
(3,382 Views)
also make sure that you are using the right calling convention __cdecl or __stdcall for your function pointers. The easy way to tell is if you look at your dll in the dependacy walker that comes with VS __stdcall function names will be like foo@4, but this does not sound like your problem because using the wrong calling convention usually causes a stack error.
0 Kudos
Message 4 of 6
(3,380 Views)
Hello,
I 've followed your advice to link statically the DLL and it work good.
Thank you
0 Kudos
Message 5 of 6
(3,366 Views)
I 've tried to use function pointer's before to ask on the forum, but it's seem to be not easy in CVI
thanks
0 Kudos
Message 6 of 6
(3,364 Views)