SetEnvironmentVariable???(100分)

  • 主题发起人 主题发起人 刘芳
  • 开始时间 开始时间

刘芳

Unregistered / Unconfirmed
GUEST, unregistred user!
想把自己应用程序的路径加到环境变量Path中,下面的语句没有报错,可就是不生效?
if (SetEnvironmentVariable('Path', PChar(ExtractFilePath(application.ExeName)))
<> true) then
showmessage(SysErrorMessage(GetLastError()));
 
你那个调用应该已经成功了。
SetEnvironmentVariable 设置的只是你当前应用的环境变量。
不信,你随后调用。
GetEnvironmentVariable 来看你的环境变量已经设置成了你的应用的。

var
v: Pchar;
begin
getMem(v,255);
try
if (SetEnvironmentVariable('Path', PChar(ExtractFilePath(application.ExeName)))
<> true) then
showmessage(SysErrorMessage(GetLastError()));
GetEnvironmentVariable('Path',v,255);
showmessage(v);
finally
freemem(v);
end;
end;
这是WIN32 Programmer's Reference 的说明的。
The SetEnvironmentVariable function sets the value of
an environment variable for the current process.

 
那我如何才能把它保存下来呢?
 
你的要求 ,我不知道怎么实现,,
在WIN32 Programmer's Reference里可以找到 SetEnvVariable 正是可以实现你的要求的
API,但是确不知道该引用哪个单元,,
在DFW里搜索到一个相关主题:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1136142
结果是,Delphi 不支持 SetEnvVariable , :)
另人失望。 你看看那个主题文章对你有没有帮助。。
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1242554
讨论得更详细一些。
 
procedure SetGlobalEnvironment(const Name, Value: string);
var
R:DWORD;
begin
with TRegistry.Create do
try
RootKey:=HKEY_LOCAL_MACHINE;
if OpenKey('System/CurrentControlSet/Control/Session Manager/Environment',True) then begin
WriteString(Name,Value);
SendMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,Integer(Pchar('Environment')));
end;
finally
Free
end;
end;
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1341056
 
谢谢 zhaqiong Oracle 全部搞定!
 

Similar threads

S
回复
0
查看
859
SUNSTONE的Delphi笔记
S
S
回复
0
查看
785
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
926
SUNSTONE的Delphi笔记
S
I
回复
0
查看
596
import
I
后退
顶部