I have a Windows
python3.7
function which successfully calls the kernel32.dll
GetSystemPowerStatus
function using ctypes
to interrogate the power status to see if my laptop is on AC or battery power. This is a pure python solution.
I want to port this function to cygwin
python3.7
. Out of the box, python3
for cygwin
's ctypes
does not seem to allow calling a windows dll. I would prefer a pure python solution, but I can use C/C++ if necessary. Does anyone have an example of how to do this?
Edited to add the code (lines 63-67) and error messages:
elif _os.name == 'posix' and _sys.platform == 'cygwin':
# c:\Windows\System32\kernel32.dll
kernel32_name = '/proc/cygdrive/c/Windows/System32/kernel32.dll'
kernel32 = CDLL(kernel32_name)
_GetSystemPowerStatus = kernel32.GetSystemPowerStatus
$ python3.7 GetSystemPowerStatus.py
Traceback (most recent call last):
File "GetSystemPowerStatus.py", line 82, in <module>
result, systemPowerStatus = GetSystemPowerStatus()
File "GetSystemPowerStatus.py", line 66, in GetSystemPowerStatus
kernel32 = CDLL(kernel32_name)
File "/usr/lib/python3.7/ctypes/__init__.py", line 356, in __init__
self._handle = _dlopen(self._name, mode)
OSError: Invalid argument
python2.7 gives the same error, but at line 366.
Solved. See my own answer below.