0

I want create a simple console application to automatically register a dll.

After some research I found this piece of code:

public static bool Register_Dlls( string dllPath )
{
  Assembly asm = Assembly.LoadFile( dllPath );
  RegistrationServices regAsm = new RegistrationServices();
  bool bResult = regAsm.RegisterAssembly( asm, AssemblyRegistrationFlags.SetCodeBase );
  return bResult;
}

Implementing my application using that code and running it as administrator the dll can not be registered. This is my problem.

But using the RegAsm command inside the cmd (as administrator) then the dll is registered correctly:

  C:\windows\system32> C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe C:\PWS\Bin\MyContextMenu.dll /codebase

My question is: am I forgetting something or the above code can not be used for my purpose?

The dll has been compiled in 'ANY CPU' Solution Platforms mode, like my application, and I need to register the assembly on a x64 machine.

baru
  • 401
  • 3
  • 9
  • 29
  • In your dll's project, in AssemblyInfo file, should exist `ComVisible` attribute with default value `false`. Did you change it to `true`? Resulting line should be `[assembly: ComVisible(true)]`. – Sam May 19 '16 at 14:26
  • "can not be registered" is not a useful diagnostic, you'll have to tell us what failure looks like. If you did not see the UAC elevation prompt then failure is guaranteed. – Hans Passant May 19 '16 at 14:33
  • Yes, the class is COM visible, I have used the `[ComVisible(true)]` attribute before the class declaration. – baru May 19 '16 at 14:35
  • @Hans Passant: UAC does not rise up. – baru May 19 '16 at 14:41
  • [So you forgot something](http://stackoverflow.com/questions/2818179/how-to-force-my-net-application-to-run-as-administrator). – Hans Passant May 19 '16 at 14:48

1 Answers1

0

Solution:

I compiled my application forcing 'x64' Solution Platforms mode, now everything works fine.

Moreover, following the last Hans Passant comment, I improved my code modifying the manifest in its requestedExecutionLevel field and adding the IsUserAdministrator() method. Thanks Hans!

Community
  • 1
  • 1
baru
  • 401
  • 3
  • 9
  • 29