Windows programming

Moderators: None (Apply to moderate this forum)
Number of threads: 3711
Number of posts: 9173

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Edit Report
How do I link a DLL to a Win32 EXE for use?? Posted by Sephiroth on 28 Oct 2000 at 4:55 PM
I'm trying to get my executeable file to load a few DLLs I have made and compiled so that I don't have to put a ton of functions into the actual executeable, but I have NO experience with DLLs. Can somebody help me figure out how to use both load-time linking (Borland says I have to link with the .LIB for this one?) and run-time linking (for stuff such as the main menu popping up when you press ESC). I would REALLY appreciate any help that anybody can give me. Thanks for your time.<br>
<br>
-Seph


Edit Report
Re: How do I link a DLL to a Win32 EXE for use?? Posted by KMS on 30 Oct 2000 at 9:53 AM
Hello Speh..<br>
<br>
For linking in compile time, you only need to add the lib file to your project, then #include the header file and off you go. For example, you have a void myfunc( int x ) in the DLL, your include file will look like this:<br>
// mydll.h<br>
#if !defined( INC_MYDLL )<br>
#define INC_MYDLL<br>
<br>
#if defined( __cplusplus )<br>
extern "C" {<br>
#endif // __cplusplus<br>
<br>
// list the functions here<br>
void WINAPI myfunc( int x );<br>
<br>
#if defined( __cplusplus )<br>
}<br>
#endif // __cplusplus<br>
<br>
#endif // INC_MYDLL<br>
<br>
If you have a problem with any of the above, post again!<br>
<br>
If you want to use a DLL created by someone else, then dont forget to IMPLIB it beforehand. BCB and VC++ both use different format for thier import libraries.<br>
<br>
To use the DLL above in run-time, you would do it like this:<br>
<br>
typedef void (*WINAPI pMYFUNC)( int );<br>
<br>
HINSTANCE hInst = LoadLibrary( "mydll.dll" );<br>
pMYFUNC pMyFunc = (pMYFUNC) GetProcAddress( hInst, "myfunc" );<br>
<br>
// and run it..<br>
pMyFunc(10);<br>
<br>
// You might want to do this before you are done<br>
FreeLibrary( hInst );<br>
<br>
I didnt understand what you mean by main menu and ESC...<br>
<br>
Hope this helped a bit


Edit Report
Re: Re: How do I link a DLL to a Win32 EXE for use?? Posted by Sephiroth on 30 Oct 2000 at 5:07 PM
OK, I didn't make a header file for the dll before. All I did was make a file with the functions in it, and then compiled to a dll, with no errors or warnings. So what would this header file be used for? Just linking purposes??<br>
<br>
-Seph<br>
<br>



Edit Report
Re: Re: Re: How do I link a DLL to a Win32 EXE for use?? Posted by KMS on 31 Oct 2000 at 2:47 PM
That header file is the interface to your DLL. Remember, a DLL is a library, and a library is all about packaging some piece of code from the user with only an interface exposed.<br>
<br>
For example, GDI.EXE (its a DLL renamed EXE really) is the DLL, windows.h is the interface that has the prototypes of the functions inside that DLL, like int WINAPI SetPixel( HDC hDC, int x, int y );<br>
<br>
: OK, I didn't make a header file for the dll before. All I did was make a file with the functions in it, and then compiled to a dll, with no errors or warnings. So what would this header file be used for? Just linking purposes??<br>
: <br>
: -Seph<br>
: <br>
: <br>
: <br>
<br>



Edit Report
Re: Re: Re: Re: How do I link a DLL to a Win32 EXE for use?? Posted by Sephiroth on 31 Oct 2000 at 3:55 PM
So it would be like this?<br>
<br>
//the dll itself<br>
int hitval(int level, int str, int agi, int opagi)<br>
{<br>
stuff here;<br>
return hit;<br>
}<br>
<br>
<br>
//the header<br>
*stuff you showed me here*<br>
int hitval(int level, int str, int agi, int opagi);<br>
<br>
<br>
I'm going to try it real fast and see what happens. This "fight.dll" is very important to the program though, considering it has all battle info in it.<br>
<br>
-Seph


