How do I link a DLL to a Win32 EXE for use?? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How do I link a DLL to a Win32 EXE for use??

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.



-Seph


Comments

  • Hello Speh..



    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:

    // mydll.h

    #if !defined( INC_MYDLL )

    #define INC_MYDLL



    #if defined( __cplusplus )

    extern "C" {

    #endif // __cplusplus



    // list the functions here

    void WINAPI myfunc( int x );



    #if defined( __cplusplus )

    }

    #endif // __cplusplus



    #endif // INC_MYDLL



    If you have a problem with any of the above, post again!



    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.



    To use the DLL above in run-time, you would do it like this:



    typedef void (*WINAPI pMYFUNC)( int );



    HINSTANCE hInst = LoadLibrary( "mydll.dll" );

    pMYFUNC pMyFunc = (pMYFUNC) GetProcAddress( hInst, "myfunc" );



    // and run it..

    pMyFunc(10);



    // You might want to do this before you are done

    FreeLibrary( hInst );



    I didnt understand what you mean by main menu and ESC...



    Hope this helped a bit


  • 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??



    -Seph






  • 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.



    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 );



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

    :

    : -Seph

    :

    :

    :






  • So it would be like this?



    //the dll itself

    int hitval(int level, int str, int agi, int opagi)

    {

    stuff here;

    return hit;

    }





    //the header

    *stuff you showed me here*

    int hitval(int level, int str, int agi, int opagi);





    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.



    -Seph


  • 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??



    -Seph


  • 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!



    OK, lets write this down.. I think you know how to create a Win32 DLL project, so here are the files to add.



    First of all here's the H file:

    //----------------------------------------------------------------------------

    // fight.h

    #if !defined( INC_FIGHT )

    #define INC_FIGHT



    #if defined( __cplusplus )

    extern "C" {

    #endif // __cplusplus



    int WINAPI hitval(int level, int str, int agi, int opagi);



    #if defined( __cplusplus )

    }

    #endif // __cplusplus



    #endif // INC_FIGHT

    //----------------------------------------------------------------------------



    And the CPP file

    //----------------------------------------------------------------------------

    // fight.cpp

    #include

    #include "fight.h"



    extern "C"

    int WINAPI hitval(int level, int str, int agi, int opagi)

    {

    // for a test, show values in a message box

    char buffer[ 80 ];

    wsprintf( buffer,

    "level = %d
    str = %d
    agi = %d
    opagi = %d",

    level, str, agi, opagi );

    MessageBox( NULL, buffer, "in dll", MB_OK );



    return 10;

    }

    //----------------------------------------------------------------------------



    And the DEF file

    ;----------------------------------------------------------------------------

    ; fight.def

    LIBRARY FIGHT.DLL ; should be the same name as the result DLL!

    DESCRIPTION 'This is the fight DLL!'

    HEAPSIZE 10000

    EXPORTS

    hitval @100 ; you can add more functions here: function name @101 and so on

    ;----------------------------------------------------------------------------



    Note that comments in DEF file are ; comment... much like ASM comments.



    That's it, compile/link the lot, you have a DLL and a LIB file. Now create a simple test EXE something like this

    // ----------------------------------------------------------------------------

    // test.cpp

    #include

    #include "fight.h" // make sure this points to the correct path



    int WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )

    {

    int ret = hitval( 1, 2, 3, 4 );

    char buffer[ 80 ];

    wsprintf( buffer, "returned %d", ret );

    MessageBox( NULL, buffer, "in exe", MB_OK );

    return 0;

    }

    //----------------------------------------------------------------------------



    Add fight.lib to this project, make sure fight.dll is somewhere along the path, run.. It should work fine.



    Hope this helps



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

    :

    : -Seph

    :






  • No text... No text... No text... No text... No text... No text...


Sign In or Register to comment.