问题
I'm making a Python script that uses SAP's GUI scripting API through win32com. The first usage is like this:
from win32com.client import Dispatch
objWrapper = Dispatch("SapROTWr.SapROTWrapper")
This works fine when using a 32-bit Python interpreter, but not when using a 64-bit interpreter, which gives me a "Class not registered" error. Is there any way to do this, specially without Administrator privileges?
The default answer will probably be something like "A 64-bit process can't load a 32-bit DLL", but I suspect it may somehow be done because:
This link mentions a "COM Surrogate" process that may take care of the problem, which by my searches indicate that the DLL itself may be loaded by
dllhost.exe. This answer's comment touches on the method but doesn't solve it.MS Office applications, like Excel for example, does it without problems. Even the hardest part is done, which is allowing a 32-bit application access a 64-bit server. I'm just not sure how this is done, since COM is a bit like black magic and I don't know if I can do this to a third party DLL like SAP's. The lack of Admin rights complicates things further.
I actually am allowed to use the SAP GUI Scripting API from within Excel (64-bit) VBA. An example module follows, for which I did not have to edit VBA's References:
Public Sub SimpleSAP()
Set SapGuiAuto = GetObject("SAPGUI") 'Get the SAP GUI Scripting object
Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
Set session = SAPCon.Children(0) 'Get the first session (window) on that connection
'Start a transaction
session.StartTransaction "IH01"
End Sub
So, how can I do this from within a Python 64-bit interpreter?
回答1:
A 64-bit process cannot directly load a 32-bit in-process COM server (ie, a DLL), and vice versa, for obvious reasons (ie, a mismatch in bitness).
MS Office apps are not in-process servers, they are out-of-process servers (ie, they run in their own EXEs), so they do not suffer from this problem. COM can be used across process boundaries without regard to the bitness of each process, as calls into/out of a COM object are marshaled as needed.
And yes, a COM Surrogate is the way to work around this issue for an in-process server, by wrapping it inside of an out-of-process proxy server that is provided by the OS.
来源:https://stackoverflow.com/questions/56062925/register-a-32-bit-com-class-for-use-in-64-bit-python-without-administrator-right