ActiveX 控制項 (.ocx) 本質是 COM 伺服器 DLL,必須在系統註冊表寫入 CLSID、ProgID、TypeLib、InprocServer32 等鍵值,容器 (VB、VC、BCB、IE、Office) 才能發現並載入。
rem 註冊
regsvr32.exe "C:\Path\MyControl.ocx"
rem 解除註冊
regsvr32.exe /u "C:\Path\MyControl.ocx"
rem 靜默模式 (無訊息框,適合安裝腳本)
regsvr32.exe /s "C:\Path\MyControl.ocx"
%windir%\System32\regsvr32.exe / SysWOW64\regsvr32.exe for 32-bit on 64-bit)。DllRegisterServer / DllUnregisterServer 導出函數。.ocx 檔案。.NET COM Interop 選項)。DllRegisterServer;解除安裝自動呼叫 DllUnregisterServer。[Files]
Source: "MyControl.ocx"; DestDir: "{sys}"; Flags: regserver uninsrestartdelete
; regserver = 安裝時註冊 (呼叫 DllRegisterServer)
; uninsrestartdelete = 解除安裝時解除註冊後刪除檔案 (需重啟)
Section "ActiveX Control"
SetOutPath "$SYSDIR"
File "MyControl.ocx"
RegDLL "$SYSDIR\MyControl.ocx" ; 註冊
SectionEnd
Section -Uninstall
ExecWait '"$SYSDIR\regsvr32.exe" /u /s "$SYSDIR\MyControl.ocx"'
SectionEnd
// C++ (Windows API)
bool RegisterActiveX(const char* ocxPath) {
HMODULE h = LoadLibrary(ocxPath);
if (!h) return false;
typedef HRESULT (__stdcall *PFN_DLLREG)();
PFN_DLLREG pfn = (PFN_DLLREG)GetProcAddress(h, "DllRegisterServer");
if (!pfn) { FreeLibrary(h); return false; }
HRESULT hr = pfn();
FreeLibrary(h);
return SUCCEEDED(hr);
}
// 使用範例 (啟動時)
if (!IsActiveXRegistered("MyControl.ocx")) {
if (RegisterActiveX(GetSysDir() + "\\MyControl.ocx"))
MessageBox("ActiveX 元件已自動註冊,請重新啟動程式。");
}
HKCU\Software\Classes\CLSID (需控制項支援 Per-User 註冊)。HKLM 需管理員權限,Vista+ 需 UAC 提權。.ocx) 必須用 32-bit regsvr32 (%windir%\SysWOW64\regsvr32.exe) 註冊,寫入 HKLM\Software\Wow6432Node\Classes\CLSID。regsvr32 /s /n /i:MyControl.ocx (僅驗證,不修改)。HKEY_CLASSES_ROOT\CLSID\{...}、HKEY_CLASSES_ROOT\MyControl.ProgID。OLEVIEW (Windows SDK 工具) 瀏覽類別資訊。