Win32::API and SetEnvironmentVariable

S

Shawn Campbell

I'm having a problem setting environment variables with a large
string. According to the MSDN documentation, an environment variable
can be up to 32k. I'm well under this value, but it's not being set.

Following is the code I'm using.

sub SetEnvVar
{
my($Variable)=shift(@_);
my($VarValue)=shift(@_);

# Load the function from the KERNEL32 library. If we can't, let it
be
# known...
#
$Win32_SetEnvVar = new Win32::API("kernel32",
"SetEnvironmentVariable", [P, P], N);

if (!(defined $Win32_SetEnvVar))
{
&StatusOut ("Unable to load SetEnvironmentVariable!\n");
exit;
}

# Now let's make the call..
#
$rtn = $Win32_SetEnvVar->Call($Variable, $VarValue);

return;
}


To test this I used a call to get the PATH variable and then write the
path value to a new environment variable (FOO) and then did a
GetEnvVar of FOO. This worked and I received the entire value back.

Then I doubled the size of the path value ($Val="$Val;$Val") and wrote
it out to FOO and did a GetEnvVar of FOO again. The second time it
failed to write out the path value and I received nothing back.

Is there some max limit on what Win32::API allows to be passed into
it?

-Shawn
 
B

Ben Morrow

Quoth (e-mail address removed) (Shawn Campbell):
I'm having a problem setting environment variables with a large
string. According to the MSDN documentation, an environment variable
can be up to 32k. I'm well under this value, but it's not being set.

Following is the code I'm using.

<snip Win32::API SetEnvironmentVar>

Why do you not just use %ENV? (Is there something I'm missing?)

Ben
 
E

Eric Bohlman

Quoth (e-mail address removed) (Shawn Campbell):

<snip Win32::API SetEnvironmentVar>

Why do you not just use %ENV? (Is there something I'm missing?)

I presume he's doing it for the reasons listed by perldoc -q environment
 
S

Shawn Campbell

Eric Bohlman said:
I presume he's doing it for the reasons listed by perldoc -q environment

Actually, it looks like the SetEnvVar sub I created is working fine,
other than truncating the value at 1024. The problem was in the
GetEnvVar sub I created. I didn't supply enough buffer size so it
wasn't reading in the value, giving me the impression it was not set.

I have another environment variable issue I am struggling with that I
hope you could give me some insight on.

I'm using ActivePerl to create an automated build process. Part of
the process is setting the LIB, INCLUDE, PATH, SOURCE variables from
within the Perl script and then using a system call to invoke
MSDEV.EXE using the /USEENV option. It looks like the process the
Perl script is running in is setting the variables I need, but MSDEV
is not finding them. If I manually set the same values within the DOS
console and then run my Perl script, MSDEV finds the variables.

I'm working on Windows XP, so I'm not sure if this might be the issue,
but it behaves as if MSDEV is not looking at the process that the Perl
script is running in, but is looking at it's parent for the
environment variables.

Is there any way of setting environment variables within the 'parent'
DOS process from the PErl script?

Thanks for any help provided.

-Shawn Campbell
 
B

Ben Morrow

Quoth (e-mail address removed) (Shawn Campbell):
Actually, it looks like the SetEnvVar sub I created is working fine,
other than truncating the value at 1024. The problem was in the
GetEnvVar sub I created. I didn't supply enough buffer size so it
wasn't reading in the value, giving me the impression it was not set.

Can you clarify: does SetEnvironmentVar simply set the var for the
current process, or does it change the default environment in the
registry? If it does the former, then there really is no reason not to
simply use %ENV.
I have another environment variable issue I am struggling with that I
hope you could give me some insight on.

I'm using ActivePerl to create an automated build process. Part of
the process is setting the LIB, INCLUDE, PATH, SOURCE variables from
within the Perl script and then using a system call to invoke
MSDEV.EXE using the /USEENV option. It looks like the process the
Perl script is running in is setting the variables I need, but MSDEV
is not finding them. If I manually set the same values within the DOS
console and then run my Perl script, MSDEV finds the variables.

This makes me suspect the latter; which is definitely not what you want
in this case. MSDEV.EXE will receive the environment given it by the
perl process, rather than the default environment. In addition, new
console windows you open *will* receive the default environment, so they
will get the values for LIB etc. you have specified... not terribly
useful. Try using %ENV.
I'm working on Windows XP, so I'm not sure if this might be the issue,
but it behaves as if MSDEV is not looking at the process that the Perl
script is running in, but is looking at it's parent for the GRR ^
environment variables.

This is unlikely. Processes don't have parents under windows (except
under certain restricted situations).
Is there any way of setting environment variables within the 'parent'
DOS process from the PErl script?

No.

Ben
 
S

Shawn Campbell

Ben Morrow said:
Quoth (e-mail address removed) (Shawn Campbell):

Can you clarify: does SetEnvironmentVar simply set the var for the
current process, or does it change the default environment in the
registry? If it does the former, then there really is no reason not to
simply use %ENV.


This makes me suspect the latter; which is definitely not what you want
in this case. MSDEV.EXE will receive the environment given it by the
perl process, rather than the default environment. In addition, new
console windows you open *will* receive the default environment, so they
will get the values for LIB etc. you have specified... not terribly
useful. Try using %ENV.


This is unlikely. Processes don't have parents under windows (except
under certain restricted situations).


No.

Ben

Using ENV worked. I still can't figure out why using the
SetEnvironmentVariable function would not work, but at this point I
don't care as long as I have a solution.

Thanks for the help!

Shawn Campbell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top