pywin32 icon indicating copy to clipboard operation
pywin32 copied to clipboard

Never use/create `win32com.gen_py`, always use one under $TEMP

Open cpede opened this issue 4 years ago • 17 comments

I have been using the pywin32 in v2.7 together with Python 2.7: pywin32-221.win32-py2.7.exe python-2.7.18.msi

together with the Windows Script Hosting ActiveX interface in my C++ application.

After changing to the latest v3 the scripting engine cannot be instantiated using the IID_IActiveScript interface, because the class is not registered?

Any idea what the problem is?

-cpede

cpede avatar Feb 24 '21 12:02 cpede

How did you install the python 3 version?

mhammond avatar Feb 24 '21 21:02 mhammond

I first installed the python-3.9.2.exe from python.org, and then your pywin32-300.win32-py3.9.exe. Both installer went fine, and you installer found the original Python installation.

I made a small Windows console program to show the error:

// PyWin32_v3Test.cpp : This file contains the 'main' function. Program execution begins and ends there.

#include <windows.h> #include <activscp.h>

// {BB1A2AE1-A4F9-11cf-8F20-00805F2CD064} DEFINE_GUID(IID_IActiveScript, 0xbb1a2ae1, 0xa4f9, 0x11cf, 0x8f, 0x20, 0x00, 0x80, 0x5f, 0x2c, 0xd0, 0x64);

int main() { ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

CLSID clsid = GUID_NULL;
HRESULT hr = ::CLSIDFromProgID(L"Python", &clsid);
if (FAILED(hr)) return FALSE;

IActiveScript* axScript = NULL;
hr = ::CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_IActiveScript, reinterpret_cast<void**>(&axScript));
if (FAILED(hr)) return FALSE;

if (axScript) axScript->Release();


// SUCCESS
//python-2.7.18.msi from https://www.python.org/downloads/
//pywin32-221.win32-py2.7.exe from https://deac-fra.dl.sourceforge.net/project/pywin32/pywin32/Build%20221/pywin32-221.win32-py2.7.exe


//ERROR -> hr = REGDB_E_CLASSNOTREG "Class not registered"
//python-3.9.2.exe from https://www.python.org/downloads/
//pywin32-300.win32-py3.9.exe from https://github.com/mhammond/pywin32/releases

}

-cpede

cpede avatar Feb 25 '21 07:02 cpede

That looks like the 32bit version of Python 3.9 - if you executable is 64bit, then that would be expected. I believe the WSH tests all work on 3.9, so if that's not the problem I'm not surewhat it might be.

mhammond avatar Feb 25 '21 21:02 mhammond

I’m running application in 32-bit, and Python is also 32-bit. Does this dump help:

pythoncom error: ERROR: server.policy could not create an instance.

Traceback (most recent call last): File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\server\policy.py", line 136, in CreateInstance return retObj.CreateInstance(clsid, reqIID) File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\server\policy.py", line 194, in CreateInstance myob = call_func(classSpec) File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\server\policy.py", line 728, in call_func return resolve_func(spec)(*args) File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\server\policy.py", line 717, in resolve_func module = _import_module(mname) File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\server\policy.py", line 736, in import_module import(mname) File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32comext\axscript\client\pyscript.py", line 17, in import win32com.client.dynamic File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\client_init.py", line 11, in from . import gencache File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\client\gencache.py", line 657, in init() File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\client\gencache.py", line 59, in init Rebuild() File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\client\gencache.py", line 644, in Rebuild _SaveDicts() File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\client\gencache.py", line 66, in _SaveDicts f = open(os.path.join(GetGeneratePath(), "dicts.dat"), "wb") File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\client\gencache.py", line 142, in GetGeneratePath f = open(fname,"w") PermissionError: [Errno 13] Permission denied: 'C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\gen_py\init.pyfile://Python39-32/lib/site-packages/win32com/gen_py/__init__.py' pythoncom error: Unexpected gateway error

Traceback (most recent call last): File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\server\policy.py", line 136, in CreateInstance return retObj.CreateInstance(clsid, reqIID) File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\server\policy.py", line 194, in CreateInstance myob = call_func(classSpec) File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\server\policy.py", line 728, in call_func return resolve_func(spec)(*args) File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\server\policy.py", line 717, in resolve_func module = _import_module(mname) File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\server\policy.py", line 736, in import_module import(mname) File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32comext\axscript\client\pyscript.py", line 17, in import win32com.client.dynamic File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\client_init.py", line 11, in from . import gencache File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\client\gencache.py", line 657, in init() File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\client\gencache.py", line 59, in init Rebuild() File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\client\gencache.py", line 644, in Rebuild _SaveDicts() File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\client\gencache.py", line 66, in _SaveDicts f = open(os.path.join(GetGeneratePath(), "dicts.dat"), "wb") File "C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\client\gencache.py", line 142, in GetGeneratePath f = open(fname,"w") PermissionError: [Errno 13] Permission denied: 'C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\gen_py\init.pyfile://Python39-32/lib/site-packages/win32com/gen_py/__init__.py' pythoncom error: CPyFactory::CreateInstance failed to create instance. (80004005)

C:\Users\Dantec_PIV\source\repos\PyWin32_v3Test\Debug\PyWin32_v3Test.exe (process 10404) exited with code 0. To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .

Best regards / med venlig hilsen

Carsten Pedersen, MSc Software Development (Point Software)

Direct +45 4457 8143

[cid:[email protected]]

