0

I am trying to get file information handled by notepad.exe.

So, my program does the following steps.

  1. Create process for notepad.exe

    CreateProcess(NULL, szCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

  2. Wait until finish initialization of notepad.exe

    WaitForInputIdle(pi.hProcess, 10000);

  3. Attach notepad.exe process to my program as Debugee.

    DebugActiveProcess(dwPID)

  4. Wait for debug event from Debugee.

  5. When my program receive CREATE_PROCESS_DEBUG_EVENT, doing something I need.

Here is my function having an issue.

LPVOID g_pfHookingAdd = NULL;
BOOL OnCreateProcessDebugEvent(LPDEBUG_EVENT pde)
{
    DWORD dwLastErr;
    if (NULL == GetModuleHandleA("advapi32.dll"))   // Not able to get a handle here.
    {
        dwLastErr = GetLastError(); // dwLastErr => 126 => (0x7E) 
    }
    g_pfHookingAdd = GetProcAddress(GetModuleHandleA("advapi32.dll"), "IsTextUnicode");
    return TRUE;
}

As you can see, my goal is to retrieve the address where IsTextUnicode() function is loaded.

However, when I call GetModuleHandleA("advapi32.dll"), I get error code 126, which is ERROR_MOD_NOT_FOUND.

I also checked that advapi32.dll is loaded during the notepad.exe execution.

Can anyone tell me why this is not working?

Here is my environmental conditions:

Windows 10 pro version 1803 (OS build 17134.165)

Community
  • 1
  • 1
Lance
  • 13
  • 1
  • You are pretty lucky that your program did not load advapi32.dll. Goal is murky, but when you write a debugger then you need to learn how to use [the DbgHelp api](https://learn.microsoft.com/en-us/windows/desktop/debug/debug-help-library). – Hans Passant Jul 28 '18 at 12:54
  • for what you need `GetModuleHandle` if you got `LOAD_DLL_DEBUG_EVENT` ? and for what you call `DebugActiveProcess` instead just create process with flag `DEBUG_ONLY_THIS_PROCESS` ? – RbMm Jul 28 '18 at 18:12
  • I am trying to get a first byte data where function loaded. Actually it is my another issue to solve, when I create process with flag `DEBUG_ONLY_THIS_PROCESS` or `DEBUG_PROCESS`, I cannot get a right data from [`memcpy(&g_cpdi, &pde->u.CreateProcessInfo, sizeof(CREATE_PROCESS_DEBUG_INFO)); ReadProcessMemory(g_cpdi.hProcess, g_pfHookingAdd, &g_chOrgByte, sizeof(BYTE), NULL);`] – Lance Jul 28 '18 at 23:20

1 Answers1

1

That isn't working because GetModuleHandle() ...

Retrieves a module handle for the specified module. The module must have been loaded by the calling process.

Answers to GetModuleHandle(), for a DLL in another process might help you.

Swordfish
  • 12,971
  • 3
  • 21
  • 43