CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Aug 1999
    Location
    Chile
    Posts
    177

    How to import a function from a DLL

    That's it...

    Jaime


  2. #2
    Join Date
    Apr 1999
    Location
    Toronto, ON
    Posts
    713

    Re: How to import a function from a DLL

    I guess, you are speaking about export:
    in your "def" file:
    //example

    EXPORTS
    ; Explicit exports can go here
    DllGetClassObject PRIVATE
    DllRegisterServer PRIVATE


    Dmitriy, MCSE

  3. #3
    Join Date
    Aug 1999
    Location
    Chile
    Posts
    177

    Re: How to import a function from a DLL

    No! if the subject of the question has the word "import" is about that what I was asking for. I am NOT doing a DLL so I DON'T want to make any exports. I just want to USE the functions from a DLL, so that I need to IMPORT them. In BC++ I did something like INT WINAPI _import FS_GetAuxCommMsg( long UnitNo, char *Buffer, long Length ); where GS_GetAux is a function from a DLL. I have to import it so that I am able to use it in my program and the linker can link by using the LIB file which has been added to the project.

    So... my questions can be enumerated as follows:

    1. How to declare the IMPORT in the Header file. Maybe something like _declspec(dllimport).
    2. How to include the DLL file in my project. In BC++ I had to create a LIB file from the DLL and then add it to the project, so that the Linker can link the functions properly. How can I do it in VC++ 6.0

    Now... that's it
    thanks in advance
    Jaime



  4. #4
    Join Date
    Aug 1999
    Location
    CA
    Posts
    23

    Re: How to import a function from a DLL

    Jaime, maybe you were not on the same wavelength as Dmitriy:
    You have to export your functions & classes for them to be imported elsewhere.
    Here is my latest DLL exporting CHook (this is "Hook.h":


    #include "SetWindowsHookExDLLDeclSpec.h"
    // START EXPAND
    #ifndef _SET_WINDOWS_HOOK_EX_DLL_DECL_SPEC__H_
    #define _SET_WINDOWS_HOOK_EX_DLL_DECL_SPEC__H_

    #ifndef AFX_DLL
    #define SET_WINDOWS_HOOK_EX_DLL_DECL_SPEC __declspec( dllexport )
    #else
    #define SET_WINDOWS_HOOK_EX_DLL_DECL_SPEC __declspec( dllimport )
    #endif

    #endif // _SET_WINDOWS_HOOK_EX_DLL_DECL_SPEC__H_
    // END EXPAND


    /////////////////////////////////////////////////////////////////////////////
    // CHook window

    class SET_WINDOWS_HOOK_EX_DLL_DECL_SPEC CHook
    {
    // whatever....
    };





    When the above is compiled in the DLL project, AFX_DLL is defined, and the class is exported. When the above is included in the EXE, it is imported, so all you have to do is tell your linker to import the library (through project settings), or in code (#pragma comment lib libname). Note that if you are importing the above in another DLL, you are going to have to change the decl spec define name to something more specific, or it will not be imported by the new dll because most likely the new dll has AFX_DLL defined (was that clear???).

    An other approach is for you to do LoadLibrary, then GetProcAddress, but then you end up with ugly (in my opinion) typedefs for function pointer types, such as


    typedef LRESULT (WINAPI *PFN_Hook)(int, WPARAM, LPARAM);

    HINSTANCE hDLL = LoadLibrary("ShellProcDLL.dll");
    ASSERT(NULL != hDLL);

    PFN_Hook pHook = (PFN_Hook) ::GetProcAddress(hDLL, "CBTProc");
    ASSERT(NULL != pHook);





  5. #5
    Join Date
    Aug 1999
    Location
    Chile
    Posts
    177

    Re: How to import a function from a DLL

    I think the answer to my question is simpler than you have actually answered.. I only want to use a FUNCTION from a DLL not made by me... to be able to compile the code, I have include this in the header file:


    int __declspec( dllimport ) IBE_WIN_ResetBoard();




    where IBE_WIN_ResetBoard( ) is the function to be imported. The only problem to be solved now is how can the linker link that function with my project. The name of the DLL is T3IBEW95.DLL. I have included this file in project settings but linker doesn't recognize the format... how can I create the corresponding LIB file? LIB files I could create with BC++ would have different format..

    Thanks
    Jaime


  6. #6
    Guest

    Re: How to import a function from a DLL

    There two ways to call DLL's exported functions: Implicit and Explicit.

    Implicit linking requires lib file during link. This lib file is created when the DLL is built. Therefore, if you don't have the lib file, you should consider explicit linking.

    Explicit linking is done by you when you need the function (as opposed to Windows loading all DLLs that are implicitly linked when your program gets loaded). You call LoadLibrary() to find/load the DLL and GetProcAddress() to get the address of the function.

    Example:

    typedef int (TestFunction)(int);
    HINSTANCE hDll;
    TestFunction* pFunc;
    hDll = ::LoadLibrary("testdll.dll");
    pFunc = (TestFunction*)::GetProcAddress(hDll, "FunctionName");
    int result = (*pFunc)(100); /* call the function passing 100 as the argument */





  7. #7
    Join Date
    Aug 1999
    Location
    Chile
    Posts
    177

    Re: How to import a function from a DLL

    Thanks!!! your hint was really useful... I have already solved the problem..

    Jaime


  8. #8
    Join Date
    Jan 2002
    Location
    India
    Posts
    6

    How to import a function from a DLL

    Hello Sir,
    I am a final year engineering student.I am writing a DLL for my project.Now I like to import these functions from my DLL (written in VC++) to a VC++ application.When I try to implicitly link the library with the apllication I get a linker error.I have declared all the functions to be exported in the dll as _stdcall.Please help me.
    Your's sincerely,
    Vignesh


  9. #9
    Join Date
    Jun 1999
    Posts
    1,786

    Re: How to import a function from a DLL

    Hello

    Here are steps for importing functions:
    In your DLL project you:
    1. In stdafx.h add these lines:
    #define DllImport __declspec( dllimport )
    #define DllExport __declspec( dllexport )
    2. In your project settings dialog you must add a macro which will be used as a flag.
    'C/C++' tab, Category combo set to 'Preprocessor', in preprocessor definitions you add something like 'MY_FIRST_DLL'. It will be a 'signature' of your DLL. If your application includes more than one project, each DLL must have an unique 'signature'
    3. In your DLL you create a header which will include declarations of functions you want to declare. This header must contain these definitions:
    3a.
    #ifdef MY_FIRST_DLL //for header only
    #define MyFirstDllExport DllExport
    #else
    #define MyFirstDllExport DllImport
    #endif
    3b. Declarations of functions or classes you want to export from your DLL.
    All declarations must be prefixed by the word 'MyFirstDllExport'

    MyFirstDllExport void MyFunction4Export();
    class MyFirstDllExport MyClassForExport
    {
    };



    4. You implement you functions and classes in CPP file. It's not necessary to mention any 'import/export' stuff in CPP file - you just do not forget to "#include" header with declaration to CPP
    5. That's pretty much it for DLL part. Now after compilation compiler will create a LIB file. To make sure everything is fine try to open thsi file in DevStudio and find names of functions you want to export. File is binary and names will be founded as substrings.

    Now in EXE project which uses you DLL you:
    1. Do the same changes in EXE's stdafx.h, i.e. add these lines:
    #define DllImport __declspec( dllimport )
    #define DllExport __declspec( dllexport )
    2. In the place where you want to call functions from DLL you include a header file from DLL. I assume you have DLL in a separate directory. So, your include will look like
    #include "MyDLL/ExportedStuff.h"
    'ExportedStuff.h' is a file mentioned in p.3 of prev section.
    3. You write calls of these functions
    4. Do not forget to link your EXE project with LIB generated during DLL compilation
    5. Compile EXE project
    6. Place DLL file in the same directory where EXE file is and run.
    That's it.

    I use this method for years now, it doesn't need any DEF files and never gave me any troubles.










    Good luck
    Please rate if you think this response was useful for you

  10. #10
    Join Date
    Jan 2002
    Location
    India
    Posts
    6

    Re: How to import a function from a DLL

    Thank you for your kind reply and useful code.But I have written a win32 dll.I use a DEF file to export all functions.How can I import those functions implicitly in a MFC application.


  11. #11
    Join Date
    Jun 1999
    Posts
    1,786

    Re: How to import a function from a DLL

    Thank you for your kind reply and useful code.
    - Your are welcome

    But I have written a win32 dll.I use a DEF file to export all functions.How can I import those functions implicitly in a MFC application.
    - The mechanizm described in my prev post doesn't work for win32 dll? Did you try?
    If you didn't try, do it.
    If you tried and it doesn't work when I don't know. What's not working? Maybe we can fix it togever?



    Good luck
    Please rate if you think this response was useful for you
    Have more questions? Feel free to ask.

  12. #12
    Join Date
    Jan 2002
    Location
    India
    Posts
    6

    Re: How to import a function from a DLL

    I have written win32 dll with all the functions to be exported placed in a DEF file.Now I started to develop a VC++ MFC application linking the dll implicitly.I linked the LIB file by changing the project settings.But I get a "unresolved external error symbol" when I use a function from the dll.I have used the same declaration as present in the dll.


  13. #13
    Join Date
    Jun 1999
    Posts
    1,786

    Re: How to import a function from a DLL

    I have written win32 dll with all the functions to be exported placed in a DEF file
    - This DLL was written using Visual C++ or another compiler?





    Good luck
    Please rate if you think this response was useful for you
    Have more questions? Feel free to ask.

  14. #14
    Join Date
    Jan 2002
    Location
    India
    Posts
    6

    Re: How to import a function from a DLL

    Yes the dll was written using VC++.


  15. #15
    Join Date
    Jun 1999
    Posts
    1,786

    Re: How to import a function from a DLL

    Yes the dll was written using VC++.
    - Also I hope you have a source code? In this case why don't you use the method I gave you? Remove DEF from your DLL project, do what I described and recompile your DLL - it will work.


    Good luck
    Please rate if you think this response was useful for you
    Have more questions? Feel free to ask.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured