Problems with delay-loading DLLs  
Author Message
Ivo6070





PostPosted: Visual C++ General, Problems with delay-loading DLLs Top

Hi.

I have a DLL module.dll and I want my application to use delay-loading. The idea is that the module may be missing and the app should detect that and continue with limited functionality. I'm using the example from here:

http://www.hide-link.com/

However I'm seeing 2 problems:

1) The __HrLoadAllImportsForDll function uses memcmp instead of stricmp to compare the argument with the list of delay-loadable modules. It turns out the string in the table is "module.DLL". So how do I know in advance what is the correct case Is it guaranteed to be that, or can it sometimes be "Module.dll", "MODULE.DLL", or something else Where does the case come from

2) Contrary to the example, if the DLL is missing the __HrLoadAllImportsForDll function throws an exception instead of returning an error code. Which one is the correct behavior Can I count on consistency accross different versions of Visual Studio and OS

Thanks

Ivo



Visual C++3  
 
 
Alex Farber





PostPosted: Visual C++ General, Problems with delay-loading DLLs Top

In one of my programs which has delay-loading libraries, I use the simple way: call LoadLibrary with Dll name. If it succeeds, I release library and set flag that library is available. If fails, I set flag that this library is not available, and don't call any functions from it later.
 
 
Simple Samples





PostPosted: Visual C++ General, Problems with delay-loading DLLs Top

To compare strings independent of case, just convert both to either all upper case or all lower case.

As for the difference between throwing an exception and returning an error code, there might be a Windows programming solution but that is outside the scope of this forum. I think there is a Windows function that can be called to set Windows to return an error code.

Using C++, can you catch the exception thrown



 
 
Ivo





PostPosted: Visual C++ General, Problems with delay-loading DLLs Top

"To compare strings independent of case, just convert both to either all upper case or all lower case."

The problem is that the second string comes from the delay load descriptor in the EXE image and is placed there by the linker. I don't control that second string, and the code that compares them. I only control the first string which is the argument I pass to __HrLoadAllImportsForDll. I don't know how the linker determines what case to use for the DLL name - that is my question.

"Using C++, can you catch the exception thrown "

Yes, I'm using __try and __except to catch the ERROR_MOD_NOT_FOUND error being thrown. My concern is that according to the samples an error code should be returned.

Ivo


 
 
Simple Samples





PostPosted: Visual C++ General, Problems with delay-loading DLLs Top

Note that the source code for __HrLoadAllImportsForDll is provided. Also, if I understand the documentation, then you must modify the source code in order to get an error code returned. I am not sure about that, but that is outside the scope of this forum.

Also note that __HrLoadAllImportsForDll uses LoadLibrary and such so it probably would be easier and as effective to just write your own code, as Alex Farber indicates. The source code for __HrLoadAllImportsForDll should be an excellent sample. If there is a reason why you must use __HrLoadAllImportsForDll, then you are likely to get help in a forum for Windows Programmeing.



 
 
Simple Samples





PostPosted: Visual C++ General, Problems with delay-loading DLLs Top

I searched the internet for something else and I happened to get an article in the CodeProject web site about delay-loading DLLs, so you should look at that article in case it helps.



 
 
Ivo





PostPosted: Visual C++ General, Problems with delay-loading DLLs Top

What's the article
 
 
Damien Watkins - MSFT





PostPosted: Visual C++ General, Problems with delay-loading DLLs Top

Hello

Re: Problems with delay-loading DLLs

As you can see from many of the responses, questions such as this are outside the scope of this forum - for the scope of the VC General forum please look at: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=19445&SiteID=1

I hope some of the suggestions/links provided here do help with answering your questions but for such issues please use a more relevant newsgroup at http://msdn.microsoft.com/newsgroups.

OTP

Thanks

Damien



 
 
Ted.





PostPosted: Visual C++ General, Problems with delay-loading DLLs Top

I see that others have had this problem as well (searching from google). The case sensitivity is a clear bug, IMO.

If you open up delayhlp.cpp from \Program Files\Microsoft Visual Studio 8\VC\include

and copy the function out of there, just make your own copy modify it, and call that instead.