Browse Source

Make win32 ansi api usage explicit

Use widechar versions in a couple of places.
Don't use C-style cast in C++ code.
v0.8.7-release
Bert Belder 13 years ago
parent
commit
8f2694bb53
  1. 42
      src/node.cc
  2. 57
      src/platform_win32.cc

42
src/node.cc

@ -2361,13 +2361,14 @@ DWORD WINAPI EnableDebugThreadProc(void* arg) {
} }
static int GetDebugSignalHandlerMappingName(DWORD pid, char* buf, size_t buf_len) { static int GetDebugSignalHandlerMappingName(DWORD pid, wchar_t* buf,
return snprintf(buf, buf_len, "node-debug-handler-%u", pid); size_t buf_len) {
return _snwprintf(buf, buf_len, L"node-debug-handler-%u", pid);
} }
static int RegisterDebugSignalHandler() { static int RegisterDebugSignalHandler() {
char mapping_name[32]; wchar_t mapping_name[32];
HANDLE mapping_handle; HANDLE mapping_handle;
DWORD pid; DWORD pid;
LPTHREAD_START_ROUTINE* handler; LPTHREAD_START_ROUTINE* handler;
@ -2376,11 +2377,11 @@ static int RegisterDebugSignalHandler() {
if (GetDebugSignalHandlerMappingName(pid, if (GetDebugSignalHandlerMappingName(pid,
mapping_name, mapping_name,
sizeof mapping_name) < 0) { ARRAY_SIZE(mapping_name)) < 0) {
return -1; return -1;
} }
mapping_handle = CreateFileMappingA(INVALID_HANDLE_VALUE, mapping_handle = CreateFileMappingW(INVALID_HANDLE_VALUE,
NULL, NULL,
PAGE_READWRITE, PAGE_READWRITE,
0, 0,
@ -2390,11 +2391,12 @@ static int RegisterDebugSignalHandler() {
return -1; return -1;
} }
handler = (LPTHREAD_START_ROUTINE*) MapViewOfFile(mapping_handle, handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
FILE_MAP_ALL_ACCESS, MapViewOfFile(mapping_handle,
0, FILE_MAP_ALL_ACCESS,
0, 0,
sizeof *handler); 0,
sizeof *handler));
if (handler == NULL) { if (handler == NULL) {
CloseHandle(mapping_handle); CloseHandle(mapping_handle);
return -1; return -1;
@ -2415,7 +2417,7 @@ static Handle<Value> DebugProcess(const Arguments& args) {
HANDLE process = NULL; HANDLE process = NULL;
HANDLE thread = NULL; HANDLE thread = NULL;
HANDLE mapping = NULL; HANDLE mapping = NULL;
char mapping_name[32]; wchar_t mapping_name[32];
LPTHREAD_START_ROUTINE* handler = NULL; LPTHREAD_START_ROUTINE* handler = NULL;
if (args.Length() != 1) { if (args.Length() != 1) {
@ -2437,22 +2439,24 @@ static Handle<Value> DebugProcess(const Arguments& args) {
if (GetDebugSignalHandlerMappingName(pid, if (GetDebugSignalHandlerMappingName(pid,
mapping_name, mapping_name,
sizeof mapping_name) < 0) { ARRAY_SIZE(mapping_name)) < 0) {
rv = ThrowException(ErrnoException(errno, "sprintf")); rv = ThrowException(ErrnoException(errno, "sprintf"));
goto out; goto out;
} }
mapping = OpenFileMapping(FILE_MAP_READ, FALSE, mapping_name); mapping = OpenFileMappingW(FILE_MAP_READ, FALSE, mapping_name);
if (mapping == NULL) { if (mapping == NULL) {
rv = ThrowException(WinapiErrnoException(GetLastError(), "sprintf")); rv = ThrowException(WinapiErrnoException(GetLastError(),
"OpenFileMappingW"));
goto out; goto out;
} }
handler = (LPTHREAD_START_ROUTINE*) MapViewOfFile(mapping, handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
FILE_MAP_READ, MapViewOfFile(mapping,
0, FILE_MAP_READ,
0, 0,
sizeof *handler); 0,
sizeof *handler));
if (handler == NULL || *handler == NULL) { if (handler == NULL || *handler == NULL) {
rv = ThrowException(WinapiErrnoException(GetLastError(), "MapViewOfFile")); rv = ThrowException(WinapiErrnoException(GetLastError(), "MapViewOfFile"));
goto out; goto out;

57
src/platform_win32.cc

@ -48,9 +48,10 @@ double Platform::prog_start_time = Platform::GetUptime();
const char *winapi_strerror(const int errorno) { const char *winapi_strerror(const int errorno) {
char *errmsg = NULL; 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, 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) { if (errmsg) {
// Remove trailing newlines // Remove trailing newlines
@ -72,9 +73,10 @@ void winapi_perror(const char* prefix = NULL) {
DWORD errorno = GetLastError(); DWORD errorno = GetLastError();
const char *errmsg = NULL; 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, 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) { if (!errmsg) {
errmsg = "Unknown error\n"; errmsg = "Unknown error\n";
@ -203,8 +205,7 @@ int Platform::GetMemory(size_t *rss) {
HANDLE current_process = GetCurrentProcess(); HANDLE current_process = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS pmc; PROCESS_MEMORY_COUNTERS pmc;
if ( !GetProcessMemoryInfo( current_process, &pmc, sizeof(pmc)) ) if (!GetProcessMemoryInfo(current_process, &pmc, sizeof(pmc))) {
{
winapi_perror("GetProcessMemoryInfo"); winapi_perror("GetProcessMemoryInfo");
} }
@ -220,40 +221,49 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
for (int i = 0; i < 32; i++) { for (int i = 0; i < 32; i++) {
char key[128] = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\"; wchar_t key[128] = L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\";
char processor_number[32]; wchar_t processor_number[32];
itoa(i, processor_number, 10); _itow(i, processor_number, 10);
strncat(key, processor_number, 2); wcsncat(key, processor_number, 2);
HKEY processor_key = NULL; HKEY processor_key = NULL;
DWORD cpu_speed = 0; DWORD cpu_speed = 0;
DWORD cpu_speed_length = sizeof(cpu_speed); DWORD cpu_speed_length = sizeof(cpu_speed);
char cpu_brand[256]; wchar_t cpu_brand[256];
DWORD cpu_brand_length = sizeof(cpu_brand); DWORD cpu_brand_length = sizeof(cpu_brand);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_QUERY_VALUE, if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
&processor_key) != ERROR_SUCCESS) { key,
0,
KEY_QUERY_VALUE,
&processor_key) != ERROR_SUCCESS) {
if (i == 0) { if (i == 0) {
winapi_perror("RegOpenKeyEx"); winapi_perror("RegOpenKeyExW");
return -1; return -1;
} }
continue; continue;
} }
if (RegQueryValueEx(processor_key, "~MHz", NULL, NULL, if (RegQueryValueExW(processor_key,
(LPBYTE)&cpu_speed, &cpu_speed_length) L"~MHz",
!= ERROR_SUCCESS) { NULL,
winapi_perror("RegQueryValueEx"); NULL,
reinterpret_cast<LPBYTE>(&cpu_speed),
&cpu_speed_length) != ERROR_SUCCESS) {
winapi_perror("RegQueryValueExW");
return -1; return -1;
} }
if (RegQueryValueEx(processor_key, "ProcessorNameString", NULL, NULL, if (RegQueryValueExW(processor_key,
(LPBYTE)&cpu_brand, &cpu_brand_length) L"ProcessorNameString",
!= ERROR_SUCCESS) { NULL,
winapi_perror("RegQueryValueEx"); NULL,
reinterpret_cast<LPBYTE>(&cpu_brand),
&cpu_brand_length) != ERROR_SUCCESS) {
winapi_perror("RegQueryValueExW");
return -1; return -1;
} }
@ -267,7 +277,8 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
times_info->Set(String::New("irq"), Integer::New(0)); times_info->Set(String::New("irq"), Integer::New(0));
Local<Object> cpu_info = Object::New(); 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("speed"), Integer::New(cpu_speed));
cpu_info->Set(String::New("times"), times_info); cpu_info->Set(String::New("times"), times_info);
(*cpus)->Set(i,cpu_info); (*cpus)->Set(i,cpu_info);

Loading…
Cancel
Save