Link to home
Start Free TrialLog in
Avatar of Narusegawa
Narusegawa

asked on

ExpandEnvironmentStringsForUser

Has anyone got an example of using this API in Delphi? I'm trying to convert a registry returned string into a full normal file path. But it contains env variables that need to be expanded. This API seems the best way, but I'm not sure how to write a function that I can pass this string to and get an expanded string back. Thanks
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

Can you give an example about that ?
It seems a little bit unclear for me ....
What is this API ? What is the registry returned string ? What is an expanded string ?
Avatar of Narusegawa
Narusegawa

ASKER

I found the API at  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/policy/policy/user_profiles_functions.asp

I have "%USERPROFILE%\My Documents\My Themes\Cursors\Premium 1.5\Premium_link.cur" as a string and I need this converting to a fully qualified file path. However for future and multi-user ease of use I will need to take into account any possible variables like %variable_name% that a user may have set up in there system.

The API, according to the document in the link, will convert these for me. However I have no idea how to do this. I've seen examples of other API called in Delphi but they are use DWord and PChars which I'm NOT very experienced with at all.
ASKER CERTIFIED SOLUTION
Avatar of gary_williams
gary_williams

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect! That works a treat! Is that pretty much the same way to call other API functions in userenv.dll for example?

All this just to get around the hyperlink cursor not working properly in Windows XP! crHandPoint doesn't work. So had to get the cursor path from registry, then use this to expand it properly, then load it manually into a global variable to be used.
Gary has it, but it can be a bit shorted out :-)

procedure TForm1.Button1Click(Sender: TObject);
var
 Buffer: array[0..MAX_PATH] of Char;
begin
 ExpandEnvironmentStrings(PChar(Edit1.Text), Buffer, MAX_PATH);
 Edit2.Text :=Buffer;
end;

which essentially does the same. I don't know why gary_williams uses SetEnvironmentVariable though...

Evarest
My apologies: I created a "huge" memoryleak ;-)

Solution:

procedure TForm1.Button1Click(Sender: TObject);
var
 Buffer: array[0..MAX_PATH] of Char;
begin
 ExpandEnvironmentStrings(PChar(Edit1.Text), Buffer, MAX_PATH);
 Edit2.Text :=Buffer;
 Finalize(Buffer); // <-- changed
end;

Evarest
Other Windows API functions that require a buffer tend to be callable in the same way, however I believe there are some variations.
The poster asked for an example of how to write a function that called ExpandEnvironmentStrings, so I provided a "button click" procedure that demonstrated its us; I called SetEnvironmentVariable just to prove that it works.
The call to Finalize is not needed.
What is the MAX_PATH thing for?

Thanks both of you! You solved a problem I was about to write a huge function to get around. I thought I was gonna have to parse for any %name% in the path and then check each one for a env variable and then strreplace each one.

Is there a document somewhere where you find all this API stuff? Preferably for the begginer/intermediate level. I been using Delphi for years but mostly it's database stuff not API/DLL.
MAX_PATH is a Windows constant representing the maximum length for any path on the system.  Generally I define TBuffer to be at least as long as this.  For simplicity, I use TBuffer in all functions that call API functions requiring a buffer for short-to-medium-length strings.
"The call to Finalize is not needed."

Oops, beg to differ :-)

Set the call in a TTimerOnTimer and set interval to 50 msec. Wait some seconds and see the RAM your program uses fill (however slowly)...

To Narusegawa:
You can find info on all API calls at msdn.microsoft.com or a more limited version in the helpfiles of Delphi: Start|Programs|...|Delphi7|Help|MS SDK Help Files|Win32 SDK Reference

NOTE: always depending on raw api can limit you application OS support.

Evarest

"Finalize" is documented to operate on "dynamically allocated variables".

A fixed-size array is not a dynamically allocated variable.

I would venture a guess this is not a true memory leak, but an artifact of how the memory use is being measured.
It seems you're correct... One needs to be able to admit one's mistakes :-)

Evarest
I notice the memory usage tends to increase regardless of the presence or absense of the call to Finalize (when the function using the buffer is called from a short-period timer).  Who knows where that extra bloat is coming from.
It's sometimes just horrible... WMI is also an example of poor memorymanagement. You just can't use it in any timer or monitoring system, as i eats up your memory...

Evarest