Drip Narix
Drip Narix

Reputation: 1

Executing Python File to Exe ImportError: DLL load failed while importing win32gui:

I have a python script, that is changing your cursor, when you run the script. The problem is, that if I run the python file, then it works without any problems, but after I executed the file to an exe and I run that, I get the error message (in my case in german "ImportError: DLL load failed while importing win32gui: Die angegebene Prozedur wurde nicht gefunden." I do understand that it does not find "win32gui", but why does it find win32con and not win32gui?

Here is my code:

import ctypes
from time import sleep
import win32con
import win32gui
import os, sys

def resource_path(relative_path):
   try:
      base_path = sys._MEIPASS
   except Exception:
      base_path = os.path.abspath(".")

   return os.path.join(base_path, relative_path)

path = resource_path("./test.cur")
sleeptime = 30

cursor_value = {
    32650: "OCR_APPSTARTING",
    32512: "OCR_NORMAL",
    32515: "OCR_CROSS",
    32649: "OCR_HAND",
    32651: "OCR_HELP",
    32513: "OCR_IBEAM",
    32648: "OCR_NO",
    32646: "OCR_SIZEALL",
    32643: "OCR_SIZENESW",
    32645: "OCR_SIZENS",
    32642: "OCR_SIZENWSE",
    32644: "OCR_SIZEWE",
    32516: "OCR_UP",
    32514: "OCR_WAIT"
}

original_cursor = {}

def load_original_cursor(Values):
    for k, v in Values.items():
        hold = win32gui.LoadImage(0, k, win32con.IMAGE_CURSOR, 0, 0, win32con.LR_SHARED)
        hsave = ctypes.windll.user32.CopyImage(hold, win32con.IMAGE_CURSOR, 0, 0, win32con.LR_COPYFROMRESOURCE)
        original_cursor[k] = hsave
    print(original_cursor)

def set_fake_cursor(path, Values):
    for k, v in Values.items():
        hnew = win32gui.LoadImage(0, path, win32con.IMAGE_CURSOR, 0, 0, win32con.LR_LOADFROMFILE)
        ctypes.windll.user32.SetSystemCursor(hnew, k)

def set_original_cursor(Original_Values):
    for k, v in Original_Values.items():
        ctypes.windll.user32.SetSystemCursor(v, k)



if __name__ == "__main__":
    load_original_cursor(cursor_value)
    set_fake_cursor(path, cursor_value)
    sleep(sleeptime)
    set_original_cursor(original_cursor)
    sys.exit(-1)

Upvotes: 0

Views: 157

Answers (1)

Drip Narix
Drip Narix

Reputation: 1

I found a solution for this problem. I created a new python environment in my Folder and installed all the packeges again (in the environment). It finally worked!

Upvotes: 0

Related Questions