注册32位COM类以在没有管理员权限的情况下在64位Python中使用 [英] Register a 32-bit COM class for use in 64-bit Python without Administrator rights

查看:48
本文介绍了注册32位COM类以在没有管理员权限的情况下在64位Python中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个通过win32com使用SAP的GUI脚本API的Python脚本.第一次用法是这样的:

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

这在使用32位Python解释器时可以正常工作,但在使用64位解释器时则不能,这会导致出现未注册类"错误.有什么方法可以做到这一点,特别是在没有管理员特权的情况下?

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?

默认答案可能类似于"64位进程无法加载32位DLL" ,但我怀疑可以这样做,原因是:

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:

  1. 此链接提到了"COM代理"过程可能会解决该问题,根据我的搜索,该问题表明DLL本身可能是由 dllhost.exe 加载的.

  1. 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应用程序(例如Excel)可以毫无问题地完成此任务.即使最困难的部分也已完成,这允许32位应用程序访问64位服务器.我只是不确定如何做到这一点,因为COM有点像黑魔法,而且我不知道我是否可以对第三方DLL(如SAP的DLL)执行此操作.缺少管理员权限使事情变得更加复杂.

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.

实际上我已被允许在Excel(64位)VBA中使用SAP GUI脚本API.接下来是一个示例模块,对于该模块,我不是必须编辑VBA的引用:

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

那么,如何在Python 64位解释器中执行此操作?

So, how can I do this from within a Python 64-bit interpreter?

推荐答案

64位进程无法直接加载32位进程内 COM服务器(即DLL),反之反之亦然,原因很明显(例如位数不匹配).

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应用程序不是进程内服务器,它们是进程外服务器(即它们在自己的EXE中运行),因此它们不是遭受这个问题.可以跨进程边界使用COM,而无需考虑每个进程的位数,因为对COM对象的调用/根据需要进行了封送处理.

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.

是的,通过将COM代理包装在进程外代理中,COM代理是解决此问题的一种方法,用于进程中服务器操作系统提供的服务器.

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.

这篇关于注册32位COM类以在没有管理员权限的情况下在64位Python中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