First off, you are mixing TCHAR and char APIs in a way you should not be. You really should not be using TCHAR at all in modern code. But, if you are going to use TCHAR, then at least use TCHAR- based functions/macros, like _tprintf() instead of printf(), _tcscat() instead of strcat(), etc.
The compiler error is because you are trying to assign the char* pointer returned by strcat() to your dstPath TCHAR[] array. You can't assign a pointer to an array like that. You should strcpy() the result of getenv() into dstPath first, and then strcat() your filename onto the end of it, eg:
#include <string>
#include <filesystem>
#include <Windows.h>
#include <Shlwapi.h>
#include <stdio.h>
#include <tchar.h>
TCHAR* _tgetenv(const TCHAR *varname)
{
#ifdef _UNICODE
return _wgetenv(varname);
#else
return getenv(varname);
#endif
}
std::basic_string<TCHAR> path2TStr(const std::filesystem::path &p)
{
#ifdef _UNICODE
return p.wstring();
#else
return p.string();
#endif
}
int main()
{
TCHAR selfPath[MAX_PATH];
TCHAR dstPath[MAX_PATH];
if (GetModuleFileName(NULL, selfPath, MAX_PATH) == 0) // Getting exe File Location
{
printf("Error : %ul\n", GetLastError());
return 0;
}
std::filesystem::path p(selfPath);
_tcscpy(dstPath, _tgetenv(_T("APPDATA")));
_tcscat(dstPath, path2TStr(p.filename()).c_str());
_tprintf(_T("Src : %s\n"), selfPath);
_tprintf(_T("Dst : %s\n"), dstPath);
return 0;
}
However, you really should be using SHGetFolderPath(CSIDL_APPDATA) or SHGetKnownFolderPath(FOLDERID_RoamingAppData) instead of using getenv("APPDATA").
And since you are using the <filesystem> library anyway, you really should just use std::filesystem::path for all of your path handling. It has operator/= and operator/ to concatenate path segments, and an operator<< for printing paths to a std::ostream, like std::cout. Don't use strcat() for concatenating path segments, it won't handle directory separators correctly, at least.
Try this instead:
#include <iostream>
#include <string>
#include <filesystem>
#include <stdexcept>
#include <Windows.h>
#include <Shlobj.h>
std::filesystem::path getSelfPath()
{
WCHAR wPath[MAX_PATH] = {};
if (!GetModuleFileNameW(NULL, wPath, MAX_PATH)) // Getting exe File Location
{
DWORD err = GetLastError();
throw std::runtime_error("Error : " << std::to_string(err));
}
return wPath;
}
std::filesystem::path getAppDataPath()
{
WCHAR wPath[MAX_PATH] = {};
HRESULT hRes = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, wPath); // Getting APPDATA Folder Location
if (hRes != S_OK)
throw std::runtime_error("Error : " << std::to_string(hRes));
return wPath;
}
int main()
{
try
{
auto selfPath = getSelfPath();
auto dstPath = getAppDataPath() / selfPath.filename();
std::cout << "Src : " << selfPath << "\n";
std::cout << "Dst : " << dstPath << "\n";
}
catch (const std::exception &e)
{
std::cerr << e.what() << "\n";
}
return 0;
}