Solved

Automatic loading of a DLL using VC++ 5.0

Posted on 1998-09-08
6
207 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

772 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