Solved

Calling DLLs within C++

Posted on 1998-06-12
15
193 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
ID: 1165755
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165756
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
ID: 1165757
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 22

Expert Comment

by:nietod
ID: 1165758
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
ID: 1165759
Actually, the LIB can be created from the DLL but I don't remember the exact procedure.
0
 
LVL 1

Author Comment

by:mgonyea
ID: 1165760
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
ID: 1165761
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
 
LVL 22

Expert Comment

by:nietod
ID: 1165762
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
ID: 1165763
"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
ID: 1165764
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
ID: 1165765
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
ID: 1165766
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
ID: 1165767
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
ID: 1165768
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
ID: 1165769
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
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 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.

770 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