delphi设置环境变量

来源:互联网 发布:403 forbidden nginx 编辑:程序博客网 时间:2024/06/17 13:32
{*********************************************}
{ Set Global Environment Function             }
{ Coder : Kingron,2002.8.6                    }
{ Bug Report : Kingron@163.net                }
{ Test OK For Windows 2000 Advance Server     }
{ Parameter:                                  }
{ Name : The environment name                 }
{ Value: The environment Value                }
{ Ex: SetGlobalEnvironment('MyVar','OK')      }
{*********************************************}
function SetGlobalEnvironment(const Name,Value:string):boolean;
const
 REG_LOCATION='System/CurrentControlSet/Control/Session Manager/Environment';
var
 R:DWORD;
begin
 with TRegistry.Create do
 try
   RootKey :=HKEY_LOCAL_MACHINE;
   Result :=OpenKey(REG_LOCATION,True);
   if Result then
   begin
     WriteString(Name,Value);
     SendMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,integer(Pchar('Environment')));
//      SendMessageTimeOut(HWND_BROADCAST,WM_SETTINGCHANGE,0,integer(Pchar('Environment')),SMTO_NORMAL,1000,R);
   end;
 finally
   Free;
 end;
end;