WinApi Question (GetEnvironmentVariable)

Blitz3D Forums/Blitz3D Programming/WinApi Question (GetEnvironmentVariable)

maverick69(Posted 2004) [#1]
I want to use the GetEnvironmentVariable Function. I have found decls on this page to use the functions, but I have one Problem. First, some example source

api_SetEnvironmentVariable ("TEST", "Value")
len = api_GetEnvironmentVariable ("TEST", c$, 255) 
print c$
waitkey
end


c$ ist always empty. How can I read out the Variable into a Blitz String?


Binary_Moon(Posted 2004) [#2]
Why are you using userlibs?

http://www.blitzbasic.com/b3ddocs/command.php?name=GetEnv

Surely that would do what you need?

To answer your question (note I'm not an expert with userlibs so I could be totally wrong). You can't pass a varible (c$) the way you are. That value would have to be the handle of a bank which you would then convert into a string (by looping through all the bytes returned and using their ascii code to work out the characters)


maverick69(Posted 2004) [#3]
The Blitz commands seems not to get them. I use the Armadillo-Software to protect my software. After I protected my blitz-code with Armadillo I have access to some ENVIRONEMENT Variables, but I don't get them with the Blitz GetEnv Command. When I use the Win32-Api Function I get back the correct length of the string/value, but I don't know how to read out the string/value correctly.

I've also tried to use a bank, but as im not very experienced with it i'm probably doing something wrong. Here is my code:

api_SetEnvironmentVariable ("TEST", "Value")

cbank = CreateBank(256)
api_GetEnvironmentVariable ("TEST", cbank, 255) 

For i=0 To 255	
	S$=S$+Str(PeekByte (cbank,i))+"|"
	If i Mod 20 = 0 Then Print "STRING: "+s$ : s$=""
Next
Print "STRING: "+s$

WaitKey
End



Jeroen(Posted 2004) [#4]
good question. I need this too, to distinct a "demo" and "full" version


BlitzSupport(Posted 2004) [#5]
Try Win32's ExpandEnvironmentStrings -- GetEnvironmentVariable is actually for environment variables specific to the calling process as I understand it (no, I don't know what use that is either).


maverick69(Posted 2004) [#6]
okay, just want to let you guys know that i solved the problem. (with the help of the search function on this board) :-) here's the source:

Function GetEnvVar$(VariableName$)
	TempBank = CreateBank(1)
	size = api_GetEnvironmentVariable%(VariableName$,TempBank,1)
	If size > 0 Then
		ResizeBank TempBank,Size+1
		api_GetEnvironmentVariable%(VariableName$,TempBank,Size+1)
	Else
		Return ""
	End If

	retstring$ = ""
	For t = 0 To BankSize(tempbank)-3
		retstring$ = retstring$ + Chr(PeekByte(tempbank,t))
	Next
		
	FreeBank tempbank
	Return retstring$
End Function



maverick69(Posted 2004) [#7]
oh, i did forgot something:

the definition in my kernel32.decls was also wrong:

api_GetEnvironmentVariable% (lpName$, lpBuffer*, nSize%) : "GetEnvironmentVariableA" ;right version
api_GetEnvironmentVariable% (lpName$, lpBuffer$, nSize%) : "GetEnvironmentVariableA" ;wrong version



Jay Kyburz(Posted 2005) [#8]
Just wanted to say thankyou Maverick. Comming back and posting your solution just saved me allot of time and pain. I'm using this code to interact with Armadillo.


TartanTangerine (was Indiepath)(Posted 2005) [#9]
Thanks for this, I was getting such a headache trying to get those pesky env variables.