Solved

Automatic loading of a DLL using VC++ 5.0

Posted on 1998-09-08
6
190 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
  • 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now