Indeed, global variables are the way to go. Don’t call LoadLibrary/FreeLibrary more than necessary (1 time) for each process (process!, not card). I’d also load every function with GetProcAddress at the time you load the library, which will once more save you from doing things more than once.
Just to clarify:
wrapper.h
#ifndef _WRAPPER_H_
#define _WRAPPER_H_
typedef VOID (WINAPI*LPFNSETCHANNELCOUNT)(DWORD);
extern HANDLE hK8062lib;
extern LPFNSETCHANNELCOUNT SetChannelCount;
// ...
BOOL K8062_Load();
BOOL K8062_Unload();
#endif /* _WRAPPER_H_ */
wrapper.c
#include "wrapper.h"
HMODULE hK8062lib = NULL;
LPFNSETCHANNELCOUNT SetChannelCount;
// ...
BOOL K8062_Load()
{
// ... LoadLibrary ...
// ...
// ... GetProcAddress for all functions into their global variables ...
// ... For example:
SetChannelCount = (LPFNSETCHANNELCOUNT)GetProcAddress(hK8062lib, _T("GetChannelCount"));
}
BOOL K8062_Unload()
{
// ... FreeLibrary ...
}
The extern keyword is absolutely needed here, in case two other files include this header. Also, do not forget to put include guards in all your header files.
application.c
#include "wrapper.h"
int main() // or whatever function you like
{
K8062_Load(); // once when your application starts
// because i was clever in naming my variables i can now use the K8062 functions
// as if they had been declared statically
SetChannelCount(5); // whoop!
K8062_Unload(); // once when the application exits
}
If you’ve ever programmed winsock, this would be similar in usage as having to call WSAStartup/WSACleanup before and after using any winsock functions.
About memory usage, if I remember correctly, any and all memory is released when a process dies. So there is never a possibility of a memory leak after a process terminates, only for running processes. This doesn’t mean that you shouldn’t use FreeLibrary though! But yes, Windows does clean up after a process.
A good classic book on advanced topics like memory management is ‘Advanced Windows’ by Jeffrey Richter. It’s basically the next book you should read after ‘Programming Windows’.
Always glad to help