Archived

This topic is now archived and is closed to further replies.

Hippokrates

Working with DLLs

Recommended Posts

I would like to load DLLs at runtime in my programme using LoadLibary und access functions they contain. What I am currently doing is this:
//Expected Functions:
typedef bool (*FILTERPROC)(LPBYTE pbySourceBuffer, DWORD dwSourceLength, LPBYTE pbyDestBuffer, DWORD * pdwDestLength);
typedef DWORD (*SIZEQUERY)(DWORD dwSourceSize);
//The functions:
FILTERPROC Modify;
SIZEQUERY Size;

//Loading the DLL and using the functions:
for(i = 0; i < m_Header.wNumberFilters; i++) {
		hFilter = LoadLibrary(m_FilterEntries.strFileName);
				Size = (SIZEQUERY)GetProcAddress(hFilter, "RequiredSize");
				
		Modify = (FILTERPROC)GetProcAddress(hFilter, "Modify");
				
		Modify(pbyContent, dwContentSize, pbyFinalContent, &dwFinalContentSize);
 
For some reason GetProcAddress returns NULL when trying to load RequiredSize. I don`t know why, do you? ;-)

Share this post


Link to post
Share on other sites
First of all make sure that the LoadLibrary() call is successful. If it is, your problem is very likely that the function in your DLL isn't actually exported as "RequiredSize", but rather as some kind of mangled name (it depends on your compiler, but a mangled name might look something like "?RequiredSize@@YAXXZ").


[edited by - spock on September 30, 2002 8:06:21 PM]

Share this post


Link to post
Share on other sites
In addition to what spock said - remember that the function name passed to GetProcAddress has to be an ANSI string. That might cause problems in a unicode context. That doesn''t look to be the case with your sample code because of the string literals - but if by chance you used an LPTSTR variable to pass in the function names ... well, I suppose that would generate a compile time error. Still, just another thing to be aware of



"Beautiful maiden," answered Candide, "when a man is in love, is jealous, and has been flogged by the Inquisition, he becomes lost to all reflection."

Share this post


Link to post
Share on other sites
Well, as you said, I am using ANSI literals.
The call to LoadLibrary should be successful to for the handle is not NULL.
But I still can`t load that function...
[EDIT]
Perhaps I am declaring them the wrong way?

DWORD _declspec(dllexport) RequiredSize(DWORD dwSourceLength);

And I do only have the DLL in my directory. Do I need any additional files ?

[edited by - Hippokrates on October 1, 2002 8:18:15 AM]

Share this post


Link to post
Share on other sites
As spock said, the name of your exported function was probably decorated when you built your DLL. To solve this problem, i''m using:

extern "C" __declspec(dllexport) DWORD RequiredSize(DWORD dwSourceLength);

This prevents the name from being decorated.

If you''re using Visual Studio, there''s a program called "Dependency Walker" (DEPENDS.EXE) which allows you to see dependencies and exported functions of .DLL,.EXE, .. It is very usefull to find out if all functions were correctly exported.

It is also a good thing to check if GetProcAddress returned a valid function pointer (ie not NULL).

To answer your second question, yes you can use GetProcAddress from a DLL.

Share this post


Link to post
Share on other sites
I think the problem is with the function declaration in the dll.

Instead of:
DWORD _declspec(dllexport) RequiredSize(DWORD dwSourceLength);

I would use (and do use):
extern "C" _declspec(dllexport)DWORD RequiredSize(DWORD, dwSourceLength);

The reason being that runtime dll functions must be C functions and not C++. This worked for me.

Share this post


Link to post
Share on other sites
THANKS A LOT!
It did indeed help!
Of course I had to despair again because for some reasons I had to rebuild all DLLs several times before VC++ accepted that I finally added this extern "C" but it is working now!
So thanks a lot!

Share this post


Link to post
Share on other sites