const GPO_OPEN_LOAD_REGISTRY = $00000001; // Load the registry files
{$EXTERNALSYM GPO_OPEN_LOAD_REGISTRY} GPO_OPEN_READ_ONLY = $00000002; // Open the GPO as read only
type HPROPSHEETPAGE = Pointer; {$EXTERNALSYM HPROPSHEETPAGE} PHPROPSHEETPAGE = ^HPROPSHEETPAGE;
LPOLESTR = POleStr;
_GROUP_POLICY_OBJECT_TYPE = (GPOTypeLocal, // GPO on the local machine GPOTypeRemote, // GPO on a remote machine GPOTypeDS); // GPO in the Active Directory
{$EXTERNALSYM _GROUP_POLICY_OBJECT_TYPE} GROUP_POLICY_OBJECT_TYPE = _GROUP_POLICY_OBJECT_TYPE;
function New(pszDomainName, pszDisplayName: LPOLESTR; dwFlags: DWORD): HRESULT; stdcall; function OpenDSGPO(pszPath: LPOLESTR; dwFlags: DWORD): HRESULT; stdcall; function OpenLocalMachineGPO(dwFlags: DWORD): HRESULT; stdcall; function OpenRemoteMachineGPO(pszComputerName: LPOLESTR; dwFlags: DWORD): HRESULT; stdcall; function Save(bMachine, bAdd: BOOL; const pGuidExtension, pGuid: TGUID): HRESULT; stdcall; function Delete: HRESULT; stdcall; function GetName(pszName: LPOLESTR; cchMaxLength: Integer): HRESULT; stdcall; function GetDisplayName(pszName: LPOLESTR; cchMaxLength: Integer): HRESULT; stdcall; function SetDisplayName(pszName: LPOLESTR): HRESULT; stdcall; function GetPath(pszPath: LPOLESTR; cchMaxPath: Integer): HRESULT; stdcall; function GetDSPath(dwSection: DWORD; pszPath: LPOLESTR; cchMaxPath: Integer): HRESULT; stdcall; function GetFileSysPath(dwSection: DWORD; pszPath: LPOLESTR;
cchMaxPath: Integer): HRESULT; stdcall;
//
// Returns a registry key handle for the requested section. The returned
// key is the root of the registry, not the Policies subkey. To set / read
// a value in the Policies subkey, you will need to call RegOpenKeyEx to
// open Software\Policies subkey first.
//
// The handle has been opened with ALL ACCESS rights. Call RegCloseKey
// on the handle when finished.
//
// If the GPO was loaded / created without the registry being loaded
// this method will return E_FAIL.
//
// dwSection is either GPO_SECTION_USER or GPO_SECTION_MACHINE
// hKey contains the registry key on return
//
function GetRegistryKey(dwSection: DWORD; var hKey: HKEY): HRESULT; stdcall; function GetOptions(var dwOptions: DWORD): HRESULT; stdcall; function SetOptions(dwOptions, dwMask: DWORD): HRESULT; stdcall; function GetType(var gpoType: GROUP_POLICY_OBJECT_TYPE): HRESULT; stdcall; function GetMachineName(pszName: LPOLESTR; cchMaxLength: Integer): HRESULT; stdcall; function GetPropertySheetPages(var hPages: PHPROPSHEETPAGE; var uPageCount: UINT): HRESULT; stdcall; end;
procedure TForm1.Button1Click(Sender: TObject); var GPO: IGroupPolicyObject;
Key: HKEY; begin GPO := CreateComObject(IID_GPO) as IGroupPolicyObject; if GPO.OpenLocalMachineGPO(GPO_OPEN_LOAD_REGISTRY) = S_OK then
begin
if GPO.GetRegistryKey(GPO_SECTION_USER, Key) = S_OK then
with TRegistry.Create do
try RootKey := HKEY_CURRENT_USER; if OpenKey('Software\Microsoft\Windows\CurrentVersion\Policies\Explorer', True) then
begin WriteInteger('StartMenuLogOff', 0); end;
RootKey := Key; if OpenKey('\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer', True) then
begin WriteInteger('StartMenuLogOff', 0); end;
RegCloseKey(Key);
GPO.Save(False, True, REGISTRY_EXTENSION_GUID, CLSID_GPESnapIn); finally Free; end;
SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, Integer(PChar('Polices')), 0); end; end;