ATL ActiveX 非管理员权限发布(支持vs2005)
在win7系统中,vs2005开发的atl activex需要管理员权限才能注册。
解决方法:
PerUserRegistration.h
#pragma once class PerUserRegistration { public: PerUserRegistration(bool perUser = true); ~PerUserRegistration(); private: #if _ATL_VER < 0x0900 static void EnablePerUserTLibRegistration(); bool m_mapping; #endif };
#include "PerUserRegistration.h" #include <Windows.h> #include <assert.h> PerUserRegistration::PerUserRegistration(bool perUser) #if _ATL_VER < 0x0900 : m_mapping(false) #endif { #if _ATL_VER < 0x0900 // this seems to be always active and therefore may break // any COM functionality thats not registered per user if (!perUser) { return; } HKEY key; LONG err = ::RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Classes"), 0, MAXIMUM_ALLOWED, &key); if(err == ERROR_SUCCESS) { err = ::RegOverridePredefKey(HKEY_CLASSES_ROOT, key); ::RegCloseKey(key); } if (err == ERROR_SUCCESS) { EnablePerUserTLibRegistration(); m_mapping = true; } #else // _ATL_VER >= 0900 AtlSetPerUserRegistration(perUser); #endif } #if _ATL_VER < 0x0900 void PerUserRegistration::EnablePerUserTLibRegistration() { HMODULE hOleaut32 = ::GetModuleHandle(TEXT("Oleaut32.dll")); assert(hOleaut32); typedef void (WINAPI * EnablePerUserTLibRegistrationProcPtr) (void); EnablePerUserTLibRegistrationProcPtr enablePerUserTLibRegistrationProcPtr = reinterpret_cast<EnablePerUserTLibRegistrationProcPtr>( GetProcAddress(hOleaut32, "OaEnablePerUserTLibRegistration")); if (enablePerUserTLibRegistrationProcPtr) { enablePerUserTLibRegistrationProcPtr(); } } #endif PerUserRegistration::~PerUserRegistration() { #if _ATL_VER < 0x0900 if (m_mapping) ::RegOverridePredefKey(HKEY_CLASSES_ROOT, NULL); #endif }
然后在ActiveX程序中使用:
// DllRegisterServer - 将项添加到系统注册表 STDAPI DllRegisterServer(void) { PerUserRegistration perUser(true); // 注册对象、类型库和类型库中的所有接口 HRESULT hr = _AtlModule.DllRegisterServer(); #ifdef _MERGE_PROXYSTUB if (FAILED(hr)) return hr; hr = PrxDllRegisterServer(); #endif return hr; } // DllUnregisterServer - 将项从系统注册表中移除 STDAPI DllUnregisterServer(void) { PerUserRegistration perUser(true); HRESULT hr = _AtlModule.DllUnregisterServer(); #ifdef _MERGE_PROXYSTUB if (FAILED(hr)) return hr; hr = PrxDllRegisterServer(); if (FAILED(hr)) return hr; hr = PrxDllUnregisterServer(); #endif return hr; }
这样就可以了,不是管理员也可以正常注册使用了。