A customer noticed that environment variables were being truncated at 2047 characters and wanted to know what the maximum length of an environment variable is.
This is one of those cases where you can answer the customer’s question or you can try to answer the question the customer is really asking. If you just answer the question, you’re doing the customer a disservice.
The theoretical maximum length of an environment variable is around 32,760 characters. However, you are unlikely to attain that theoretical maximum in practice.
All environment variables must live together in a single environment block, which itself has a limit of 32767 characters. But that count is the sum over all environment variable names and values, so you could, I guess, hit that theoretical maximum length if you deleted all the environment variables and then set a single variable called X
with that really huge 32,760-character value. In practice, of course, you have to share the environment block with all the other variables in the block, so your random call to SetEnvironmentVariable
with a 32,760-character string is unlikely to succeed.
But that’s not your only practical limit.
It also depends on how you’re setting the variable; i.e., the code that your environment-variable-setting technique passes through before it gets to the SetEnvironmentVariable
call. If you’re using a batch file, then you’re constrained by the maximum command line length since the environment variable needs to fit into the command line buffer of the batch processor. On the other hand, maybe you’re setting the Environment registry key, in which case you run into a 2048-character limit in the code that parses that registry key and builds an environment block out of it. There’s also a limitation in the dialog box for interactively setting environment variables, the numeric value of which I don’t happen to know off the top of my head.
This is one of those skills you have to develop when answering questions from customers: Taking the customer’s question and trying to figure out what their real question is.
0 comments