Dantec Dynamics A/S / Tonsbakken 16-18 / DK-2740 Skovlunde / Denmark Phone +45 4457 8000 / www.dantecdynamics.com

This message (including any attachments) may contain confidential, proprietary, privileged and/or private information. The information is intended to be for the use of the individual or entity designated above. If you are not the intended recipient of this message, please notify the sender immediately, and delete the message and any attachments. Any disclosure, reproduction, distribution or other use of this message or any attachments by an individual or entity other than the intended recipient is prohibited. Please be aware of the increase in cybercrime and fraud. If you receive an email purporting to be from someone at Dantec Dynamics which seeks to direct a payment to bank details which differ from those we have already given to you via an authorized invoice signed and stamped by Dantec Dynamics, it is unlikely to be genuine. Please do not reply to the email nor act on any information contained therein but contact us immediately by phone. Please consider the environment before printing this e-mail.

From: Mark Hammond [email protected] Sent: 25. februar 2021 22:30 To: mhammond/pywin32 [email protected] Cc: Carsten Pedersen [email protected]; Author [email protected] Subject: Re: [mhammond/pywin32] Python 3 with WSH and IID_IActiveScript (#1666)

That looks like the 32bit version of Python 3.9 - if you executable is 64bit, then that would be expected. I believe the WSH tests all work on 3.9, so if that's not the problem I'm not surewhat it might be.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/mhammond/pywin32/issues/1666#issuecomment-786240279, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACUD6U5K2HIQDJ4SDHJGOYLTA26MFANCNFSM4YEMIPUA.

This message (including any attachments) may contain confidential, proprietary, privileged and/or private information. The information is intended to be for the use of the individual or entity designated above. If you are not the intended recipient of this message, please notify the sender immediately, and delete the message and any attachments. Any disclosure, reproduction, distribution or other use of this message or any attachments by an individual or entity other than the intended recipient is prohibited. Please be aware of the increase in cybercrime and fraud. If you receive an email purporting to be from someone at Dantec Dynamics which seeks to direct a payment to bank details which differ from those we have already given to you via an authorized invoice signed and stamped by Dantec Dynamics, it is unlikely to be genuine. Please do not reply to the email nor act on any information contained therein but contact us immediately by phone. Please consider the environment before printing this e-mail.

cpede avatar Feb 25 '21 23:02 cpede

huh, yeah, it probably does. Can you please try removing the C:\Program Files (x86)\Python39-32\lib\site-packages\win32com\gen_py directory - that should cause pywin32 to use one in %TEMP%. There's a proposal somewhere I can't find to stop using win32com\gen_py and always just use a local writable dir which I can't find, but which I really must push forward.

mhammond avatar Feb 26 '21 00:02 mhammond

Bingo, that makes it work :-) Will you make a new version?

Two quick questions:

  • Can I include your installation .exe in my application?
  • Are there any installer switches that make the installer run unattended/silently?

cpede avatar Feb 26 '21 06:02 cpede

Will you make a new version?

Yes, eventually, but a fix for this isn't even committed yet.

* Can I include your installation .exe in my application?

Sure.

* Are there any installer switches that make the installer run unattended/silently?

No, but the installer exe is just a .zip, so you can just manually unpack, then copy the files, then run the postinstall script manually.

mhammond avatar Feb 26 '21 07:02 mhammond

I wish I could find that conversation I referred to - so in the meantime we can track the non-writable gen_py directory here

mhammond avatar Feb 26 '21 07:02 mhammond

I wish I could find that conversation I referred to - so in the meantime we can track the non-writable gen_py directory here

https://github.com/mhammond/pywin32/issues/1143 ?

kxrob avatar Feb 26 '21 21:02 kxrob

Now it is not working again. And I can't remember how/where I got the traceback file from? How do I read the traceback error when running my c++ program?

-cpede

cpede avatar Jun 24 '21 10:06 cpede

How do I read the traceback error when running my c++ program?

Generally by wrapping the python code you call in try/catch and a call to traceback.print_exc()

mhammond avatar Jun 24 '21 10:06 mhammond

Hmm I did not do that, since I'm not calling py code at all, only my c++ code, where the last part fails?

CLSID clsid = GUID_NULL;
HRESULT hr = ::CLSIDFromProgID(L"Python", &clsid);
if (FAILED(hr)) return FALSE;

IActiveScript* axScript = NULL;
hr = ::CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_IActiveScript, reinterpret_cast<void**>(&axScript));
if (FAILED(hr)) return FALSE;

if (axScript) axScript->Release();

cpede avatar Jun 24 '21 11:06 cpede

Right. Maybe registered the python com object with --debug and using win32traceutil?

mhammond avatar Jun 24 '21 11:06 mhammond

OK I found the difference between Python 2.7 which always works and 3.9. It seems that you are copying the pythoncom27.dll and the pythoncom39.dll into the Windows /System32 folder. But 2.7 also copies the dependent python27.dll, whereas python39.dll is located in the Python installation folder. I did not setup the Environment Variables why it can't find it. Copying python39.dll into the Windows /System32 folder makes it work (and probably also setting up the environment variable). When installed side by side, how to I select between running the 2.7 and 3.9 versions?

cpede avatar Jun 24 '21 12:06 cpede

It also looks like your pywin32 version 2.7 removes the pythoncom27.dll and Python removes the python27.dll from Windows/System32, during uninstall. But 3.8 and 3.9 forget to uninstall all files in Windows/System32 ?

cpede avatar Jun 24 '21 12:06 cpede