vba 无需重新加气的 COM 互操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1354581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
COM Interop without regasm
提问by SLaks
I'm a limited user, and I need to write an Outlook macro that exposes a C# library in Outlook 2003 and 2007.
我是一个受限用户,我需要编写一个 Outlook 宏,在 Outlook 2003 和 2007 中公开一个 C# 库。
I do not have any admin privilges at all, not even at install time, so I can't run RegAsm and I can't (I assume) write a managed add-in.
我根本没有任何管理员权限,甚至在安装时也没有,所以我无法运行 RegAsm 并且我无法(我假设)编写托管加载项。
Is there any way to call .Net code from VBA in this scenario, or are there any other solutions?
在这种情况下,有没有办法从 VBA 调用 .Net 代码,或者还有其他解决方案吗?
This is for personal use only, so an ugly hack is perfectly acceptable (so long as it works)
这仅供个人使用,因此完全可以接受丑陋的 hack(只要它有效)
采纳答案by SLaks
I solved this by running regasm
with the /regfile
option, and replacing HKLM with HKCU in the resulting .reg
file.
我通过运行regasm
该/regfile
选项解决了这个问题,并在结果.reg
文件中用 HKCU 替换了 HKLM 。
回答by noseratio
There's RegistrationServices.RegisterAssembly
.NET API (which I believe is used by RegAsm.exe
).
有RegistrationServices.RegisterAssembly
.NET API(我相信它被 使用RegAsm.exe
)。
There's also RegOverridePredefKey
Win32 API, inconspicuous but quite useful, which allows to remap HKEY_CLASSES_ROOT
(used by RegisterAssembly
) to HKEY_CURRENT_USER\Software\Classes
.
还有RegOverridePredefKey
Win32 API,不显眼但非常有用,它允许将HKEY_CLASSES_ROOT
(由 使用RegisterAssembly
)重新映射到HKEY_CURRENT_USER\Software\Classes
.
A clever program can use both RegistrationServices.RegisterAssembly
and RegOverridePredefKey
to register an assembly for COM clients under HKEY_CURRENT_USER
.
一个聪明的程序可以使用RegistrationServices.RegisterAssembly
和RegOverridePredefKey
来为 .NET 下的 COM 客户端注册程序集HKEY_CURRENT_USER
。
I haven't tried this myself, but I believe it's quite doable.
我自己没有尝试过,但我相信这是完全可行的。
Updated, apparently this approach does works.
更新,显然这种方法确实有效。
回答by okigan
Registering a COM object (ex. ActiveX control) and making it visible to all users in the system requires administrative rights.
注册 COM 对象(例如 ActiveX 控件)并使其对系统中的所有用户可见需要管理权限。
BUT, if visibility to all users is not needed (or not possible due to limited user restriction) then the COM object can be registered only for the particular user.
但是,如果不需要所有用户的可见性(或由于有限的用户限制而不可能),则只能为特定用户注册 COM 对象。
This generally done by registering the object in HKCU instead of HKLM. This will apply for "regular" COM objects and the ones exposed through .NET COM Interop.
这通常通过在 HKCU 而非 HKLM 中注册对象来完成。这将适用于“常规”COM 对象和通过 .NET COM Interop 公开的对象。
Other more fine grained approaches also exits, the already mentioned RegFree COM which addresses a particular executable.
其他更细粒度的方法也存在,已经提到的 RegFree COM 解决了特定的可执行文件。
So, given all of the above the solution to capture all info into reg file and replace HKLM with HKCU shall work (not nice, but so are the tools).
因此,鉴于上述所有内容,将所有信息捕获到 reg 文件中并用 HKCU 替换 HKLM 的解决方案应该可以工作(不好,但工具也是如此)。
回答by SLaks
回答by Andrew Burns
Unfortunately due to the way that COM works it would be impossible to install a COM object (interop or otherwise) without admin privileges.
不幸的是,由于 COM 的工作方式,没有管理员权限就无法安装 COM 对象(互操作或其他)。
When you register a com objects it writes several entries to HKEY_CLASSES_ROOT
which is a machine level key and therefore requires admin privileges.
当您注册一个 com 对象时,它会写入几个条目,HKEY_CLASSES_ROOT
这些条目是机器级别的密钥,因此需要管理员权限。
Now because you did say that a hackish solution is acceptable here are some possible steps you could take to get it to work; however if you do not have admin rights I am assuming that this is not your machine and most likely a work machine. Taking these steps could very well get you fired. Standard disclaimer and whatnot: It isn't my fault if you are a moron and get yourself sued/fired/shot/mugged/etc.
现在,因为您确实说过可以接受 hackish 解决方案,所以您可以采取一些可能的步骤来使其工作;但是,如果您没有管理员权限,我假设这不是您的机器,很可能是一台工作机器。采取这些步骤很可能会让你被解雇。标准免责声明和诸如此类:如果你是个白痴并且让自己被起诉/解雇/枪击/抢劫/等等,这不是我的错。
First you need to find out what registry keys need to be added. Make sure you are doing this on a 'clean' machine that host not seen your component before (or one you know that doesn't have the reg keys). Install a machine monitoring program to capture the keys. Hereis a link to a [unrelated] MS article where they recommend some apps that do just that.
首先,您需要找出需要添加哪些注册表项。确保您在一台“干净”的机器上执行此操作,该机器以前从未见过您的组件(或您知道没有 reg 密钥的机器)。安装机器监控程序来捕获密钥。 这是 [无关] MS 文章的链接,他们推荐了一些可以做到这一点的应用程序。
Now with the monitor running install you addin. You should now be able to get a log of what keys need to be created. Using your log create registry export files.
现在随着监视器运行安装你的插件。您现在应该能够获得需要创建哪些密钥的日志。使用您的日志创建注册表导出文件。
Now here is where it gets tricky. Basically what you will do is boot the machine from a BartPE image and mount the registry and run your scripts; however the key paths will be incorrect so the scripts will not work 'out of the box'.
现在这就是棘手的地方。基本上,您要做的是从 BartPE 映像启动机器并挂载注册表并运行您的脚本;但是,关键路径将不正确,因此脚本将无法“开箱即用”。
MS has a TechNet article (but the link has parens in it so it doesn't play nice here) so Googleit, it should be the first result.
MS 有一篇 TechNet 文章(但链接中有括号,所以它在这里不好用)所以谷歌它,它应该是第一个结果。
After you have read that article you will see that the machines registry will be mounted as a subkey of HKEY_LOCAL_MACHINE
. What you need to do is change your registry exports to match the new path. As soon as this step is complete you should be able to boot from your BartPE image, mount the registry, and then import your scripts, unmount the hive and then reboot. Assuming you copied the files to the correct path then it 'should' work.
阅读完那篇文章后,您将看到机器注册表将作为HKEY_LOCAL_MACHINE
. 您需要做的是更改注册表导出以匹配新路径。完成此步骤后,您应该能够从 BartPE 映像启动,安装注册表,然后导入脚本,卸载配置单元,然后重新启动。假设您将文件复制到正确的路径,那么它“应该”工作。
Good luck.
祝你好运。