tags:

views:

64

answers:

4

Hi, why did it fail to load the library at link at compilation time? i don't care about freeing the library yet it just won't work.

#include <windows.h>

    int main()
    {
        LoadLibrary("winmm.lib");
        timeGetTime();
    }
+2  A: 

.lib is not a dynamically linked library (DLL), and cannot be loaded at runtime. You need to load the .dll, or link the .lib at link time (at which point you don't use LoadLibrary).

Yann Ramin
i tried 'LoadLibrary("winmm.dll");' but it wouldn't work.
Carl_1789
+2  A: 

From your comment above, it's clear that the problem is that timeGetTime() requrires the winmm module at compile time, which means you have to link with winmm.lib. You cannot call the function directly by its name if you want to use run-time linking; you have to get its function pointer out of the DLL.

If you truly want to load the DLL at run time, you must use GetProcAddress. A full set of example code for using LoadLibrary properly is found on this MSDN page.

Victor Liu
+3  A: 

Try this code. It should solve your problem.

#include <windows.h>

#pragma comment(lib, "winmm.lib")

int main()
{
    DWORD time = timeGetTime();
}
Jujjuru
Although it's not "Portable", I like the idea to keep link info and compile info closely together.
xtofl
+1  A: 

You are trying to load a .lib file (linker library information) using the LoadLibrary function, which is designed to load dynamic-link libraries - that is plain wrong. .lib files are linked in the executable at link time, whereas the .dll files are loaded at runtime, either via explicit loading using LoadLibrary or by feeding the linker a .lib file that references a .dll file.

  • If you want to load a static library you need to tell the linker to include it - consult your compiler's documentation about this.
  • To load a dynamic library using a .lib file, you need to do the same as for a static library and put the dynamic library in the global PATH or in the same directory as the executable.
  • To load a dynamic library at runtime you need to call LoadLibrary to get a handle to it and pass that to GetProcAddress to get pointers to the functions you are interested in. Wikipedia has a small example on how to do this.
iconiK