winutils.pp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2009 by the Free Pascal development team
  4. Misc windows utility functions
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}{$H+}
  12. {$IFNDEF FPC_DOTTEDUNITS}
  13. unit WinUtils;
  14. {$ENDIF FPC_DOTTEDUNITS}
  15. Interface
  16. {$IFDEF FPC_DOTTEDUNITS}
  17. Uses WinApi.Windows, WinApi.Comobj, WinApi.Activex;
  18. {$ELSE FPC_DOTTEDUNITS}
  19. Uses Windows, ComObj, ActiveX;
  20. {$ENDIF FPC_DOTTEDUNITS}
  21. // returns True if the currently logged Windows user has Administrator rights. Delphi.about.com
  22. // From Delphi.about.com with permission, http://delphi.about.com/od/delphitips2007/qt/is_win_admin.htm
  23. function IsWindowsAdmin: Boolean;
  24. // Removes Browsers "downloaded" attribute from a file.
  25. procedure UnBlockFile(const name:String);
  26. const
  27. NET_FW_PROFILE2_DOMAIN = 1;
  28. NET_FW_PROFILE2_PRIVATE = 2;
  29. NET_FW_PROFILE2_PUBLIC = 4;
  30. NET_FW_IP_PROTOCOL_TCP = 6;
  31. NET_FW_IP_PROTOCOL_UDP = 17;
  32. NET_FW_ACTION_ALLOW = 1;
  33. // add firewall rule e.g.
  34. // AddProgramExceptionToFireWall( Application.Title,Application.Title, Application.ExeName, NET_FW_IP_PROTOCOL_TCP, NET_FW_PROFILE2_DOMAIN or NET_FW_PROFILE2_PRIVATE or NET_FW_PROFILE2_PUBLIC);
  35. procedure AddProgramExceptionToFireWall(Const wsCaption, wsDescription, wsExecutable: WideString; iProtocol,iProfile:Integer);
  36. // remove firewall rule, e.g. RemoveExceptionFromFW(Application.Title);
  37. procedure RemoveExceptionFromFW(Const exCaption: WideString);
  38. implementation
  39. const
  40. SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority = (Value: (0, 0, 0, 0, 0, 5)) ;
  41. const
  42. SECURITY_BUILTIN_DOMAIN_RID = $00000020;
  43. DOMAIN_ALIAS_RID_ADMINS = $00000220;
  44. function IsWindowsAdmin: Boolean;
  45. var
  46. hAccessToken: THandle;
  47. ptgGroups: PTokenGroups;
  48. dwInfoBufferSize: DWORD;
  49. psidAdministrators: PSID;
  50. g: Integer;
  51. bSuccess: BOOL;
  52. begin
  53. Result := False;
  54. bSuccess := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True, hAccessToken) ;
  55. if not bSuccess then
  56. begin
  57. if GetLastError = ERROR_NO_TOKEN then
  58. bSuccess := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hAccessToken) ;
  59. end;
  60. if bSuccess then
  61. begin
  62. GetMem(ptgGroups, 1024) ;
  63. bSuccess := GetTokenInformation(hAccessToken, TokenGroups, ptgGroups, 1024, dwInfoBufferSize) ;
  64. CloseHandle(hAccessToken) ;
  65. if bSuccess then
  66. begin
  67. AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, psidAdministrators) ;
  68. for g := 0 to ptgGroups^.GroupCount - 1 do
  69. if EqualSid(psidAdministrators, ptgGroups^.Groups[g].Sid) then
  70. begin
  71. Result := True;
  72. Break;
  73. end;
  74. FreeSid(psidAdministrators) ;
  75. end;
  76. FreeMem(ptgGroups) ;
  77. end;
  78. end;
  79. procedure UnBlockFile(const name:String);
  80. var f : file;
  81. begin
  82. assignfile(f,name+':Zone.Identifier');
  83. rewrite(f,1);
  84. truncate(f);
  85. closefile(f);
  86. end;
  87. procedure AddProgramExceptionToFireWall(Const wsCaption, wsDescription, wsExecutable: WideString; iProtocol, iProfile:Integer);
  88. var
  89. fwPolicy2 : OleVariant;
  90. RulesObject : OleVariant;
  91. NewRule : OleVariant;
  92. begin
  93. fwPolicy2 := CreateOleObject('HNetCfg.FwPolicy2');
  94. RulesObject := fwPolicy2.Rules;
  95. NewRule := CreateOleObject('HNetCfg.FWRule');
  96. NewRule.Name := wsCaption;
  97. NewRule.Description := wsDescription;
  98. NewRule.Applicationname := wsExecutable;
  99. NewRule.Protocol := iProtocol;
  100. NewRule.Enabled := TRUE;
  101. NewRule.Profiles := iProfile;
  102. NewRule.Action := NET_FW_ACTION_ALLOW;
  103. RulesObject.Add(NewRule);
  104. end;
  105. procedure RemoveExceptionFromFW(Const exCaption: WideString);
  106. var
  107. fwPolicy2 : OleVariant;
  108. begin
  109. fwPolicy2 := CreateOleObject('HNetCfg.FwPolicy2');
  110. fwPolicy2.Rules.Remove(exCaption);
  111. end;
  112. end.