How to get the "temp folder" in Windows 7? - c++

In Windows 7, how can I get programatically the system temporary folder?

The GetTempPath function is probably what you're looking for.
TCHAR buf [MAX_PATH];
if (GetTempPath (MAX_PATH, buf) != 0)
MessageBox (0, buf, _T("Temp path"), 0);

Have you given a try to GetTempPath()?
Retrieves the path of the directory designated for temporary files.
You can find a code sample here.

You could get the environment variable for the temp folder:
http://msdn.microsoft.com/en-us/library/ms683188%28VS.85%29.aspx

Related

c++ GetPrivateProfileString read ini file from current directory

I'm creating a dll on c++. It is a Visual Studio project. The dll reads some data from ini file. I have decided to use GetPrivateProfileString function. It works almost completely. It does not see file in current directory. How can I provide this parameter (variable called path)?
How can I pass last parameter (path)
Code:
LPCTSTR path = L"\\test.ini";
TCHAR protocolChar[32];
int a = GetPrivateProfileString(_T("Connection"), _T("Protocol"), _T(""), protocolChar, 32, path);
String from test.ini:
[Connection]
Protocol = HTTP
I also tried this:
LPCTSTR path = L"test.ini";
But it did not help me
LPCTSTR path = _T(".\\test.ini");
. symbolises current directory. Hope this will work for you.
WCHAR cfg_IniName[256];
GetCurrentDirectory (MAX_PATH, cfg_IniName );
wcscat ( cfg_IniName, L"\\test.ini" );
way to get full path

How to open a folder in %appdata% with C++?

As you all know, the appdata folder is this
C:\Users\*Username*\AppData\Roaming
on windows 7
Since my application will be deployed on all kinds of Windows OSes i need to be able to get the folder 100% percent of the time.
The question is how do you do it in C++? Since i don't know the the exact Windows OS it could be XP,Vista or 7 and most importantly i don't know what the Username is.
For maximum compatibility with all versions of Windows, you can use the SHGetFolderPath function.
It requires that you specify the CSIDL value for the folder whose path you want to retrieve. For the application data folder, that would be CSIDL_APPDATA.
On Windows Vista and later, you should use the SHGetKnownFolderPath function instead, which requires that you specify the folder's KNOWNFOLDERID value. Again, for the application data folder, the appropriate value is FOLDERID_RoamingAppData.
To use either of these functions from your C++ application, you'll need to include shlobj.h.
You can try the following:
char* appdata = getenv("APPDATA");
This code reads the environment variable APPDATA (you can also see it when you type SET in a command window). It is set by Windows when your system starts.
It will return the path of the user's appdata as an absolute path, including Username and taking into account whichever OS version they're using.
Perhaps fellow Googlers might find it interesting to have a look at std::filesystem. For instance, let's assume the default temp directory location and AppData directory structure in Windows 10:
#include <filesystem>
auto path = std::filesystem::temp_directory_path()
.parent_path()
.parent_path();
path /= "Roaming";
if (!std::filesystem::exists(path))
std::filesystem::create_directories(path);
In the case of OP, I am assuming this doesn't solve the problem. I do want to raise a word of caution against doing the above in a situation that requires a 100% robust implementation, as system configurations can easily change and break the above.
But perhaps new visitors to the question might find std::filesystem useful. Chances are, you're going to want to manipulate the items in the directory if you're looking for it, and for this, std::filesystem can be your friend.
If someone is looking for a simple implementation, here's mine:
#include <windows.h>
#include <shlobj.h>
#include <filesystem>
#include <iostream>
int main(void)
{
std::filesystem::path path;
PWSTR path_tmp;
/* Attempt to get user's AppData folder
*
* Microsoft Docs:
* https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath
* https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid
*/
auto get_folder_path_ret = SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &path_tmp);
/* Error check */
if (get_folder_path_ret != S_OK) {
CoTaskMemFree(path_tmp);
return 1;
}
/* Convert the Windows path type to a C++ path */
path = path_tmp;
/* Free memory :) */
CoTaskMemFree(path_tmp);
std::cout << path << std::endl;
return 0;
}
Use this Code to reads the environment variable "APPDATA"
Include stdio.h file in beginning
char *pValue;
size_t len;
errno_t err = _dupenv_s(&pValue, &len, "APPDATA");
Here is a simple implementation for old C++ versions :
#include <shlobj.h>
// ...
wchar_t* localAppDataFolder;
if (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, NULL, &localAppDataFolder) != S_OK) {
std::cerr << "problem getting appdata folder" << std::endl;
}
else std::wcout << L"folder found: " << localAppDataFolder << std::endl;

