GetTempPathA returning incorrect user folder

Multi tool use
Multi tool use


GetTempPathA returning incorrect user folder



I need to access the user specific temp folder on windows, which is supposed to be doable with the


GetTempPathA()



I have something along the lines of:


char buff[512];
GetTempPath(500,buff);
strcat(buff,"specific_folder_in_temp\file.txt");
FILE f*;
f = fopen(buff,"w");



However, instead of returning the expected


C:Usersuser.nameAppDataLocalTemp...



I'm getting


C:UsersUSER~1.NAMAppDataLocalTemp...



This results in my code failing. Any tips as to what I might be doing wrong?





What does GetTempPathW return? And what is the values of your TMP, TEMP, and USERPROFILE environment variables? (Type set at command prompt to see your env)
– selbie
Jul 2 at 5:47



set





As the environment variables TMP or TEMP may use short path, you could try GetLongPathNameA to convert it to the long path format.
– Stan
Jul 2 at 5:55


TMP


TEMP


GetLongPathNameA





Sounds a bit odd. Why would your code fail when presented with short file names? It sounds much more like the problem is in your code, code that we can't see. Also, not checking return values for errors is surely a problem. Don't do that.
– David Heffernan
Jul 2 at 9:09





It's normal that GetTempPath() returns a short path and you shouldn't worry about it. As others said, the problem is in the code that you don't show.
– zett42
Jul 2 at 12:18


GetTempPath()





@Liam USER~1.NAM is simply the short 8.3 form of the user.name folder. They are the same folder! If your code can't handle short and long forms of a given path, then you have bugs in your code. Whether you open C:UsersUSER~1.NAMAppDataLocalTempspecific_folder_in_tempfile.txt or C:Usersuser.nameAppDataLocalTempspecific_folder_in_tempfile.txt, you are opening the same file. What exactly is failing for you when you get a short path instead of a long path?
– Remy Lebeau
Jul 2 at 18:11



USER~1.NAM


user.name


C:UsersUSER~1.NAMAppDataLocalTempspecific_folder_in_tempfile.txt


C:Usersuser.nameAppDataLocalTempspecific_folder_in_tempfile.txt




1 Answer
1



something~1.ext is a short name. Short names are generated for compatibility with DOS/16-bit applications. Short name generation can be turned off globally or per volume with fsutil. Applications should not care if the path is short or long because a user may use either as input in your application.


something~1.ext


fsutil



Why does the system convert TEMP to a short file name?



When you set environment variables with the System control panel, the
TEMP and TMP variables are silently converted to their short file name
equivalents (if possible). Why is that?



For compatibility, of course.



It is very common for batch files to assume that the paths referred
to by the %TEMP% and %TMP% environment variables do not contain any
embedded spaces. (Other programs may also make this assumption, but
batch files are the most common place where you run into this
problem.)



I say "if possible" because you can disable short name generation, in
which case there is no short name equivalent, and the path remains in
its original long form.



You should use a function like PathCchAppend to join path elements because it takes care of the backslashes for you.


PathCchAppend






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

RxaUK000N1iG ht2s rVdD mGi7,cRAU0pY
c,uf FVJUnr,z9,yMuZXwTBoifjtV,BvxAdSu 8wFMkiybDx7jQ5KLCYBmm glq3COg,AlE6y yBgn4VCMX0nfj

Popular posts from this blog

Rothschild family

Boo (programming language)