Best way to call functions exported by a dll?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Best way to call functions exported by a dll?

  1. #1
    Join Date
    Oct 2005
    Posts
    71

    Question Best way to call functions exported by a dll?

    Question:

    I have a dll that exports some functions.

    Like:
    EXPORT char* WINAPI WebServicesGetPluginName()
    EXPORT bool WINAPI WebServicesInit(char *szAppTitle, char* szPath, IDispatch* pCALLBACK)
    EXPORT int WINAPI WebServicesGetVersion(char *version);

    Now to have access to them in an executable that loads the dll via LoadLibrary, I do the following:
    1. typedef the function pointer, e.g.: typedef int (WINAPI *WebServicesGetVersion_t)(char *);
    2. create a function pointer: WebServicesGetVersion_t WebServicesGetVersion ;
    3. get the function address via getproc, e.g. //WebServicesGetVersion = (WebServicesGetVersion_t) GetProcAddress(hModWebServices, "WebServicesGetVersion");


    Now if the dll exports some houndred of functions, this is quite much typing work...
    Is there a better way (getting the function type, creating a variable)?

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Best way to call functions exported by a dll?

    Do implicit linking using the LIB file that is generated along with the DLL.
    In this method give the LIB file as a reference to the linker generating the EXE.

    Now you can directly call the exported functions without doing a GetProcAddress on it.
    _Superman_
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  3. #3
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Best way to call functions exported by a dll?

    Quote Originally Posted by quandary View Post
    Now if the dll exports some houndred of functions, this is quite much typing work...
    Declare the functions that you actually use in the program. You don't need to create function pointers for every single exported function.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,027

    Re: Best way to call functions exported by a dll?

    Quote Originally Posted by quandary View Post
    Now if the dll exports some houndred of functions, this is quite much typing work...
    Is there a better way (getting the function type, creating a variable)?
    You know, this is not the most complex problem a programmer typically faces to...
    Best regards,
    Igor

  5. #5
    Join Date
    Oct 2005
    Posts
    71

    Arrow Re: Best way to call functions exported by a dll?

    Quote Originally Posted by Paul McKenzie View Post
    Declare the functions that you actually use in the program. You don't need to create function pointers for every single exported function.

    Regards,

    Paul McKenzie
    Unfortunately, I need most of them!

  6. #6
    Join Date
    Oct 2005
    Posts
    71

    Arrow Re: Best way to call functions exported by a dll?

    Quote Originally Posted by _Superman_ View Post
    Do implicit linking using the LIB file that is generated along with the DLL.
    In this method give the LIB file as a reference to the linker generating the EXE.

    Now you can directly call the exported functions without doing a GetProcAddress on it.
    Can I generate a LIB file somehow, if I only have the dll?

  7. #7
    Join Date
    May 2002
    Posts
    1,435

    Re: Best way to call functions exported by a dll?

    If I faced the same task I would write a utility program that reads each line of exported function declarations, parses the information and writes to a text file typedefs, calls to GetProcAddress(), etc. in C++ source code format. I could probably do that in a fraction of the time that it would take to enter all the information manually - and it would not make typing mistakes.

  8. #8
    Join Date
    Oct 2005
    Posts
    71

    Talking Re: Best way to call functions exported by a dll?

    Quote Originally Posted by 0xC0000005 View Post
    If I faced the same task I would write a utility program that reads each line of exported function declarations, parses the information and writes to a text file typedefs, calls to GetProcAddress(), etc. in C++ source code format. I could probably do that in a fraction of the time that it would take to enter all the information manually - and it would not make typing mistakes.
    Good idea, just at the moment, I'm playing with the Microsoft Interface Definition Language Compiler.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,133

    Re: Best way to call functions exported by a dll?

    Quote Originally Posted by quandary View Post
    Can I generate a LIB file somehow, if I only have the dll?
    Whoever created the DLL should be able to supply a LIB file. That would be the easiest way to go.

  10. #10
    Join Date
    Oct 2005
    Posts
    71

    Unhappy Re: Best way to call functions exported by a dll?

    Quote Originally Posted by GCDEF View Post
    Whoever created the DLL should be able to supply a LIB file. That would be the easiest way to go.
    The provider company has filed bankruptcy already some time ago...

  11. #11
    Join Date
    Nov 2003
    Posts
    1,815

  12. #12
    Join Date
    Oct 2005
    Posts
    71

    Wink Re: Best way to call functions exported by a dll?

    Quote Originally Posted by Codeplug View Post
    Typical Microsoft documentation.
    Long, imprecise and useless.

    Code:
    Create .lib file from .dll
    
    search c:\program files\Microsoft Visual Studio for dumpbin.exe
    
    Copy it to your project path
    Now run the dumpbin command to get a list of all exported functions of your dll:
    
    dumpbin /exports C:\msys\1.0\home\admin\myproject\mylib.dll
    
    
    This will print quite a bit of text to the console. However we are only interested in the functions:
    
    
     ordinal hint RVA      name
    
              4    0 00001113 MyDllClose
             19    1 0000112C MyDllConfigureNonModal
              2    2 00001064 MyDllDetect
             20    3 00001122 MyDllDisable
             18    4 00001014 MyDllEnable
              7    5 00001078 MyDllGetChannel
              6    6 000010A0 MyDllGetChannels
             11    7 00001073 MyDllGetErrorText
             14    8 000010EB MyDllGetFrameCount
             13    9 0000104B MyDllGetFrameRate
             10    A 000010AF MyDllGetMaxChannels
             16    B 000010B9 MyDllGetPluginName
              1    C 000010D7 MyDllGetVersion
             17    D 00001069 MyDllInit
              3    E 00001096 MyDllInitializeMode
              8    F 00001055 MyDllSetChannel
              5   10 0000100A MyDllSetChannels
             12   11 0000108C MyDllSetFrameRate
             15   12 0000103C MyDllSetFramesInAverage
              9   13 0000111D MyDllSetMaxChannels
             21   14 0000110E MyDllTerm
    ...etc
    
    Now copy all those function names (only the names!) and paste them into a new textfile. 
    Name the nextfile yourlib.def and put the line EXPORTS at its top. 
    
    My mylib.def file looks like this:
    
    LIBRARY MyDll
    
    EXPORTS
    	MyDllGetVersion
    	MyDllDetect
    	MyDllInitializeMode
    	MyDllClose	
    	MyDllSetChannels
    	MyDllGetChannels
    	MyDllGetChannel	
    	MyDllSetChannel	
    	MyDllSetMaxChannels
    	MyDllGetMaxChannels
    	MyDllGetErrorText
    	MyDllSetFrameRate
    	MyDllGetFrameRate
    	MyDllGetFrameCount
    	MyDllSetFramesInAverage
    	MyDllGetPluginName
    	MyDllInit	  
    	MyDllEnable	  
    	MyDllConfigureNonModal
    	MyDllDisable	      
    	MyDllTerm
    
    
    
    
    Now from that definition file, we can finally create the .lib file. 
    
    search c:\program files\Microsoft Visual Studio for lib.exe
    copy it to your project path
    
    run this command in the Command Prompt:
    lib /def:C:\msys\1.0\home\admin\myproject\mylib.def /OUT:C:\msys\1.0\home\admin\myproject\mylib.lib
    
    
    That’s it, happy coding
    Last edited by quandary; March 17th, 2009 at 10:49 AM.

  13. #13
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Best way to call functions exported by a dll?

    Quote Originally Posted by 0xC0000005 View Post
    If I faced the same task I would write a utility program
    I did a similar thing, only I parsed the header file that came with the library. I would parse the header file and create the entire source code to the function pointers, the call to LoadLibrary() and each call to GetProcAddress().

    Regards,

    Paul McKenzie

  14. #14
    Join Date
    Nov 2003
    Posts
    1,815

    Re: Best way to call functions exported by a dll?

    >> Copy it to your project path
    That's not exactly how you use the command line tools. You should have a "Visual Studio 200x Command Prompt" on your start menu. This sets up the environment variables you need (including PATH) in order to use VS tools on the command line.

    gg

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,133

    Re: Best way to call functions exported by a dll?

    Quote Originally Posted by quandary View Post
    The provider company has filed bankruptcy already some time ago...
    I'd probably look for another source of whatever functionality the DLL provides in that case.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center