troubleshooting Question

HowTo: LoadLibrary and GetProcAddress?

Avatar of newton-allan
newton-allan asked on
System Programming
16 Comments1 Solution27207 ViewsLast Modified:
I'm trying to use up to 3 dll's in an vc6 application. On the end-user's system, not all the dll's will necessarily be installed. I'm using LoadLibrary so the program can proceed as long as at least one .dll is loaded.

My problem is that the project(s) seems setup such that I get a runtime error message if all three .dll's aren't available. Also, the _ASSERTE statement below for GetProcAddress is failing.

Here is a "stub" of the vc6 main.cpp

#include "FirstDll.h"
#include "SecondDll.h"
#include "ThirdDll.h"

void main(void)
   const char* pBuf;
   HINSTANCE hLibrary = ::LoadLibrary("FirstDll.dll");
   if (hLibrary == NULL) {
      printf("Unable to load FirstDll\n");
   else {
      pBuf = FirstGetBuf(1);
      printf([%s]\n", pBuf);
   hLibrary = ::LoadLibrary("SecondDll.dll");
   if (hLibrary == NULL) {
      printf("Unable to load SecondDll\n");
   else {
      pBuf = SecondGetBuf(1);
      printf([%s]\n", pBuf);
   hLibrary = ::LoadLibrary("ThirdDll.dll");
   if (hLibrary == NULL) {
      printf("Unable to load ThirdDll\n");
   else {
      pBuf = ThirdGetBuf(1);
      printf([%s]\n", pBuf);
   typedef const char* const (CALLBACK* GET_PROC)(USHORT);
   GET_PROC pGetBuf;
   pGetBuf  = (GET_PROC)GetProcAddress(hLibrary, "ThirdGetBuf");
   _ASSERTE(pGetBuf != NULL);
   pBuf = pGetBuf((USHORT)1);

Note that the commented out statements don't work ... the _ASSERTE is raised because GetProcAddress isn't resolving the function FirstGetBuf.

Here is a stub of FirstDll.h as generated by the Vc6 wizard for Win32 Dynamic Link Library:
#define FIRSTINDLL_API __declspec(dllexport)
#define FIRSTINDLL_API __declspec(dllimport)

// Exported function.
FIRSTINDLL_API const char* const FirstGetBuf(USHORT Num);

If SecondDll.dll isn't installed at run-time, I get the message:

"This application has failed to start because SecondDll.dll was not found. Re-installing the application may fix this problem."

What am I doing wrong? Do I have the main.dsp project and/or *Dll.dsp projects incorrectly setup to insist that the *Dll.dll files exists? Do I need to have .def files? Does having the statement:
  pBuf = FirstGetBuf(1);
cause the linker to insist on having FirstDll.dll be present? How to I get the GetProcAddress call to work?

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 16 Comments.
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>


Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 16 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004