We help IT Professionals succeed at work.

Calling DLLs within C++

mgonyea
mgonyea asked
on
213 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.
Comment
Watch Question

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
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.

Commented:
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.

Commented:
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.)

Commented:
Actually, the LIB can be created from the DLL but I don't remember the exact procedure.

Author

Commented:
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)

Commented:
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.

Commented:
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?

Author

Commented:
"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 );

Commented:
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.

Author

Commented:
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.

Commented:
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?

Author

Commented:
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

Commented:
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.

Author

Commented:
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 :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.