-
March 13th, 2009, 04:30 PM
#1
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)?
-
March 13th, 2009, 11:10 PM
#2
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.
-
March 14th, 2009, 02:27 AM
#3
Re: Best way to call functions exported by a dll?
Originally Posted by quandary
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
-
March 14th, 2009, 05:33 AM
#4
Re: Best way to call functions exported by a dll?
Originally Posted by quandary
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
-
March 17th, 2009, 07:21 AM
#5
Re: Best way to call functions exported by a dll?
Originally Posted by Paul McKenzie
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!
-
March 17th, 2009, 07:22 AM
#6
Re: Best way to call functions exported by a dll?
Originally Posted by _Superman_
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?
-
March 17th, 2009, 07:28 AM
#7
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.
-
March 17th, 2009, 07:55 AM
#8
Re: Best way to call functions exported by a dll?
Originally Posted by 0xC0000005
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.
-
March 17th, 2009, 08:12 AM
#9
Re: Best way to call functions exported by a dll?
Originally Posted by quandary
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.
-
March 17th, 2009, 09:18 AM
#10
Re: Best way to call functions exported by a dll?
Originally Posted by GCDEF
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...
-
March 17th, 2009, 10:24 AM
#11
Re: Best way to call functions exported by a dll?
-
March 17th, 2009, 10:46 AM
#12
Re: Best way to call functions exported by a dll?
Originally Posted by Codeplug
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.
-
March 17th, 2009, 10:52 AM
#13
Re: Best way to call functions exported by a dll?
Originally Posted by 0xC0000005
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
-
March 17th, 2009, 11:52 AM
#14
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
-
March 17th, 2009, 12:17 PM
#15
Re: Best way to call functions exported by a dll?
Originally Posted by quandary
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|