Skip to content

Commit

Permalink
Use ansi codepage for multibyte strings (fixes #976)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertony committed Dec 9, 2020
1 parent 06951ea commit 5302efd
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 32 deletions.
28 changes: 3 additions & 25 deletions src/lib/common/win32/DataDirectories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,12 @@
*/

#include "../DataDirectories.h"

#include <Shlobj.h>

std::string unicode_to_mb(const WCHAR* utfStr)
{
int utfLength = lstrlenW(utfStr);
int mbLength = WideCharToMultiByte(CP_UTF8, 0, utfStr, utfLength, NULL, 0, NULL, NULL);
std::string mbStr(mbLength, 0);
WideCharToMultiByte(CP_UTF8, 0, utfStr, utfLength, &mbStr[0], mbLength, NULL, NULL);
return mbStr;
}

std::string known_folder_path(const KNOWNFOLDERID& id)
{
std::string path;
WCHAR* buffer;
HRESULT result = SHGetKnownFolderPath(id, 0, NULL, &buffer);
if (result == S_OK) {
path = unicode_to_mb(buffer);
CoTaskMemFree(buffer);
}
return path;
}
#include "KnownFolderPaths.h"

const std::string& DataDirectories::profile()
{
if (_profile.empty())
_profile = known_folder_path(FOLDERID_LocalAppData) + "\\Barrier";
_profile = KnownFolderPaths::localAppData() + "\\Barrier";
return _profile;
}
const std::string& DataDirectories::profile(const std::string& path)
Expand All @@ -55,7 +33,7 @@ const std::string& DataDirectories::profile(const std::string& path)
const std::string& DataDirectories::global()
{
if (_global.empty())
_global = known_folder_path(FOLDERID_ProgramData) + "\\Barrier";
_global = KnownFolderPaths::programData() + "\\Barrier";
return _global;
}
const std::string& DataDirectories::global(const std::string& path)
Expand Down
61 changes: 61 additions & 0 deletions src/lib/common/win32/KnownFolderPaths.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* barrier -- mouse and keyboard sharing utility
* Copyright (C) 2018 Debauchee Open Source Group
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "KnownFolderPaths.h"
#include <string>

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <Shlobj.h>

std::string unicode_to_mb(const WCHAR* utfStr)
{
int utfLength = lstrlenW(utfStr);
int mbLength = WideCharToMultiByte(CP_ACP, 0, utfStr, utfLength, NULL, 0, NULL, NULL);
std::string mbStr(mbLength, 0);
WideCharToMultiByte(CP_ACP, 0, utfStr, utfLength, &mbStr[0], mbLength, NULL, NULL);
return mbStr;
}

std::string known_folder_path(const KNOWNFOLDERID& id)
{
std::string path;
WCHAR* buffer;
HRESULT result = SHGetKnownFolderPath(id, 0, NULL, &buffer);
if (result == S_OK) {
path = unicode_to_mb(buffer);
CoTaskMemFree(buffer);
}
return path;
}

std::string KnownFolderPaths::desktop()
{
return known_folder_path(FOLDERID_Desktop);
}

std::string KnownFolderPaths::localAppData()
{
return known_folder_path(FOLDERID_LocalAppData);
}

std::string KnownFolderPaths::programData()
{
return known_folder_path(FOLDERID_ProgramData);
}
32 changes: 32 additions & 0 deletions src/lib/common/win32/KnownFolderPaths.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* barrier -- mouse and keyboard sharing utility
* Copyright (C) 2018 Debauchee Open Source Group
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <string>

class KnownFolderPaths
{
public:
static std::string desktop();
static std::string localAppData();
static std::string programData();

private:
// static class
KnownFolderPaths() {}
};
11 changes: 4 additions & 7 deletions src/lib/platform/MSWindowsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
#include "base/IEventQueue.h"
#include "base/TMethodEventJob.h"
#include "base/TMethodJob.h"
#include "common/win32/KnownFolderPaths.h"

#include <string.h>
#include <Shlobj.h>
#include <comutil.h>
#include <algorithm>

Expand Down Expand Up @@ -145,15 +145,12 @@ MSWindowsScreen::MSWindowsScreen(
forceShowCursor();
LOG((CLOG_DEBUG "screen shape: %d,%d %dx%d %s", m_x, m_y, m_w, m_h, m_multimon ? "(multi-monitor)" : ""));
LOG((CLOG_DEBUG "window is 0x%08x", m_window));

// SHGetFolderPath is deprecated in vista, but use it for xp support.
char desktopPath[MAX_PATH];
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_DESKTOP, NULL, 0, desktopPath))) {
m_desktopPath = std::string(desktopPath);
m_desktopPath = KnownFolderPaths::desktop();
if (!m_desktopPath.empty()) {
LOG((CLOG_DEBUG "using desktop for drop target: %s", m_desktopPath.c_str()));
}
else {
LOG((CLOG_ERR "failed to get desktop path, no drop target available, error=%d", GetLastError()));
LOG((CLOG_ERR "failed to get desktop path, no drop target available"));
}

OleInitialize(0);
Expand Down

0 comments on commit 5302efd

Please sign in to comment.