Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cpede opened this issue Feb 24, 2021 · 15 comments · May be fixed by #1675
Open

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

cpede opened this issue Feb 24, 2021 · 15 comments · May be fixed by #1675

Comments

@cpede
Copy link

cpede commented Feb 24, 2021

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

@mhammond
Copy link
Owner

How did you install the python 3 version?

@cpede
Copy link
Author

cpede commented Feb 25, 2021

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

@mhammond
Copy link
Owner

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.

@cpede
Copy link
Author

cpede commented Feb 25, 2021 via email

@mhammond
Copy link
Owner

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.

@cpede
Copy link
Author

cpede commented Feb 26, 2021

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?

@mhammond
Copy link
Owner

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
Copy link
Owner

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 mhammond changed the title Python 3 with WSH and IID_IActiveScript Never use/create win32com.gen_py, always use one under $TEMP Feb 26, 2021
@kxrob
Copy link
Collaborator

kxrob commented Feb 26, 2021

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

#1143 ?

@cpede
Copy link
Author

cpede commented Jun 24, 2021

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

@mhammond
Copy link
Owner

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()

@cpede
Copy link
Author

cpede commented Jun 24, 2021

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();

@mhammond
Copy link
Owner

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

@cpede
Copy link
Author

cpede commented Jun 24, 2021

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
Copy link
Author

cpede commented Jun 24, 2021

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants