0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UWSCで環境変数を読み書き、永続変数的な使い道も

Last updated at Posted at 2019-02-17

コード

envval.uws
print GetEnv("SystemRoot")      //環境変数"SystemRoot"を取得、多分"C:\WINDOWS"
print GetEnv("CUSTOM1")         //環境変数"CUSTOM1"値を取得、
                                // 実行1回目は存在しないので空、二回目は前回保持されたGETTIME値
SetEnv("CUSTOM1", GETTIME() )   //GETTIME値を環境変数"CUSTOM1"にセットする
doscmd("cmd /k echo !%CUSTOM1%!",false,true) //子プロセスに引き継がれる

//環境変数を取得
function GetEnv(name)
	DEF_DLL GetEnvironmentVariableA(string, var string, long ): long: Kernel32.dll

	result = empty
	len = GetEnvironmentVariableA(""+name,int(0),0) //必要バッファ長さ
	if len > 0
		buf = "                                                                 "//64文字
		for i=0 to int(power(len,-2))-6
			buf = buf + buf
		next
		len = GetEnvironmentVariableA(""+name,buf,len)
		result = COPY(buf,0,len)
	endif
fend

//環境変数をセット
function SetEnv(name,value)
	DEF_DLL SetEnvironmentVariableA(string, string): long: Kernel32.dll
	result = SetEnvironmentVariableA(""+name,""+value)
fend


応用

SETした場合、UWSCのプロセスが終了しない限り環境変数が保持されるはず。
このため、ランチメニューからスクリプトを起動するのであればPHPの$_SESSION変数のような感覚で利用できるかも。
スクリプトが終了しても消えない変数としての利用や、スクリプト間で環境変数を共有するなどの使いみちが考えられる。

補足

  • 環境変数セットの影響があるのはスクリプトを動かしたUWSCのプロセス、またはこれから起動されたサブプロセスのみ。システム環境変数はこの方法で変えることは出来ない。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?