COM Interop without regasm
Asked Answered
B

5

20

I'm a limited user, and I need to write an Outlook macro that exposes a C# library in Outlook 2003 and 2007.

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.

Is there any way to call .Net code from VBA in this scenario, or are there any other solutions?

This is for personal use only, so an ugly hack is perfectly acceptable (so long as it works)

Blackandwhite answered 30/8, 2009 at 18:42 Comment(0)
B
18

I solved this by running regasm with the /regfile option, and replacing HKLM with HKCU in the resulting .reg file.

Blackandwhite answered 23/10, 2009 at 12:7 Comment(2)
the regasm output that I get when using this approach does not contain any HKLM keys, only HKEY_CLASSES_ROOT, which also requires admin privileges to write to. Did you find a way around this too or is my case just different from yours?Wobbling
Where do you put the DLL on the user's machine? Doesn't it still require to run gacutil (which requires admin rights)?Gravity
S
9

There's RegistrationServices.RegisterAssembly .NET API (which I believe is used by 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.

A clever program can use both RegistrationServices.RegisterAssembly and RegOverridePredefKey to register an assembly for COM clients under HKEY_CURRENT_USER.

I haven't tried this myself, but I believe it's quite doable.

Updated, apparently this approach does works.

Stallion answered 15/4, 2014 at 7:21 Comment(1)
That's an interesting idea. I'll give it a try.Gravity
H
5

Registering a COM object (ex. ActiveX control) and making it visible to all users in the system requires administrative rights.

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.

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.

Other more fine grained approaches also exits, the already mentioned RegFree COM which addresses a particular executable.

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

Hypsometry answered 22/3, 2010 at 18:27 Comment(0)
B
1

I found a possible solution, but I haven't have time to try it yet.

Blackandwhite answered 9/9, 2009 at 18:12 Comment(0)
B
0

Unfortunately due to the way that COM works it would be impossible to install a COM object (interop or otherwise) without admin privileges.

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.

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.

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. Here is a link to a [unrelated] MS article where they recommend some apps that do just that.

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'.

MS has a TechNet article (but the link has parens in it so it doesn't play nice here) so Google it, it should be the first result.

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.

Good luck.

Bly answered 9/9, 2009 at 15:47 Comment(4)
What about Registration-Free COM, or HKCU\Classes?Blackandwhite
There is no way that I'm doing this solution.Blackandwhite
I thought you would want to use it, but put the info out there for others who are hacking. What do you mean by Registration-Free COM? You can just use HKCU\Classes because COM still requires the entries in HKLM.Bly
Can a .Net assembly be used as a registration free COM server? Also, did you mean can't? As I stated in my answer, it should work.Blackandwhite

© 2022 - 2024 — McMap. All rights reserved.