VC修改组策略

来源:互联网 发布:java 汉字转utf8编码 编辑:程序博客网 时间:2024/06/08 02:31

在组策略种设置的升级服务器,如下图

现在升级服务器地址变了,不能每一台机器都去改,网上查找一番,多数方法是直接修改注册表,测试过之后,发现直接修改注册表是不能生效的,要用IGroupPolicyObject修改才行。

  


#define   INITGUID

  #include <windows.h>
  #include <Guiddef.h>
  #include <Gpedit.h>
  #define GPO_UPDATE_REG "SOFTWARE\\Policies\\Microsoft\\Windows\\WindowsUpdate"
  BOOL SetGroup();
 int main()
 {
   CoInitialize(NULL);
   SetGroup();
   CoUninitialize();
   return 0;
 
 }
 
 BOOL SetGroup()
 {   
   HKEY ghoKey, ghoSubKey, hSubKey;
   IGroupPolicyObject *pGPO = NULL;
   CoCreateInstance(CLSID_GroupPolicyObject, NULL, CLSCTX_ALL,IID_IGroupPolicyObject, (LPVOID*)&pGPO);
   RegCreateKeyEx(HKEY_LOCAL_MACHINE, GPO_UPDATE_REG, 0,NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hSubKey, NULL);
   RegSetValueEx(hSubKey,"WUServer", 0, REG_SZ, (const BYTE *)"http://192.168.1.33", strlen((char*)"http://192.168.1.33") + 1);
   RegSetValueEx(hSubKey,"WUStatusServer", 0, REG_SZ, (const BYTE *)"http://192.168.1.33", strlen((char*)"http://192.168.1.33") + 1);
   pGPO->OpenLocalMachineGPO(GPO_OPEN_LOAD_REGISTRY);
   pGPO->GetRegistryKey(GPO_SECTION_MACHINE,&ghoKey);
   RegCreateKeyEx(ghoKey, GPO_UPDATE_REG, 0,NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &ghoSubKey, NULL);
   RegSetValueEx(ghoSubKey,"WUServer", 0, REG_SZ, (const BYTE *)"http://192.168.1.33", strlen((char*)"http://192.168.1.33") + 1);
   RegSetValueEx(ghoSubKey,"WUStatusServer", 0, REG_SZ, (const BYTE *)"http://192.168.1.33", strlen((char*)"http://192.168.1.33") + 1);
   GUID RegistryId = REGISTRY_EXTENSION_GUID;
  //save method 的第一个参数一定要True
  pGPO->Save(TRUE,TRUE,const_cast<GUID*>(&RegistryId),const_cast<GUID*>(&CLSID_GPESnapIn));
  pGPO->Release();
  RegCloseKey(ghoKey);
  RegCloseKey(ghoSubKey);
  return TRUE;

 }

网上比较全的是这篇文章:http://blogs.msdn.com/b/dsadsi/archive/2009/07/23/working-with-group-policy-objects-programmatically-simple-c-example-illustrating-how-to-modify-a-registry-based-policy.aspx
0 0
原创粉丝点击