Solved

Automatic loading of a DLL using VC++ 5.0

Posted on 1998-09-08
6
215 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

828 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