How to get the path of the exexuter in C++?

I am using Visual studio 2008 and I want to get the absolute path of the .exe file?
meaning when the user opens the exe file, I need to know its absolute path??
thanks in advance
Under Windows try the following:
char ExeName[8192]; // or what ever max. size you expect.
if (0 != GetModuleFileName (NULL, ExeName, sizeof (ExeName)))
{
printf ("Your array was probably not large enough. Call GetLastError for details\n");
}
If you compile for unicode use wchar_t.
Using the _pgmptr or _wpgmptr global variable is probably the easiest way.* (They're in stdlib.h.)
*Note: Under some rather rare circumstances, it's possible that this won't work... in that case, use GetModuleFileName(NULL, ...);
If you want to obtain a path of the current process, you should use API function:
GetModuleFileName
But, if you want to obtain a full path of the process that is not written by you, use
GetModuleFileNameEx
Above function expects one argument more than GetModuleFileName - it is a HANDLE of a process which path is supposed to be obtained. It is explained in more details on MSDN.

How to delete folder into recycle bin

I am programing under C++, MFC, windows.
I want to delete a folder into recycle bin.
How can I do this?
CString filePath = directorytoBeDeletePath;
TCHAR ToBuf[MAX_PATH + 10];
TCHAR FromBuf[MAX_PATH + 10];
ZeroMemory(ToBuf, sizeof(ToBuf));
ZeroMemory(FromBuf, sizeof(FromBuf));
lstrcpy(FromBuf, filePath);
SHFILEOPSTRUCT FileOp;
FileOp.hwnd = NULL
FileOp.wFunc=FO_DELETE;
FileOp.pFrom=FromBuf;
FileOp.pTo = NULL;
FileOp.fFlags=FOF_ALLOWUNDO|FOF_NOCONFIRMATION;
FileOp.hNameMappings=NULL;
bRet=SHFileOperation(&FileOp);
Any thing wrong with the code above?
It always failed.
I found the problem:
filePath should be : "c:\abc" not "c:\abc\"
The return value from SHFileOperation is an int, and should specify the error code. What do you get?
i know it is not the right way but if you cant find a solution you can try this..
download file nircmd.exe or another exe that can empty recycle bin.
then you call these functions by system("nircmd.exe emptybin")
You have found a solution that works, however it's only by accident. The actual problem here is that the pFrom parameter is in a special format. According to the MSDN docs for SHFILEOPTS, it stores a list of file paths, each one null-terminated, and an extra null after the last one.
In your case this happens to work because the FromBuf array is longer than the filename and all the entries are initialised to zero. The more general solution is to create a buffer that is long enough for the filename and then add two nul characters after it. Note that Windows filenames can be longer than _MAX_PATH, eg see https://learn.microsoft.com/en-us/windows/desktop/fileio/naming-a-file#maximum-path-length-limitation

MFC: GetCurrentDirectory function

I know that GetCurrentDirectory() and SetCurrentDirectory() functions exist on the MFC framework, but I don't have a CFtpConnection object in my application. I have a simple CWinApp-derived class, and I would like to retrieve its working directory upon program startup. What's the easiest method to achieve this goal? Thanks in advance for the advices.
GetCurrentDirectory is a simple Win32 API function, so just call it like this:
TCHAR currentDir[MAX_PATH];
GetCurrentDirectory( MAX_PATH, currentDir );
I assume you are trying to get the directory where your .exe file is located instead of the current directory. This directory can be different from the current directory.
TCHAR buff[MAX_PATH];
memset(buff, 0, MAX_PATH);
::GetModuleFileName(NULL,buff,sizeof(buff));
CString strFolder = buff;
strFolder = strFolder.Left(strFolder.ReverseFind(_T('\\'))+1);