title: Python GetModuleHandleW OSError: [WinError 126] The specified module could not be found
body: <p>The following code generates the error : </p>
  
  <pre>OSError: [WinError 126] The specified module could not be found</pre>
  
  <pre><code>windll.kernel32.GetModuleHandleW.restype = wintypes.HMODULE
  windll.kernel32.GetModuleHandleW.argtypes = [wintypes.LPCWSTR]
  KERNEL32 = windll.GetModuleHandleW("C:\\Windows\\System32\\kernel32.dll")
  </code></pre>
  
  <p>However, if I use</p>
  
  <pre><code>KERNEL32 = windll.LoadLibrary("C:\\Windows\\System32\\kernel32.dll")
  </code></pre>
  
  <p>The dll is found but I encounter a different error by the following code:</p>
  
  <pre><code>LoadLibAddy = windll.kernel32.GetProcAddress(KERNEL32, "LoadLibraryA")
  </code></pre>
  
  <p>The error generated is : </p>
  
  <pre>ctypes.ArgumentError: argument 1:  Don't know how to convert parameter 1</pre>
  
  <p>I was hoping someone would have an idea on how to fix either of these errors.
  The full script:</p>
  
  <pre><code>from ctypes import *
  from ctypes import wintypes
  
  def dllinjector(processID, DLL_NAME):
      KERNEL32 = WinDLL('kernel32.dll', use_last_error=True)
      KERNEL32.GetProcAddress.restype = c_void_p
      KERNEL32.GetProcAddress.argtypes = (wintypes.HMODULE, wintypes.LPCSTR)
      PROCESS_CREATE_THREAD = 0x0002
      PROCESS_QUERY_INFORMATION = 0x0400
      PROCESS_VM_OPERATION = 0x0008
      PROCESS_VM_WRITE = 0x0020
      PROCESS_VM_READ = 0x0010
      openHandle = KERNEL32.OpenProcess(PROCESS_CREATE_THREAD|
                                               PROCESS_QUERY_INFORMATION|
                                               PROCESS_VM_OPERATION|
                                               PROCESS_VM_WRITE|
                                               PROCESS_VM_READ, False, processID)
      MEM_RESERVE = 0x00002000
      MEM_COMMIT = 0x00001000
      PAGE_READWRITE = 0x04
  
      if not openHandle:
          print("OpenProcess failed.")
          print("GetLastError: ", KERNEL32.GetLastError())
          return False
      print("openProcess GetLastError: ", KERNEL32.GetLastError())
      #print("Successfully opened process.")
  
      LoadLibAddy = KERNEL32.GetProcAddress(KERNEL32._handle, b"LoadLibraryA")
      if not LoadLibAddy:
          raise WinError(ctypes.get_last_error())
      print("LoadLibAddy: ", LoadLibAddy)
      print("LoadLibAddy GetLastError: ", KERNEL32.GetLastError())
  
      # Allocate space in the process for the dll
      RemoteString = KERNEL32.VirtualAllocEx(openHandle, None, len(DLL_NAME), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)
      print("RemoteString GetLastError: ", KERNEL32.GetLastError())
      if not RemoteString:
          print("VirtualAllocEx failed.")
          return False
  
      # Write the string name of the dll in the memory allocated
      if not KERNEL32.WriteProcessMemory(openHandle, RemoteString, DLL_NAME, len(DLL_NAME), None):
          print("WriteProcessMemory failed.")
          return False
      print("WriteProcessMemory GetLastError: ", KERNEL32.GetLastError())
  
      #Load the dll
      if not KERNEL32.CreateRemoteThread(openHandle, None, None, c_longlong(LoadLibAddy), RemoteString, None, None):
          print("CreateRemoteThread failed.")
          return False
      print("CreateRemotedThread GetLastError: ", windll.kernel32.GetLastError())
      #print("CreateRemoteThread: ", windll.kernel32.CreateRemoteThread(openHandle, None, None, LoadLibAddy, RemoteString, None, None))
  
      KERNEL32.CloseHandle(openHandle)
      print("CloseHandle GetLastError: ", KERNEL32.GetLastError())
  
      return True
  
  def main():
      processID = 25408
      DLL_NAME = "mydll32.dll"
  
      dllinjector(processID, DLL_NAME)
  
      print("program completed.")
  
  main()
  </code></pre>