[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 518
  • Last Modified:

how to Refresh explorer for take effect system polices

Hello Experts,

I have a problem, I write some registry values to apply system polices on windows, but for this polices
take effect I have reboot or kill explorer process and then the system launch again explorer and then
the system polices are present, my problem is that some pc don't launch again explorer, then
my question are if exists any form to launch explorer with parameters or other form to
force the explorer to take changes in system polices.

Thanks in advance.
0
MauricioGaviria
Asked:
MauricioGaviria
2 Solutions
 
HypoviaxCommented:
I would kill it then reload it manually (don't wait for the system to do it). Example:

function KillTask(ExeFileName: string): integer;
const
PROCESS_TERMINATE=$0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot
(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle,
FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(OpenProcess(
PROCESS_TERMINATE, BOOL(0),
FProcessEntry32.th32ProcessID), 0));
ContinueLoop := Process32Next(FSnapshotHandle,
FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;

procedure TForm6.ButKillClick(Sender: TObject);
begin
killtask('explorer.exe');
end;

procedure TForm6.ButReloadClick(Sender: TObject);
begin
shellexecute(handle,'Open',pchar('C:\Windows\explorer.exe'),'','',sw_show);
end;

Regards,

Hypoviax
0
 
amebikesCommented:
Have you tried to inform the explorer about the changes?

SendMessage(HWND_Broadcast, WM_SETTINGCHANGE, 0, 0);

Regards
  amebikes
0
 
MauricioGaviriaAuthor Commented:
Hi  Hypoviax ,

I don't launch Explorer manually because killer of explorer process it's do it from nt service then if launch
explorer process then this run with system privilege.

Thanks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MauricioGaviriaAuthor Commented:
Hi amebikes,

Your solution seems to be wthat  I need  accord with WM_SETTINGCHANGE doc, but I probe your solution
and don't take effect system polices, I think that 3 and 4 parameters can be different of 0.

0
 
amebikesCommented:
Hi MauricioGaviria,

I modified this procedure from BDN:

<pre>
procedure TForm1.Button2Click(Sender: TObject);
var
    lParam, wParam : Integer;   {Integers that indicate pointers to parameters}
    Buf     : Array[0..5] of Char; {Buffer used to indicate what setting we have changed.}
    aResult : Cardinal;         {Error Number returned from API Call}
begin
    {Now comes the interesting part.}
    {Environment is the section of global settings we want the system to update}
     Buf := 'Policy';
     wParam := 0;
     {This gives us a pointer to the Buffer for Windows to read.}
     lParam := Integer(@Buf[0]);

     {Here we make a call to SendMessageTimeout to Broadcast a message to the
     entire system telling every application (including explorer) to update
     its settings}
     SendMessageTimeout(HWND_BROADCAST ,
                        WM_SETTINGCHANGE ,
                        wParam,
                        lParam,
                        SMTO_NORMAL      ,
                        4000,
                        aResult);

     {Display windows lasterror if the result is an error.}
     if aResult <> 0 then
     begin
         showmessage(SysErrorMessage(aResult));
     end;
end;
</pre>

0
 
MauricioGaviriaAuthor Commented:
Hi  amebikes,

I write some polices on registry like ( "NoRun:Dword=1" and "NoFind:Dword=1" on HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\
Explorer ) this polices hide Run and Find options on windows start menu, I use your function to send broadcast message then all windows refresh, like
regedit windows refresh, the explorer refresh but the polices don't take effect, I probe change Buf := 'Policy' to  Buf := 'Software\Microsoft\Windows\CurrentVersion\Policies\Explorer'  but nothing, I don't sure of correct value of Buf.

Please helpme amebikes. thanks.
0
 
MauricioGaviriaAuthor Commented:
Hi amebikes,

I found this
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_10356417.html?query=WM_SETTINGCHANGE&clearTAFilter=true 

and the solution are same that you send me, but don't understand why not funciona operate.
0
 
amebikesCommented:
Hi,
I took the code from Borland.

I changed the pointer and now it seems to work :-)

<pre>
procedure TfmRunAsEx_Main.btnSendMessageClick(Sender: TObject);
var
    lParam, wParam : Integer;   {Integers that indicate pointers to parameters}
    aResult : Cardinal;         {Error Number returned from API Call}
    s : String;
begin
  with TRegistry.Create do
  begin
    if OpenKey('Software\Microsoft\Windows\CurrentVersion\Policies\Explorer', True) then
    begin
      WriteInteger('NoRun', 1);
      WriteInteger('NoFind', 1);
      CloseKey;
    end;
    Free;
  end;

  wParam := 0;
  {This gives us a pointer to the Buffer for Windows to read.}
  s := 'Policy';
  lParam := Integer(@s[1]);
  SendMessageTimeout(HWND_BROADCAST ,
                    WM_SETTINGCHANGE ,
                    wParam,
                    lParam,
                    SMTO_NORMAL     ,
                    4000,
                    aResult);

  {Display windows lasterror if the result is an error.}
  if aResult <> 0 then
  begin
     showmessage(SysErrorMessage(aResult));
  end;
end;
</pre>

regards
0
 
MadshiCommented:
You can try this:

var c1 : dword;
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, integer(pchar('Policy')), SMTO_NORMAL or SMTO_ABORTIFHUNG, 5000, c1);

If it doesn't work there's no other way than to restart the explorer. But there's a much better way than to use TerminateProcess. Actually the Explorer is one of the few processes which listen to external shutdown requests - you just need to know how to tell the Explorer to close. Here's how:

PostMessage(FindWindow('Progman', 'Program Manager'), WM_QUIT, 0, 0);

Caution: This works only for the explorer. Most other processes don't react to this message. If you execute this, the explorer will friendly shut down and not restart again. Afterwards you manually need to start the explorer again.
0
 
MauricioGaviriaAuthor Commented:
Thanks amebikes and  Madshi. :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now