Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Make win32 ansi api usage explicit
Use widechar versions in a couple of places.
Don't use C-style cast in C++ code.
  • Loading branch information
piscisaureus committed Feb 16, 2012
1 parent d52f502 commit 8f2694b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 42 deletions.
42 changes: 23 additions & 19 deletions src/node.cc
Expand Up @@ -2361,13 +2361,14 @@ DWORD WINAPI EnableDebugThreadProc(void* arg) {
}


static int GetDebugSignalHandlerMappingName(DWORD pid, char* buf, size_t buf_len) {
return snprintf(buf, buf_len, "node-debug-handler-%u", pid);
static int GetDebugSignalHandlerMappingName(DWORD pid, wchar_t* buf,
size_t buf_len) {
return _snwprintf(buf, buf_len, L"node-debug-handler-%u", pid);
}


static int RegisterDebugSignalHandler() {
char mapping_name[32];
wchar_t mapping_name[32];
HANDLE mapping_handle;
DWORD pid;
LPTHREAD_START_ROUTINE* handler;
Expand All @@ -2376,11 +2377,11 @@ static int RegisterDebugSignalHandler() {

if (GetDebugSignalHandlerMappingName(pid,
mapping_name,
sizeof mapping_name) < 0) {
ARRAY_SIZE(mapping_name)) < 0) {
return -1;
}

mapping_handle = CreateFileMappingA(INVALID_HANDLE_VALUE,
mapping_handle = CreateFileMappingW(INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
Expand All @@ -2390,11 +2391,12 @@ static int RegisterDebugSignalHandler() {
return -1;
}

handler = (LPTHREAD_START_ROUTINE*) MapViewOfFile(mapping_handle,
FILE_MAP_ALL_ACCESS,
0,
0,
sizeof *handler);
handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
MapViewOfFile(mapping_handle,
FILE_MAP_ALL_ACCESS,
0,
0,
sizeof *handler));
if (handler == NULL) {
CloseHandle(mapping_handle);
return -1;
Expand All @@ -2415,7 +2417,7 @@ static Handle<Value> DebugProcess(const Arguments& args) {
HANDLE process = NULL;
HANDLE thread = NULL;
HANDLE mapping = NULL;
char mapping_name[32];
wchar_t mapping_name[32];
LPTHREAD_START_ROUTINE* handler = NULL;

if (args.Length() != 1) {
Expand All @@ -2437,22 +2439,24 @@ static Handle<Value> DebugProcess(const Arguments& args) {

if (GetDebugSignalHandlerMappingName(pid,
mapping_name,
sizeof mapping_name) < 0) {
ARRAY_SIZE(mapping_name)) < 0) {
rv = ThrowException(ErrnoException(errno, "sprintf"));
goto out;
}

mapping = OpenFileMapping(FILE_MAP_READ, FALSE, mapping_name);
mapping = OpenFileMappingW(FILE_MAP_READ, FALSE, mapping_name);
if (mapping == NULL) {
rv = ThrowException(WinapiErrnoException(GetLastError(), "sprintf"));
rv = ThrowException(WinapiErrnoException(GetLastError(),
"OpenFileMappingW"));
goto out;
}

handler = (LPTHREAD_START_ROUTINE*) MapViewOfFile(mapping,
FILE_MAP_READ,
0,
0,
sizeof *handler);
handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
MapViewOfFile(mapping,
FILE_MAP_READ,
0,
0,
sizeof *handler));
if (handler == NULL || *handler == NULL) {
rv = ThrowException(WinapiErrnoException(GetLastError(), "MapViewOfFile"));
goto out;
Expand Down
57 changes: 34 additions & 23 deletions src/platform_win32.cc
Expand Up @@ -48,9 +48,10 @@ double Platform::prog_start_time = Platform::GetUptime();
const char *winapi_strerror(const int errorno) {
char *errmsg = NULL;

FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPSTR>(&errmsg), 0, NULL);

if (errmsg) {
// Remove trailing newlines
Expand All @@ -72,9 +73,10 @@ void winapi_perror(const char* prefix = NULL) {
DWORD errorno = GetLastError();
const char *errmsg = NULL;

FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPSTR>(&errmsg), 0, NULL);

if (!errmsg) {
errmsg = "Unknown error\n";
Expand Down Expand Up @@ -203,8 +205,7 @@ int Platform::GetMemory(size_t *rss) {
HANDLE current_process = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS pmc;

if ( !GetProcessMemoryInfo( current_process, &pmc, sizeof(pmc)) )
{
if (!GetProcessMemoryInfo(current_process, &pmc, sizeof(pmc))) {
winapi_perror("GetProcessMemoryInfo");
}

Expand All @@ -220,40 +221,49 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {

for (int i = 0; i < 32; i++) {

char key[128] = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\";
char processor_number[32];
itoa(i, processor_number, 10);
strncat(key, processor_number, 2);
wchar_t key[128] = L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\";
wchar_t processor_number[32];
_itow(i, processor_number, 10);
wcsncat(key, processor_number, 2);

HKEY processor_key = NULL;

DWORD cpu_speed = 0;
DWORD cpu_speed_length = sizeof(cpu_speed);

char cpu_brand[256];
wchar_t cpu_brand[256];
DWORD cpu_brand_length = sizeof(cpu_brand);

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_QUERY_VALUE,
&processor_key) != ERROR_SUCCESS) {
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
key,
0,
KEY_QUERY_VALUE,
&processor_key) != ERROR_SUCCESS) {
if (i == 0) {
winapi_perror("RegOpenKeyEx");
winapi_perror("RegOpenKeyExW");
return -1;
}

continue;
}

if (RegQueryValueEx(processor_key, "~MHz", NULL, NULL,
(LPBYTE)&cpu_speed, &cpu_speed_length)
!= ERROR_SUCCESS) {
winapi_perror("RegQueryValueEx");
if (RegQueryValueExW(processor_key,
L"~MHz",
NULL,
NULL,
reinterpret_cast<LPBYTE>(&cpu_speed),
&cpu_speed_length) != ERROR_SUCCESS) {
winapi_perror("RegQueryValueExW");
return -1;
}

if (RegQueryValueEx(processor_key, "ProcessorNameString", NULL, NULL,
(LPBYTE)&cpu_brand, &cpu_brand_length)
!= ERROR_SUCCESS) {
winapi_perror("RegQueryValueEx");
if (RegQueryValueExW(processor_key,
L"ProcessorNameString",
NULL,
NULL,
reinterpret_cast<LPBYTE>(&cpu_brand),
&cpu_brand_length) != ERROR_SUCCESS) {
winapi_perror("RegQueryValueExW");
return -1;
}

Expand All @@ -267,7 +277,8 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
times_info->Set(String::New("irq"), Integer::New(0));

Local<Object> cpu_info = Object::New();
cpu_info->Set(String::New("model"), String::New(cpu_brand));
cpu_info->Set(String::New("model"),
String::New(reinterpret_cast<uint16_t*>(cpu_brand)));
cpu_info->Set(String::New("speed"), Integer::New(cpu_speed));
cpu_info->Set(String::New("times"), times_info);
(*cpus)->Set(i,cpu_info);
Expand Down

0 comments on commit 8f2694b

Please sign in to comment.