Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

Calling DLLs within C++

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
mgonyea
Asked:
mgonyea
  • 8
  • 5
  • 2
1 Solution
 
nietodCommented:
answer coming.
0
 
nietodCommented:
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
 
alexoCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nietodCommented:
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
 
alexoCommented:
Actually, the LIB can be created from the DLL but I don't remember the exact procedure.
0
 
mgonyeaAuthor 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)
0
 
nietodCommented:
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
 
nietodCommented:
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
 
mgonyeaAuthor 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 );

0
 
nietodCommented:
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
 
mgonyeaAuthor 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.
0
 
nietodCommented:
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
 
mgonyeaAuthor 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
0
 
nietodCommented:
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
 
mgonyeaAuthor 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 :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now