Sunday, February 6, 2011

Reload Group policy using gpupdate and WM_SETTINGCHANGE

When we change any of the group policy setting using registry then applications does not get notified about the change of policy and the effect take place only when the application gets restart or the user logs off and login again. To avoid restarting application and user logoff we have to notify all application about the change in group policy and so the application will reload the group policies. The applications can be notifiied about the change by using anyone of the below given approach

1. Using gpupdate.exe We can reload group policies by executing below command at command prompt.
gpupdate.exe /Force

2. Using WM_SETTINGCHANGE Message The group policies can be reloaded programatically by broadcasting WM_SETTINGCHANGE message using SendNotifyMessage. SendNotifyMessage take following paramters

hwnd: HWND_BROADCAST = 0xFFFF
msg: WM_SETTINGCHANGE = 0x001A
wParam: 0 when user group policies are applied and 1 when reload machie level group policies are applied.
lParam: pointer to string "Policy".

Here is an implmentation of same using C#.NET 

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg, IntPtr wParam,

private void reloadPolicies()
{
   IntPtr hwnd = new IntPtr(0xFFFF);
   IntPtr wparam = new IntPtr(0);
   IntPtr lparam = Marshal.StringToHGlobalUni("Policy");
   SendNotifyMessage(hwnd, 0x1A, wparam, lparam);
   System.Threading.Thread.Sleep(1500);
   Marshal.FreeHGlobal(lparam); 
}

1 comment:

Anonymous said...

man i lock ed drive and now i cant unlock what to d?