Solved

Automatic loading of a DLL using VC++ 5.0

Posted on 1998-09-08
6
222 Views
Last Modified: 2010-04-02
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
Comment
Question by:tvandegr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 150 total points
ID: 1172281
You need to use LoadLibrary() and FreeLibrary.  Details follow.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1172282
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
 
LVL 2

Expert Comment

by:milenvk
ID: 1172283
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

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

Author Comment

by:tvandegr
ID: 1172285
nietod

Sorry I thought I graded the question they 8th.  I guess not
0
 
LVL 22

Expert Comment

by:nietod
ID: 1172286
That's fine.  Good to know its solved.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question