Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GUI] Use wchar_t API explicitly on Windows #2174

Merged
merged 1 commit into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions src/qt/guiutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,17 @@ bool DHMSTableWidgetItem::operator<(QTableWidgetItem const& item) const
#ifdef WIN32
fs::path static StartupShortcutPath()
{
if (gArgs.GetBoolArg("-testnet", false))
return GetSpecialFolderPath(CSIDL_STARTUP) / "PIVX (testnet).lnk";
else if (gArgs.GetBoolArg("-regtest", false))
return GetSpecialFolderPath(CSIDL_STARTUP) / "PIVX (regtest).lnk";

return GetSpecialFolderPath(CSIDL_STARTUP) / "PIVX.lnk";
}

bool GetStartOnSystemStartup()
{
// check for PIVX.lnk
// check for PIVX*.lnk
return fs::exists(StartupShortcutPath());
}

Expand All @@ -648,36 +653,35 @@ bool SetStartOnSystemStartup(bool fAutoStart)
CoInitialize(NULL);

// Get a pointer to the IShellLink interface.
IShellLink* psl = NULL;
HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink,
IShellLinkW* psl = nullptr;
HRESULT hres = CoCreateInstance(CLSID_ShellLink, nullptr,
CLSCTX_INPROC_SERVER, IID_IShellLinkW,
reinterpret_cast<void**>(&psl));

if (SUCCEEDED(hres)) {
// Get the current executable path
TCHAR pszExePath[MAX_PATH];
GetModuleFileName(NULL, pszExePath, sizeof(pszExePath));
WCHAR pszExePath[MAX_PATH];
GetModuleFileNameW(nullptr, pszExePath, ARRAYSIZE(pszExePath));

TCHAR pszArgs[5] = TEXT("-min");
// Start client minimized
QString strArgs = "-min";
// Set -testnet /-regtest options
strArgs += QString::fromStdString(strprintf(" -testnet=%d -regtest=%d", gArgs.GetBoolArg("-testnet", false), gArgs.GetBoolArg("-regtest", false)));
Fuzzbawls marked this conversation as resolved.
Show resolved Hide resolved

// Set the path to the shortcut target
psl->SetPath(pszExePath);
PathRemoveFileSpec(pszExePath);
PathRemoveFileSpecW(pszExePath);
psl->SetWorkingDirectory(pszExePath);
psl->SetShowCmd(SW_SHOWMINNOACTIVE);
psl->SetArguments(pszArgs);
psl->SetArguments(strArgs.toStdWString().c_str());

// Query IShellLink for the IPersistFile interface for
// saving the shortcut in persistent storage.
IPersistFile* ppf = NULL;
hres = psl->QueryInterface(IID_IPersistFile,
reinterpret_cast<void**>(&ppf));
IPersistFile* ppf = nullptr;
hres = psl->QueryInterface(IID_IPersistFile, reinterpret_cast<void**>(&ppf));
if (SUCCEEDED(hres)) {
WCHAR pwsz[MAX_PATH];
// Ensure that the string is ANSI.
MultiByteToWideChar(CP_ACP, 0, StartupShortcutPath().string().c_str(), -1, pwsz, MAX_PATH);
// Save the link by calling IPersistFile::Save.
hres = ppf->Save(pwsz, TRUE);
hres = ppf->Save(StartupShortcutPath().wstring().c_str(), TRUE);
ppf->Release();
psl->Release();
CoUninitialize();
Expand All @@ -694,7 +698,7 @@ bool SetStartOnSystemStartup(bool fAutoStart)
#elif defined(Q_OS_LINUX)

// Follow the Desktop Application Autostart Spec:
// http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html
// http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html

fs::path static GetAutostartDir()
{
Expand Down Expand Up @@ -746,8 +750,13 @@ bool SetStartOnSystemStartup(bool fAutoStart)
// Write a pivx.desktop file to the autostart directory:
optionFile << "[Desktop Entry]\n";
optionFile << "Type=Application\n";
optionFile << "Name=PIVX\n";
optionFile << "Exec=" << pszExePath << " -min\n";
if (gArgs.GetBoolArg("-testnet", false))
optionFile << "Name=PIVX (testnet)\n";
else if (gArgs.GetBoolArg("-regtest", false))
optionFile << "Name=PIVX (regtest)\n";
else
optionFile << "Name=PIVX\n";
optionFile << "Exec=" << pszExePath << strprintf(" -min -testnet=%d -regtest=%d\n", gArgs.GetBoolArg("-testnet", false), gArgs.GetBoolArg("-regtest", false));
optionFile << "Terminal=false\n";
optionFile << "Hidden=false\n";
optionFile.close();
Expand Down
6 changes: 3 additions & 3 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,13 +737,13 @@ void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length)
#ifdef WIN32
fs::path GetSpecialFolderPath(int nFolder, bool fCreate)
{
char pszPath[MAX_PATH] = "";
WCHAR pszPath[MAX_PATH] = L"";

if (SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate)) {
if (SHGetSpecialFolderPathW(nullptr, pszPath, nFolder, fCreate)) {
return fs::path(pszPath);
}

LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
LogPrintf("SHGetSpecialFolderPathW() failed, could not obtain requested path.\n");
return fs::path("");
}
#endif
Expand Down