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
NarusegawaAsked:
Who is Participating?
 
gary_williamsConnect With a Mentor Commented:
type
  TBuffer = array[0..MAX_PATH] of Char;

function ExpandEnvironmentString(const S: String): String;
var
  Buffer: TBuffer;
begin
  ExpandEnvironmentStrings(PChar(S), Buffer, SizeOf(Buffer));
  Result := Buffer;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetEnvironmentVariable('A', 'Blah');
  ShowMessage(ExpandEnvironmentString('%A% %A%')); // BlahBlah
end;
0
 
esoftbgCommented:
Can you give an example about that ?
0
 
esoftbgCommented:
It seems a little bit unclear for me ....
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
esoftbgCommented:
What is this API ? What is the registry returned string ? What is an expanded string ?
0
 
NarusegawaAuthor Commented:
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.
0
 
NarusegawaAuthor Commented:
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.
0
 
EvarestCommented:
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
0
 
EvarestCommented:
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
0
 
gary_williamsCommented:
Other Windows API functions that require a buffer tend to be callable in the same way, however I believe there are some variations.
0
 
gary_williamsCommented:
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.
0
 
gary_williamsCommented:
The call to Finalize is not needed.
0
 
NarusegawaAuthor Commented:
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.
0
 
gary_williamsCommented:
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.
0
 
EvarestCommented:
"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

0
 
gary_williamsCommented:
"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.
0
 
EvarestCommented:
It seems you're correct... One needs to be able to admit one's mistakes :-)

Evarest
0
 
gary_williamsCommented:
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.
0
 
EvarestCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.