Skip to content

Commit b3fc57c

Browse files
authored
Merge pull request #412 from elaberge/ansi
Handle non-Unicode configurations
2 parents 58c8e4e + 26c8815 commit b3fc57c

File tree

13 files changed

+89
-63
lines changed

13 files changed

+89
-63
lines changed

src/cpucounters.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ bool PCM::initWinRing0Lib()
103103

104104
BYTE major, minor, revision, release;
105105
GetDriverVersion(&major, &minor, &revision, &release);
106-
wchar_t buffer[128];
107-
swprintf_s(buffer, 128, _T("\\\\.\\WinRing0_%d_%d_%d"),(int)major,(int)minor, (int)revision);
106+
TCHAR buffer[128];
107+
_stprintf_s(buffer, 128, TEXT("\\\\.\\WinRing0_%d_%d_%d"),(int)major,(int)minor, (int)revision);
108108
restrictDriverAccess(buffer);
109109

110110
return true;
@@ -1087,11 +1087,11 @@ bool PCM::discoverSystemTopology()
10871087
}
10881088
else
10891089
{
1090-
std::wcerr << "Error in Windows function 'GetLogicalProcessorInformationEx': " <<
1090+
tcerr << "Error in Windows function 'GetLogicalProcessorInformationEx': " <<
10911091
GetLastError() << " ";
10921092
const TCHAR * strError = _com_error(GetLastError()).ErrorMessage();
1093-
if (strError) std::wcerr << strError;
1094-
std::wcerr << "\n";
1093+
if (strError) tcerr << strError;
1094+
tcerr << "\n";
10951095
return false;
10961096
}
10971097
}
@@ -2121,8 +2121,8 @@ PCM::PCM() :
21212121
// drv.stop(); // restart driver (usually not needed)
21222122
if (!drv.start())
21232123
{
2124-
std::wcerr << "Cannot access CPU counters\n";
2125-
std::wcerr << "You must have a signed driver at " << drv.driverPath() << " and have administrator rights to run this program\n";
2124+
tcerr << "Cannot access CPU counters\n";
2125+
tcerr << "You must have a signed driver at " << drv.driverPath() << " and have administrator rights to run this program\n";
21262126
return;
21272127
}
21282128
#endif

