Load DLL Library

2019-07-15 10:14发布

问题:

Is it possible to load a DLL with C and use its functions?

I am new in C, and I am trying to search some good references on the internet for this; but I can't find any.

Any help would be appreciated!

I am using GNU GCC in Code::Blocks on Windows 7, 64 Bit.

回答1:

HMODULE hModule = LoadLibrary(<dll file name>) followed by GetProcAddress(hModule, <function name>) will do this job using the WinAPI.

An example could be found here.



回答2:

I think you should investigate the LoadLibrary function.

http://msdn.microsoft.com/en-us/library/ms684175.aspx

Loads the specified module into the address space of the calling process. The specified module may cause other modules to be loaded.



回答3:

Building a DLL using MinGW, here are some instructions:

First, you need to mark your functions for export, so they can be used by callers of the DLL. To do this, modify them so they look like (for example)

__declspec( dllexport ) int add2(int num){
   return num + 2;
}

then, assuming your functions are in a file called funcs.c, you can compile them:

gcc -shared -o mylib.dll funcs.c The -shared flag tells gcc to create a DLL.

For a free IDE which will automate all the flags etc. needed to build DLLs, take a look at the excellent Code::Blocks, which works very well with MinGW.

Also, see the article Creating a MinGW DLL for Use with Visual Basic on the MinGW Wiki.