1

How to convert %USERPROFILE% path to file path in C++? It should work on Windows XP and later versions. I need it because when I'm trying to launch .jar file with CreateProcessW method using javaw.exe, the file is not found.

Ernestas Gruodis
  • 8,567
  • 14
  • 55
  • 117
  • possible duplicate of [expand file names that have environment variables in their path](http://stackoverflow.com/questions/1902681/expand-file-names-that-have-environment-variables-in-their-path) – ivan_pozdeev Apr 05 '14 at 00:45
  • Possible duplicate - but no good answer for me. There is nothing about `SHGetFolderPathW` method. – Ernestas Gruodis Apr 05 '14 at 06:51
  • Folders returned by `SHGetFolderPath` have nothing to do with environment variables. All the less with expanding them. – ivan_pozdeev Apr 05 '14 at 18:30

1 Answers1

2

You can use a function that expands environment variables. For instance getenv, or on Windows you could choose to use GetEnvironmentVariable.

However, the right way to find the user profile directory is to use the Windows API for it. If you need to support XP then you need to use the CSIDL for that folder: CSIDL_PROFILE. Use SHGetSpecialFolderPath to read out the path associated with a particular CSIDL value.

This is how to get the user profile directory, although I must admit to struggling to understand how that would help you launch javaw.exe.

Update

In the comments you indicate that you actually want the temporary directory. Use GetTempPath to obtain that.

Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I have to get full path to TEMP directory. I have read registry key `HKEY_CURRENT_USER\\Environment` `TEMP`, but the value contains %USERPROFILE%, and that creates the problem to javaw.exe - it does not know what is %USERPROFILE%. – Ernestas Gruodis Apr 03 '14 at 08:56
  • See http://stackoverflow.com/questions/6827496/how-to-properly-use-userprofile-inside-code – Yury Schkatula Apr 03 '14 at 08:57
  • If you want the users temp directory path then `GetTempPath` may be helpful. – Retired Ninja Apr 03 '14 at 09:05
  • Currently I'm using value from registry, but %USERPROFILE% is replaced with `SHGetFolderPathW` using `CSIDL_PROFILE` flag. Maybe `GetTempPath` is even better. Thanks for help indeed :) – Ernestas Gruodis Apr 03 '14 at 17:53
  • Well, what you are doing now is plain wrong. You have to use GetTempPath. – David Heffernan Apr 03 '14 at 17:57