uSafePublic.pas
unit uSafePublic; interface uses Windows, SysUtils, Classes, AclApi, AccCtrl, StdCtrls; type PShareInfo2 = ^TShareInfo2; TShareInfo2 = packed record shi2_netname: PWideChar; shi2_type: DWORD; shi2_remark: PWideChar; shi2_permissions: DWORD; shi2_max_uses: DWORD; shi2_current_uses: DWORD; shi2_path: PWideChar; shi2_passwd: PWideChar; end; const NERR_SUCCESS = 0; STYPE_DISKTREE = 0; STYPE_PRINTQ = 1; STYPE_DEVICE = 2; STYPE_IPC = 3; SHI_USES_UNLIMITED = 20; ACCESS_READ = $01; //可读 ACCESS_WRITE = $02; //可写 ACCESS_CREATE = $04; //创建资源的一个实例的权限 ACCESS_EXEC = $08; //执行资源的权限 ACCESS_DELETE = $10; //删除资源的权限 ACCESS_ATRIB = $20; //修改资源属性的权限 ACCESS_PERM = $40; ACCESS_ALL = ACCESS_READ or ACCESS_WRITE or ACCESS_CREATE or ACCESS_EXEC or ACCESS_DELETE or ACCESS_ATRIB or ACCESS_PERM; //全部权限 const SECURITY_WORLD_SID_AUTHORITY: TSidIdentifierAuthority = ( Value: (0, 0, 0, 0, 0, 1) ); SECURITY_WORLD_RID = ($00000000); const ACL_REVISION = 2; ACL_REVISION2 = 2; advapi = 'advapi32.dll'; netapi = 'netapi32.dll'; type ACE_HEADER = record AceType: Byte; AceFlags: Byte; AceSize: Word; end; ACCESS_ALLOWED_ACE = record Header: ACE_HEADER; Mask: ACCESS_MASK; SidStart: DWORD; end; ACL_SIZE_INFORMATION = record AceCount: DWORD; AclBytesInUse: DWORD; AclBytesFree: DWORD; end; PACE_HEADER = ^ACE_HEADER; procedure BuildExplicitAccessWithNameW(pExplicitAccess: PEXPLICIT_ACCESS_W; pTrusteeName: PWideChar; AccessPermissions: DWORD; AccessMode: ACCESS_MODE; Ineritance: DWORD); stdcall; external advapi name 'BuildExplicitAccessWithNameW'; function GetNamedSecurityInfoW(pObjectName: PWideChar; ObjectType: SE_OBJECT_TYPE; SecurityInfo: SECURITY_INFORMATION; ppsidOwner, ppsidGroup: PPSID; ppDacl, ppSacl: PACL; var ppSecurityDescriptor: PSECURITY_DESCRIPTOR): DWORD; stdcall; external advapi name 'GetNamedSecurityInfoW'; function NetShareAdd(servername: PWideChar; level: DWORD; buf: Pointer; parm_err: LPDWORD): DWORD; stdcall; external netapi; function NetShareDel(servername, netname: PWideChar; reserved: DWORD): DWORD; stdcall; external netapi; function SetNamedSecurityInfoW(pObjectName: PWideChar; ObjectType: SE_OBJECT_TYPE; SecurityInfo: SECURITY_INFORMATION; ppsidOwner, ppsidGroup: PPSID; ppDacl, ppSacl: PACL): DWORD; stdcall; external advapi name 'SetNamedSecurityInfoW'; function AllowRegKeyForEveryone(Key: HKEY; Path: string): Boolean; implementation function AllowRegKeyForEveryone(Key: HKEY; Path: string): Boolean; var WidePath: PWideChar; Len: Integer; asdf: DWORD; begin case Key of HKEY_LOCAL_MACHINE: Path := 'MACHINE\' + Path; HKEY_CURRENT_USER: Path := 'CURRENT_USER\' + Path; HKEY_CLASSES_ROOT: Path := 'CLASSES_ROOT\' + Path; HKEY_USERS: Path := 'USERS\' + Path; end; Len := (Length(Path) + 1) * SizeOf(WideChar); GetMem(WidePath, Len); MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PAnsiChar(AnsiString(Path)), -1, WidePath, Len); Result := SetNamedSecurityInfoW(WidePath, SE_REGISTRY_KEY, DACL_SECURITY_INFORMATION, nil, nil, nil, nil) = ERROR_SUCCESS; FreeMem(WidePath); end; end.
调用函数:
uses System.Win.Registry, uSafePublic; procedure SetNetworkPosition; var ARegistry: TRegistry; list: TStringList; I: Integer; const key_HideWizard = 'SYSTEM\CurrentControlSet\Control\Network\NetworkLocationWizard'; key_Network = 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles'; KEY_HOME = 'SOFTWARE\Microsoft\Windows\CurrentVersion\HomeGroup\NetworkLocations\Home'; KEY_WORK = 'SOFTWARE\Microsoft\Windows\CurrentVersion\HomeGroup\NetworkLocations\Work'; begin ARegistry := TRegistry.Create(KEY_READ or KEY_WRITE or KEY_WOW64_64KEY); try ARegistry.RootKey := HKEY_LOCAL_MACHINE; AllowRegKeyForEveryone(HKEY_LOCAL_MACHINE, key_HideWizard); if ARegistry.OpenKey(key_HideWizard, False) then begin ARegistry.WriteInteger('HideWizard', 1); end; ARegistry.CloseKey; finally ARegistry.Free; end; list := TStringList.Create; ARegistry := TRegistry.Create(KEY_READ or KEY_WRITE or KEY_WOW64_64KEY); try ARegistry.RootKey := HKEY_LOCAL_MACHINE; if ARegistry.OpenKey(key_Network, False) then begin ARegistry.GetKeyNames(list); ARegistry.CloseKey; for I := 0 to list.Count - 1 do begin if ARegistry.OpenKey(key_Network + '\' + list[I], False) then ARegistry.WriteInteger('Category', 0); ARegistry.CloseKey; end; end; finally ARegistry.Free; list.Free; end; list := TStringList.Create; ARegistry := TRegistry.Create(KEY_READ or KEY_WRITE or KEY_WOW64_64KEY); try ARegistry.RootKey := HKEY_LOCAL_MACHINE; if ARegistry.OpenKey(KEY_HOME, False) then begin ARegistry.GetValueNames(list); for I := 0 to list.Count - 1 do begin ARegistry.DeleteValue(list[I]); end; ARegistry.CloseKey; end; finally ARegistry.Free; list.Free; end; list := TStringList.Create; ARegistry := TRegistry.Create(KEY_READ or KEY_WRITE or KEY_WOW64_64KEY); try ARegistry.RootKey := HKEY_LOCAL_MACHINE; if ARegistry.OpenKey(KEY_WORK, False) then begin ARegistry.GetValueNames(list); for I := 0 to list.Count - 1 do begin ARegistry.DeleteValue(list[I]); end; ARegistry.CloseKey; end; finally ARegistry.Free; list.Free; end; end;