[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Automatic loading of a DLL using VC++ 5.0

I am writing an application that uses the rasapi32 dll to dial an ISP if the computer is not on a LAN. I use a registry to determine if it is on a LAN.  If the computer is on a LAN I do not need the dial up stuff.  But since I might need the rasapi dll, it has to be installed for my program to run.  That involves installing dial-up networking on a computer that is attached to a LAN and has no modem.  What I would like to do is to see if the computer is on a LAN to determine if I need to load the dll.

I know there are some sort of LoadDll() and UnloadDLL() function calls someplace.  I am using VC++ 5.0 and I do not know how to keep it from loading the dll.  I also don't know the proper way to manually load dlls.  This program is being developed under NT and will be used on both NT and 95 computers.  

Thanks.

Thanks in advance.
0
tvandegr
Asked:
tvandegr
  • 4
1 Solution
 
nietodCommented:
You need to use LoadLibrary() and FreeLibrary.  Details follow.
0
 
nietodCommented:
You use LoadLibrary() to load a dll with a specified file name and (optionally) path.  This returns an instance handle that you save so you can use it later to refer to the library.  When you are done with the library you call FreeLibrary() to unload the library and specify the instance handle returned from LoadLibrary().  

The bad part about this, is that you cannot directly call functions in the library as you can when it is explicitly linked to the library.  Instead you need get a pointer to the library procedure and then call the function using the pointer.  You use the GetProcAddress() function to get a pointer to a procedure inside a DLL.

Let me know if you have questions.
0
 
milenvkCommented:
To prevent VC linking your program to the DLL automatically go to the project settings, the link tab and clear the library. Then at the point of your code where you decide to load the DLL do the following:

void (*pFoo)(int i);

HINSTANCE hInst = LoadLibrary(lpszLibPath);
if(hInst !=  NULL)
{
  pFoo = GetProcAddress(hInst, "Foo");
  if(pFoo)
    pFoo(5);
}
FreeLibrary(hInst);

In the example above I assume that the lpszLibPath variable is of type string and contains the path to the DLL of interest. Then pFoo is a pointer to a function of the same type as the function you want to call from the DLL. And finally in the example I assume the function is called "Foo". Use the exact name of the function (case sensitive) that you want to call. And then the example calls the funciton with a parameter of 5. Then you can free up the library.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nietodCommented:
tvanddegr, has this answered your question?  If so, please grade it.  If not, let me know and we can work on it.
0
 
tvandegrAuthor Commented:
nietod

Sorry I thought I graded the question they 8th.  I guess not
0
 
nietodCommented:
That's fine.  Good to know its solved.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now