Edit Report
Re: Re: Re: Re: How do I link a DLL to a Win32 EXE for use?? Posted by Sephiroth on 31 Oct 2000 at 4:08 PM
OK, just got done trying it. I pasted what you posted into the header file, and changed it from INC_MYDLL to INC_FIGHT (fight.dll/fight.cpp) and the void function I replaced with "int hitval(int level, int str, int agi, int opagi);". I am still getting "unresolved external referenced from AoY.OBJ". I have added the .lib file to the project, to be linked with the executeable. Any ideas??<br>
<br>
-Seph


Edit Report
Re: Re: Re: Re: Re: How do I link a DLL to a Win32 EXE for use?? Posted by KMS on 1 Nov 2000 at 7:51 AM
You still need to tell the compiler which functions to export from the DLL. This can be done in 2 ways, the __dllimport/__dllexport or through a DEF file, I use DEF files always because it lets me define the ordinal numbers of those functions myself, meaning, if I update the DLL by adding new functions, the EXE's need not to be recompiled. A good practice!<br>
<br>
OK, lets write this down.. I think you know how to create a Win32 DLL project, so here are the files to add.<br>
<br>
First of all here's the H file:<br>
//----------------------------------------------------------------------------<br>
// fight.h<br>
#if !defined( INC_FIGHT )<br>
#define INC_FIGHT<br>
<br>
#if defined( __cplusplus )<br>
extern "C" {<br>
#endif // __cplusplus<br>
<br>
int WINAPI hitval(int level, int str, int agi, int opagi);<br>
<br>
#if defined( __cplusplus )<br>
}<br>
#endif // __cplusplus<br>
<br>
#endif // INC_FIGHT<br>
//----------------------------------------------------------------------------<br>
<br>
And the CPP file<br>
//----------------------------------------------------------------------------<br>
// fight.cpp<br>
#include <windows.h><br>
#include "fight.h"<br>
<br>
extern "C"<br>
int WINAPI hitval(int level, int str, int agi, int opagi)<br>
{<br>
// for a test, show values in a message box<br>
char buffer[ 80 ];<br>
wsprintf( buffer,<br>
"level = %d\nstr = %d\nagi = %d\nopagi = %d",<br>
level, str, agi, opagi );<br>
MessageBox( NULL, buffer, "in dll", MB_OK );<br>
<br>
return 10;<br>
}<br>
//----------------------------------------------------------------------------<br>
<br>
And the DEF file<br>
;----------------------------------------------------------------------------<br>
; fight.def<br>
LIBRARY FIGHT.DLL ; should be the same name as the result DLL!<br>
DESCRIPTION 'This is the fight DLL!'<br>
HEAPSIZE 10000<br>
EXPORTS<br>
hitval @100 ; you can add more functions here: function name @101 and so on<br>
;----------------------------------------------------------------------------<br>
<br>
Note that comments in DEF file are ; comment... much like ASM comments.<br>
<br>
That's it, compile/link the lot, you have a DLL and a LIB file. Now create a simple test EXE something like this<br>
// ----------------------------------------------------------------------------<br>
// test.cpp<br>
#include <windows.h><br>
#include "fight.h" // make sure this points to the correct path<br>
<br>
int WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )<br>
{<br>
int ret = hitval( 1, 2, 3, 4 );<br>
char buffer[ 80 ];<br>
wsprintf( buffer, "returned %d", ret );<br>
MessageBox( NULL, buffer, "in exe", MB_OK );<br>
return 0;<br>
}<br>
//----------------------------------------------------------------------------<br>
<br>
Add fight.lib to this project, make sure fight.dll is somewhere along the path, run.. It should work fine.<br>
<br>
Hope this helps<br>
<br>
: OK, just got done trying it. I pasted what you posted into the header file, and changed it from INC_MYDLL to INC_FIGHT (fight.dll/fight.cpp) and the void function I replaced with "int hitval(int level, int str, int agi, int opagi);". I am still getting "unresolved external referenced from AoY.OBJ". I have added the .lib file to the project, to be linked with the executeable. Any ideas??<br>
: <br>
: -Seph<br>
: <br>
<br>



Edit Report
The missing include file is windows.h (NT) Posted by KMS on 1 Nov 2000 at 7:54 AM
No text... No text... No text... No text... No text... No text...





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.