Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8417047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:56:08+00:00 2026-06-10T01:56:08+00:00

I am using dliNotePreLoadLibrary in my own delay loading hook to verify the code

  • 0

I am using dliNotePreLoadLibrary in my own delay loading hook to verify the code signature of a DLL to be loaded before actually loading it. In order to avoid it to execute any code whatsoever, I load it by means of LoadLibraryExA(...,...,LOAD_LIBRARY_AS_DATAFILE) (the hook only provides the ANSI name, so that’s okay) and unload it after verification so that it can be loaded as a DLL with code.

Stepping through the code in the debugger I can see that I receive a handle and that handle is <baseaddress>+1 as expected for a “data module”. However, once I attempt to pass this handle to GetModuleFileName inside a library function (that is also the reason why I can’t pass the name, only the module handle), the function returns 0 and GetLastError gives me ERROR_MOD_NOT_FOUND. However, the module got loaded, so it was definitely found. Also, this is in the current process, so access to the “target process” is no issue here.

So I thought why not use VirtualQuery to retrieve the actual base address (MEMORY_BASIC_INFORMATION::BaseAddress) of the DLL I just loaded (in case the <baseaddress>+1 is an issue), but the result remains the same: ERROR_MOD_NOT_FOUND.

I’m out of ideas. Does anyone have any ideas what is going on here?

Platform of the tests: Windows 7 SP1, x64 (latest patches)

Here’s the code of the:

FARPROC WINAPI MyDliHook(unsigned dliNotify, PDelayLoadInfo pdli)
{
    switch(dliNotify)
    {
    case dliNotePreLoadLibrary:
        if(0 == lstrcmpiA(pdli->szDll, "DLLNAME.dll"))
        {
            HMODULE hVerifiedDll = LoadLibraryExA(pdli->szDll, NULL, LOAD_LIBRARY_AS_DATAFILE);
            if(hVerifiedDll)
            {
                MEMORY_BASIC_INFORMATION mbi;
                if(0 != VirtualQuery(hVerifiedDll, &mbi, sizeof(mbi)));
                {
                    VerifyModuleSignature((HMODULE)mbi.BaseAddress, pdli->szDll);
                }
                FreeLibrary(hVerifiedDll);
            }
        }
        break;
    default:
        break;
    }
    return NULL;
}
PfnDliHook __pfnDliNotifyHook2 = MyDliHook;

The function VerifyModuleSignature calls first of all GetModuleFileName to retrieve the file name and it fails at that step. I verified that with the .exe that creates the process, the code signature verification works fine.

Side-note: I have verified that the ERROR_MOD_NOT_FOUND error comes from GetModuleFileName and not, for example, from the earlier call to LoadLibraryExA. In order to make sure I don’t get a “polluted” last error code I called SetLastError(ERROR_SUCCESS) right before the call to GetModuleFileName.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:56:09+00:00Added an answer on June 10, 2026 at 1:56 am

    As explained in MSDN, you cannot retrieve the Module using GetModuleHandle when LOAD_LIBRARY_AS_DATAFILE is used:

    If this value is used, the system maps the file into the calling process’s virtual address space as if it were a data file. Nothing is done to execute or prepare to execute the mapped file. Therefore, you cannot call functions like GetModuleFileName, GetModuleHandle or GetProcAddress with this DLL.

    Edit: Actually Hans Passant was a bit closer to the reason than this answer. While this answer reiterates what MS states, it doesn’t give a reason. The reason however, is that GetModuleFileName basically walks the LDR_MODULE list of the PEB, and that is only populated with the details when you “load code”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using EF Code First I have an model object that has multiple properties that
Using Rails 3.2.0.rc2 and ruby 1.9.3p0 In app/views/requests/_form.html.erb I have the following code for
using VB.Net2010 I need to call a C# DLL The problem I have is
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
using this code I can send one texture to the shader: devcon->PSSetShaderResources(0, 1, &pTexture);
Using DMD 2.057, I cannot get the following code to compile: import std.stdio; import
Using ValueInjecter, I often find myself writing code like this: var foo1 = new
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
Using a populated Table Type as the source for a TSQL-Merge. I want to
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.