Solved

Calling DLLs within C++

Posted on 1998-06-12
15
190 Views
Last Modified: 2010-04-01
I'm using mike's account (he gave me permission)  so please bear with me :)

I'm using Visual C++ version 1.52.  I would like to call a function that's in a DLL file and would like to know the syntax to do so :)  For some reason I have 'extern "C" ' stuck in my head.  Please let me know how to call a DLL function from my program and/or the steps necessary to do this. :)

Thanks :)

John K.
0
Comment
Question by:mgonyea
  • 8
  • 5
  • 2
15 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
Comment Utility
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I assume this is not for a DLL that you've written.  right?  If you wrote it ignore this and let me know.
I also assume you don't have the .lib file for the DLL.  If you do, let me know.

You need to use LoadLibrary() to get a handle to the DLL.  
Then you need to use GetProcAddress() to get a pointer to the procedure in the DLL.  
Then you call the procedure through the pointer.
Finally you unload the library with FreeLibrary().

It might look like this

typedef int (*PrcPtrTyp)(int);
HINSTANCE LibHnd = LoadLibrary("ADLL.DLL");
PrcPtrTyp PrcPtr = (PrcPtrTyp)GetProcAddress(LibHnd,"FunctionName");
int Result = PrcPtr(5);
FreeLibrary(LibHnd);

That's about the best info I can give with the information you've provided.  If you need more you'll have to be more specific.

0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
If the DLL was compiled with a C compiler, all it's functions are extern "C".
If you link statically to the DLL (much simpler than using LoadLibrary), you just have to declare the function as extern "C" and call it normally.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Alexo's answer assumes you have the .lib file for the DLL so you can link to the DLL.  That's much easier than what I proposed (that's why I asked that in my answer.)
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
Actually, the LIB can be created from the DLL but I don't remember the exact procedure.
0
 
LVL 1

Author Comment

by:mgonyea
Comment Utility
I have declared it before like this:

extern "C" ExitWindows(<paramlist goes here>)

and it returns this error when compiling:

Initializing...
Compiling...
d:\examp\small.c
d:\examp\ash.h(38) : error C2059: syntax error : 'string'
d:\examp\small.c(41) : warning C4013: 'ExitWindows' undefined; assuming extern returning int
 CL returned error code 2.
SMALL.C - 1 error(s), 1 warning(s)
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
First of all ExitWindows() and a windows API procedure.  Are you trying to call that procedure? ( Windows' procedure)  Or are you trying to call a procedure in a DLL that you (or someone else) wrote.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:nietod
Comment Utility
That's supposed to be "ExitWindows IS  a windows API procedure."

What is on line 38 of ash.h?

When you wrote "<paramlist goes here>"  you actually have a proper C++ parameter list in the file right?  That's not what you have in the file its it?

0
 
LVL 1

Author Comment

by:mgonyea
Comment Utility
"First of all ExitWindows() is a windows API procedure."
Yes, I am trying to call a Windows NT API function

When you wrote "<paramlist goes here>"  you actually have a proper C++ parameter list in the file right?  That's not what you have in the file its it?

lol!  Of course not!  Instead of typing all the DWORD declarations I made, I just specified the parameter list like that :)

Line 38 of ash.h contains this:

extern "C" ExitWindows ( DWORD, DWORD );

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
You don't need to declare the windows API procedures.  Just include windows.h to get the windows procedures  You also might want to define WIN32_LEAN_AND_MEAN to make it include only the most essential parts of the windows aPI (This will compile faster).  like

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

Then you don't want to define ExitWindows(), or any other Windows procedures as extern.  

You will need to link to  the windows .lib files.  These should be kernal.lib, gdi.lib, and user.lib.  However, this is usually taken care of automatically in a windows application.
0
 
LVL 1

Author Comment

by:mgonyea
Comment Utility
Remember, this is Visual C++ v1.52 and I think it was designed for Win 3.1.  Does that matter?  I didn't think you could easily call a 32-bit DLL from a 16-bit program.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
windows.h includes both 16 bit and 32 bit includes.  I suspect even for v1.52.  Windows 3.1 supported 32 bit programs--not well, but it did.

You aren't going to call a 32 bit DLL.  windows has 16 bit version of the system DLLs.  Those will be called.  (And they, with difficulty, call into the 32 bit, or vice versa)

I assume this is for an ordinary 16 bit windows program.   Right?
0
 
LVL 1

Author Comment

by:mgonyea
Comment Utility
Actually, the target platform is WinNT.  Developing this in C++ v1.52 was my only choice at this time, considering MS shipping takes 4-6 weeks at best
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
You are missing the point.  If this is an ordinary windows program, there isn't really anything special to do.  You just include <windows.h> and then call the windows functions you want.  You must link with the right .lib files, but VC 4 and 5 take care of that atuomatically, so I assume that 1.5 does as well.
0
 
LVL 1

Author Comment

by:mgonyea
Comment Utility
I've completed the project.  The problem was we needed a 32-bit compiler in order to reach and use some of the 32-bit DLLs within our project.  Thanks again to everyone :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 learn how to clear a vector as well as how to detect empty vectors in C++.

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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now