-
February 2nd, 2010, 06:03 AM
#1
[win32] temp and preference path on windows
Hi,
I was searching a place in the windows filesystem to save a preference file. I have found out that onmy system (vista) that place is supposed to be:
Pref: \Users\MY_ACCOUNT\AppData\Local
Also, I'd like to know if the following is the correct path to a %temp% directory to save some file for the running session to be delete at the end of the session:
Temp: \Users\MY_ACCOUNT\AppData\Local\Temp
Do you think I could retrieve those paths with some API function?
thanks
-
February 2nd, 2010, 06:50 AM
#2
Re: [win32] temp and preference path on windows
You could begin by opening a command window and looking at the environment variables. (From the command prompt type "set" - without the quotes). You'll find variables like
APPDATA
HOMEPATH
TEMP
USERPROFILE
and so on. You can then use GetEnvironmentVariable to get those values.
Hope that helps.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
February 2nd, 2010, 07:12 AM
#3
Re: [win32] temp and preference path on windows
Originally Posted by THEARTOFWEB
...
Do you think I could retrieve those paths with some API function?
Yes!
SHGetKnownFolderPath Function
KNOWNFOLDERID
or
SHGetFolderPath Function
CSIDL
Victor Nijegorodov
-
February 2nd, 2010, 01:23 PM
#4
Re: [win32] temp and preference path on windows
Ok, I am tring this: http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
Code:
char * fullpath;
HRESULT res = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, 0, fullpath);
I get this:
'SHGetKnownFolderPath' : cannot convert parameter 4 from 'char *' to 'PWSTR *'
how can I cast correctly?
thanks
-
February 2nd, 2010, 01:32 PM
#5
Re: [win32] temp and preference path on windows
Why do you ignore what MSDN claims?
Parameter 4 must be of the type PWSTR *, not char *!
Code:
PWSTR pName;
HRESULT res = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, 0, &pName);
Victor Nijegorodov
-
February 2nd, 2010, 01:49 PM
#6
Re: [win32] temp and preference path on windows
oh well...yeah! Yet, I'd like to normalize that to a regular string or char arrays. Infact if I do like this:
Code:
std::cout << *pName << std::endl;
// or
std::cout << pName << std::endl;
I get just numbers :-(
-
February 2nd, 2010, 03:54 PM
#7
Re: [win32] temp and preference path on windows
Originally Posted by THEARTOFWEB
oh well...yeah! Yet, I'd like to normalize that to a regular string or char arrays. Infact if I do like this:
Code:
std::cout << *pName << std::endl;
// or
std::cout << pName << std::endl;
I get just numbers :-(
You need to start understanding the difference between compiling for UNICODE and MBCS (ANSI). Also in what happens to CString and tchar.h typedef types under the different character sets.
-
February 2nd, 2010, 06:36 PM
#8
Re: [win32] temp and preference path on windows
Ok, sorted out this way:
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <shlobj.h>
int main()
{
TCHAR szPath[MAX_PATH] = {0};
HRESULT res = SHGetFolderPath(NULL,
CSIDL_LOCAL_APPDATA|CSIDL_FLAG_DONT_VERIFY,
NULL,
0,
szPath);
std::cout << szPath << std::endl;
system("pause");
return EXIT_SUCCESS;
}
Now, I was wondering whether there was a way to append a file path to the end of szPath. I mean, is there any function aimed to deal with slashes or is it something I can do on my own??
-
February 2nd, 2010, 07:15 PM
#9
Re: [win32] temp and preference path on windows
Originally Posted by THEARTOFWEB
Now, I was wondering whether there was a way to append a file path to the end of szPath. I mean, is there any function aimed to deal with slashes or is it something I can do on my own??
Yep, you can use the CString class which already knows about UNICODE/MBCS.
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <shlobj.h>
#include <atlstr.h>
int main()
{
CString sPath;
HRESULT res = SHGetFolderPath(
NULL,
CSIDL_LOCAL_APPDATA | CSIDL_FLAG_DONT_VERIFY,
NULL,
0,
sPath.GetBuffer( MAX_PATH );
sPath.ReleaseBuffer( );
sPath += _T("\\appendthis");
std::cout << sPath.GetString( ) << std::endl;
// if compiling for UNICODE...
// std::wcout << sPath.GetString( ) << std::endl;
system("pause");
return EXIT_SUCCESS;
}
-
February 3rd, 2010, 06:06 AM
#10
Re: [win32] temp and preference path on windows
Ok, one more thing. Why the following fires an error?
Code:
#include <iostream>
#include <cstdlib>
#include <tchar.h>
#include <shlobj.h>
#include <shlwapi.h>
int _tmain()
{
TCHAR szPath[MAX_PATH] = {0};
HRESULT res = SHGetFolderPath(NULL,
CSIDL_LOCAL_APPDATA|CSIDL_FLAG_DONT_VERIFY,
NULL,
0,
szPath);
std::cout << szPath << std::endl;
PathAppend(szPath, TEXT("myfile.txt"));
std::cout << szPath << std::endl;
system("pause");
return EXIT_SUCCESS;
}
main.obj : error LNK2019: unresolved external symbol __imp__PathAppendA@8 referenced in function _main
???
thanks
-
February 3rd, 2010, 06:10 AM
#11
Re: [win32] temp and preference path on windows
Originally Posted by THEARTOFWEB
Ok, one more thing. Why the following fires an error?
...
main.obj : error LNK2019: unresolved external symbol __imp__PathAppendA@8 referenced in function _main
???
Why don't you want to read the PathAppend documentation????
Please, have a look at the "Function Information section (Header and Import library)
Victor Nijegorodov
-
February 3rd, 2010, 07:04 AM
#12
Re: [win32] temp and preference path on windows
yeah, I always miss that :-(
Code:
#pragma comment (lib, "shlwapi.lib")
thanks
-
February 3rd, 2010, 12:09 PM
#13
Re: [win32] temp and preference path on windows
Originally Posted by THEARTOFWEB
yeah, I always miss that :-(
Code:
#pragma comment (lib, "shlwapi.lib")
thanks
Or you could just use the CString class and eliminate about 1/3 of your string handling code.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|