简体   繁体   中英

Use DLL in C without lib

How can I use the functions in a DLL in C without a LIB file to go with it? I know all of the function prototypes and their names.

Yes you can. You should use the GetProcAddress function, to call the function directly in the DLL, without involving the LIB

Processes explicitly linking to a DLL call GetProcAddress to obtain the address of an exported function in the DLL. You use the returned function pointer to call the DLL function.

To quote the Example from the above link:

typedef UINT (CALLBACK* LPFNDLLFUNC1)(DWORD,UINT);
...

HINSTANCE hDLL;               // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1;    // Function pointer
DWORD dwParam1;
UINT  uParam2, uReturnVal;

hDLL = LoadLibrary("MyDLL");
if (hDLL != NULL)
{
   lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,
                                           "DLLFunc1");
   if (!lpfnDllFunc1)
   {
      // handle the error
      FreeLibrary(hDLL);
      return SOME_ERROR_CODE;
   }
   else
   {
      // call the function
      uReturnVal = lpfnDllFunc1(dwParam1, uParam2);
   }
}

You can use LoadLibrary() and GetProcAddress() as described in the answer by DarkXphenomenon. Or, another alternative is to create your own import library for the DLL by creating a.def file then running that through the LIB command to generate an import library. Additional details here:

http://support.microsoft.com/kb/131313

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM