Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatically registering .dll's on Windows Vista (using DllRegisterServer)

Instead of calling regsvr32.exe, one can register a .DLL using the following steps:

HINSTANCE hLib = ::LoadLibraryEx(dllPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
HRESULT (STDAPICALLTYPE* lpDllEntryPoint)(void);
(FARPROC&)lpDllEntryPoint = ::GetProcAddress(hLib, "DllRegisterServer");
const HRESULT hRes = (*lpDllEntryPoint)();

This works fine on Windows XP. Regrettably, it fails on Vista, but only with some specific DLLs. hRes becomes E_ACCESSDENIED. I guess this is a security issue. Does anyone know how to register a .DLL from code on Windows Vista?

Note: I was logged in as administrator when running this code.

like image 743
Dimitri C. Avatar asked Sep 07 '09 11:09

Dimitri C.


People also ask

How do I manually register a DLL?

Click Start > All Programs > Accessories and right-click on "Command Prompt" and select "Run as Administrator" OR in the Search box, type CMD and when cmd.exe appears in your results, right-click on cmd.exe and select "Run as administrator" At the command prompt, enter: REGSVR32 "PATH TO THE DLL FILE"

How do I register a DLL in SysWOW64?

to register the Microsoft "ScrRun. dll" file in the Windows\SysWOW64 folder of a 64-bit version of Windows, type cd \Windows\SysWOW64 and press ENTER; then type regsvr32 scrrun. dll and press ENTER; to register the CTIAnnouncement.

How do I register a 32-bit DLL?

Register 32 or 64-bit DLLs in Windows Step 1: First click on Start, then Run. Step 2: Now all you have to do to register a DLL file is to type in the regsvr32 command, followed by the path of the DLL file. Step 3: Now click OK and you should get a confirmation message that the DLL has been registered successfully.


1 Answers

COM registration requires write access to the HKEY_LOCAL_MACHINE part of the registry.

Under UAC, write access to the HKEY_LOCAL_MACHINE requires an elevated administrator.

The easiest way to get an elevated process is to create it with a manifest that specifies 'requireAdministrator' access. - Look under the Project Properties -> Configuration Properties->Linker->Manifest File->UAC Execution Level to set the correct setting.

This means you will probably want to split your EXE into two parts. The 'normal' asInvoker part, and, when self registration is detected as a requirement, an elevated InstallMyself part. When the non elevated part detects a first-run type condition, it needs to use ShellExecute(Ex) to execute the FirstInstall.exe part - using CreateProcess or some other API will simply fail with a insufficient privilege error. ShellExecute will present the UAC prompt.

It is possible to use Application Isolation to load COM dll's without any registration step at all.


Is is unfortunate that the cause cannot be determined. However, if you are interested in doing further research, a tool that will help a lot would be Process Monitor from SysInternals. Process Monitor can log all the File, Registry and other access for a process, including all success and fail codes making it a lot easier to debug problems like this without having to resort to deeper means of reverse engineering.

like image 123
Chris Becke Avatar answered Oct 19 '22 22:10

Chris Becke