src/cpucounters.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
namespace pcm {
6767

6868
#ifdef _MSC_VER
69-
void PCM_API restrictDriverAccess(LPCWSTR path);
69+
void PCM_API restrictDriverAccess(LPCTSTR path);
7070
#endif
7171

7272
class SystemCounterState;

src/mmio.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ class PCMPmem : public WinPmem {
4343
switch (sys_info.wProcessorArchitecture)
4444
{
4545
case PROCESSOR_ARCHITECTURE_AMD64:
46-
wcscat_s(driver_filename, MAX_PATH, L"\\winpmem_x64.sys");
46+
_tcscat_s(driver_filename, MAX_PATH, TEXT("\\winpmem_x64.sys"));
4747
if (GetFileAttributes(driver_filename) == INVALID_FILE_ATTRIBUTES)
4848
{
4949
std::cerr << "ERROR: winpmem_x64.sys not found in current directory. Download it from https://github.com/Velocidex/WinPmem/blob/master/kernel/binaries/winpmem_x64.sys .\n";
5050
std::cerr << "ERROR: Memory bandwidth statistics will not be available.\n";
5151
}
5252
break;
5353
case PROCESSOR_ARCHITECTURE_INTEL:
54-
wcscat_s(driver_filename, MAX_PATH, L"\\winpmem_x86.sys");
54+
_tcscat_s(driver_filename, MAX_PATH, TEXT("\\winpmem_x86.sys"));
5555
if (GetFileAttributes(driver_filename) == INVALID_FILE_ATTRIBUTES)
5656
{
5757
std::cerr << "ERROR: winpmem_x86.sys not found in current directory. Download it from https://github.com/Velocidex/WinPmem/blob/master/kernel/binaries/winpmem_x86.sys .\n";

src/pcm-memory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,8 +1123,8 @@ int main(int argc, char * argv[])
11231123
Driver tmpDrvObject = Driver(Driver::msrLocalPath());
11241124
if (!tmpDrvObject.start())
11251125
{
1126-
wcerr << "Can not access CPU counters\n";
1127-
wcerr << "You must have a signed driver at " << tmpDrvObject.driverPath() << " and have administrator rights to run this program\n";
1126+
tcerr << "Can not access CPU counters\n";
1127+
tcerr << "You must have a signed driver at " << tmpDrvObject.driverPath() << " and have administrator rights to run this program\n";
11281128
exit(EXIT_FAILURE);
11291129
}
11301130
exit(EXIT_SUCCESS);

src/pcm-msr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ int main(int argc, char * argv[])
8383
// drv.stop(); // restart driver (usually not needed)
8484
if (!drv.start())
8585
{
86-
std::wcerr << "Can not load MSR driver.\n";
87-
std::wcerr << "You must have a signed driver at " << drv.driverPath() << " and have administrator rights to run this program\n";
86+
tcerr << "Can not load MSR driver.\n";
87+
tcerr << "You must have a signed driver at " << drv.driverPath() << " and have administrator rights to run this program\n";
8888
return -1;
8989
}
9090
#endif

src/pcm-pcicfg.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ int main(int argc, char * argv[])
8383
// drv.stop(); // restart driver (usually not needed)
8484
if (!drv.start())
8585
{
86-
std::wcerr << "Can not load MSR driver.\n";
87-
std::wcerr << "You must have a signed driver at " << drv.driverPath() << " and have administrator rights to run this program\n";
86+
tcerr << "Can not load MSR driver.\n";
87+
tcerr << "You must have a signed driver at " << drv.driverPath() << " and have administrator rights to run this program\n";
8888
return -1;
8989
}
9090
#endif

src/pcm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,8 +1306,8 @@ int main(int argc, char * argv[])
13061306
Driver tmpDrvObject = Driver(Driver::msrLocalPath());
13071307
if (!tmpDrvObject.start())
13081308
{
1309-
wcerr << "Can not access CPU counters\n";
1310-
wcerr << "You must have a signed driver at " << tmpDrvObject.driverPath() << " and have administrator rights to run this program\n";
1309+
tcerr << "Can not access CPU counters\n";
1310+
tcerr << "You must have a signed driver at " << tmpDrvObject.driverPath() << " and have administrator rights to run this program\n";
13111311
exit(EXIT_FAILURE);
13121312
}
13131313
exit(EXIT_SUCCESS);

src/utils.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,18 +264,18 @@ void set_signal_handlers(void)
264264
// to fix Cygwin/BASH setting Ctrl+C handler need first to restore the default one
265265
handlerStatus = SetConsoleCtrlHandler(NULL, FALSE); // restores normal processing of CTRL+C input
266266
if (handlerStatus == 0) {
267-
std::wcerr << "Failed to set Ctrl+C handler. Error code: " << GetLastError() << " ";
267+
tcerr << "Failed to set Ctrl+C handler. Error code: " << GetLastError() << " ";
268268
const TCHAR * errorStr = _com_error(GetLastError()).ErrorMessage();
269-
if (errorStr) std::wcerr << errorStr;
270-
std::wcerr << "\n";
269+
if (errorStr) tcerr << errorStr;
270+
tcerr << "\n";
271271
_exit(EXIT_FAILURE);
272272
}
273273
handlerStatus = SetConsoleCtrlHandler((PHANDLER_ROUTINE)sigINT_handler, TRUE);
274274
if (handlerStatus == 0) {
275-
std::wcerr << "Failed to set Ctrl+C handler. Error code: " << GetLastError() << " ";
275+
tcerr << "Failed to set Ctrl+C handler. Error code: " << GetLastError() << " ";
276276
const TCHAR * errorStr = _com_error(GetLastError()).ErrorMessage();
277-
if (errorStr) std::wcerr << errorStr;
278-
std::wcerr << "\n";
277+
if (errorStr) tcerr << errorStr;
278+
tcerr << "\n";
279279
_exit(EXIT_FAILURE);
280280
}
281281
SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)&unhandled_exception_handler);

src/utils.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929

3030
namespace pcm {
3131

32+
#ifdef _MSC_VER
33+
using tstring = std::basic_string<TCHAR>;
34+
#ifdef UNICODE
35+
static auto& tcerr = std::wcerr;
36+
#else
37+
static auto& tcerr = std::cerr;
38+
#endif
39+
#endif // _MSC_VER
40+
3241
void exit_cleanup(void);
3342
void set_signal_handlers(void);
3443
void set_real_time_priority(const bool & silent);
@@ -80,7 +89,7 @@ void MySystem(char * sysCmd, char ** argc);
8089
#endif
8190
struct null_stream : public std::streambuf
8291
{
83-
void overflow(char) { }
92+
int_type overflow(int_type) override { return {}; }
8493
};
8594
#ifdef __GCC__
8695
#pragma GCC diagnostic pop
@@ -474,7 +483,7 @@ std::string safe_getenv(const char* env);
474483
#ifdef _MSC_VER
475484
inline HANDLE openMSRDriver()
476485
{
477-
return CreateFile(L"\\\\.\\RDMSR", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
486+
return CreateFile(TEXT("\\\\.\\RDMSR"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
478487
}
479488
#endif
480489

src/windows/PCMService.h

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
#include "pcm-lib.h"
1111
#include "windriver.h"
1212
#include <stdexcept>
13+
14+
#ifndef UNICODE
15+
#include <locale>
16+
#include <codecvt>
17+
#endif
1318
#pragma managed
1419

1520
using namespace pcm;
@@ -743,29 +748,29 @@ namespace PCMServiceNS {
743748

744749
// Read configuration values from registry
745750
HKEY hkey;
746-
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\pcm\\service", NULL, KEY_READ, &hkey))
751+
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\pcm\\service"), NULL, KEY_READ, &hkey))
747752
{
748753
DWORD regDWORD = static_cast<DWORD>(REG_DWORD);
749754
DWORD lenDWORD = 32;
750755

751756
DWORD sampleRateRead(0);
752-
if (ERROR_SUCCESS == RegQueryValueEx(hkey, L"SampleRate", NULL, NULL, reinterpret_cast<LPBYTE>(&sampleRateRead), &lenDWORD))
757+
if (ERROR_SUCCESS == RegQueryValueEx(hkey, TEXT("SampleRate"), NULL, NULL, reinterpret_cast<LPBYTE>(&sampleRateRead), &lenDWORD))
753758
{
754759
sampleRate = (int)sampleRateRead;
755760
}
756761

757762
DWORD collectCoreRead(0);
758-
if (ERROR_SUCCESS == RegQueryValueEx(hkey, L"CollectCore", NULL, NULL, reinterpret_cast<LPBYTE>(&collectCoreRead), &lenDWORD)) {
763+
if (ERROR_SUCCESS == RegQueryValueEx(hkey, TEXT("CollectCore"), NULL, NULL, reinterpret_cast<LPBYTE>(&collectCoreRead), &lenDWORD)) {
759764
collectionInformation->core = (int)collectCoreRead > 0;
760765
}
761766

762767
DWORD collectSocketRead(0);
763-
if (ERROR_SUCCESS == RegQueryValueEx(hkey, L"CollectSocket", NULL, NULL, reinterpret_cast<LPBYTE>(&collectSocketRead), &lenDWORD)) {
768+
if (ERROR_SUCCESS == RegQueryValueEx(hkey, TEXT("CollectSocket"), NULL, NULL, reinterpret_cast<LPBYTE>(&collectSocketRead), &lenDWORD)) {
764769
collectionInformation->socket = (int)collectSocketRead > 0;
765770
}
766771

767772
DWORD collectQpiRead(0);
768-
if (ERROR_SUCCESS == RegQueryValueEx(hkey, L"CollectQpi", NULL, NULL, reinterpret_cast<LPBYTE>(&collectQpiRead), &lenDWORD)) {
773+
if (ERROR_SUCCESS == RegQueryValueEx(hkey, TEXT("CollectQpi"), NULL, NULL, reinterpret_cast<LPBYTE>(&collectQpiRead), &lenDWORD)) {
769774
collectionInformation->qpi = (int)collectQpiRead > 0;
770775
}
771776

@@ -778,7 +783,13 @@ namespace PCMServiceNS {
778783
drv_ = new Driver;
779784
if (!drv_->start())
780785
{
781-
String^ s = gcnew String((L"Cannot open the driver.\nYou must have a signed driver at " + drv_->driverPath() + L" and have administrator rights to run this program.\n\n").c_str());
786+
#ifdef UNICODE
787+
const auto& driverPath = drv_->driverPath();
788+
#else
789+
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> char_to_wide;
790+
std::wstring driverPath = char_to_wide.from_bytes(drv_->driverPath().c_str());
791+
#endif
792+
String^ s = gcnew String((L"Cannot open the driver.\nYou must have a signed driver at " + driverPath + L" and have administrator rights to run this program.\n\n").c_str());
782793
EventLog->WriteEntry(Globals::ServiceName, s, EventLogEntryType::Error);
783794
SetServiceFail(ERROR_FILE_NOT_FOUND);
784795
throw gcnew Exception(s);

0 commit comments

Comments
 (0)