Skip to content

Commit

Permalink
Merge #2174: [GUI] Use wchar_t API explicitly on Windows
Browse files Browse the repository at this point in the history
e2fee5c [GUI] Use wchar_t API explicitly on Windows (Fuzzbawls)

Pull request description:

  backport of bitcoin#13734 to use the wchar API for windows startup function. This is the first in a long list of upstream PRs that aim to bring full Unicode support to Windows clients as detailed in bitcoin#13869

  Also included here is the relevant parts of bitcoin#5793, to use per-network auto startup shortcuts.

ACKs for top commit:
  furszy:
    utACK e2fee5c .
  random-zebra:
    utACK e2fee5c and merging...

Tree-SHA512: dc4ea84ee10199bdf293977f00c09caf65cbdd1ee2ae14b1adb34fd75c05b847d5971abd17732a77be88bed9e06092b670b09c5760a686b29d64ba8f02d2e4b2
  • Loading branch information
random-zebra committed Feb 6, 2021
2 parents 4903df0 + e2fee5c commit f15a167
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
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)));

// 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

0 comments on commit f15a167

Please sign in to comment.