Browse Source

Windows/cygwin: no more GetConsoleTitleW errors on XP

v0.7.4-release
Bert Belder 14 years ago
parent
commit
7a6a77e07c
  1. 47
      src/platform_cygwin.cc
  2. 48
      src/platform_win32.cc

47
src/platform_cygwin.cc

@ -64,15 +64,21 @@ char** Platform::SetupArgs(int argc, char *argv[]) {
}
// Max title length; the only thing MSDN tells us about the maximum length
// of the console title is that it is smaller than 64K. However in practice
// it is much smaller, and there is no way to figure out what the exact length
// of the title is or can be, at least not on XP. To make it even more
// annoying, GetConsoleTitle failes when the buffer to be read into is bigger
// than the actual maximum length. So we make a conservative guess here;
// just don't put the novel you're writing in the title, unless the plot
// survives truncation.
#define MAX_TITLE_LENGTH 8192
void Platform::SetProcessTitle(char *title) {
// We need to convert _title_ to UTF-16 first, because that's what windows uses internally.
// It would be more efficient to use the UTF-16 value that we can obtain from v8,
// but it's not accessible from here.
// Max title length; according to the specs it should be 64K but in practice it's a little over 30000,
// but who needs titles that long anyway?
const int MAX_TITLE_LENGTH = 30001;
int length;
WCHAR *title_w;
@ -109,60 +115,37 @@ void Platform::SetProcessTitle(char *title) {
static inline char* _getProcessTitle() {
WCHAR *title_w;
WCHAR title_w[MAX_TITLE_LENGTH];
char *title;
int length, length_w;
length_w = GetConsoleTitleW((WCHAR*)L"\0", sizeof(WCHAR));
// If length is zero, there may be an error or the title may be empty
if (!length_w) {
if (GetLastError()) {
_winapi_perror("GetConsoleTitleW");
return NULL;
} else {
// The title is empty, so return empty string
process_title = strdup("\0");
return process_title;
}
}
// Room for \0 terminator
length_w++;
int result, length;
title_w = new WCHAR[length_w];
result = GetConsoleTitleW(title_w, sizeof(title_w) / sizeof(WCHAR));
if (!GetConsoleTitleW(title_w, length_w * sizeof(WCHAR))) {
if (result == 0) {
_winapi_perror("GetConsoleTitleW");
delete title_w;
return NULL;
}
// Find out what the size of the buffer is that we need
length = WideCharToMultiByte(CP_UTF8, 0, title_w, length_w, NULL, 0, NULL, NULL);
length = WideCharToMultiByte(CP_UTF8, 0, title_w, -1, NULL, 0, NULL, NULL);
if (!length) {
_winapi_perror("WideCharToMultiByte");
delete title_w;
return NULL;
}
title = (char *) malloc(length);
if (!title) {
perror("malloc");
delete title_w;
return NULL;
}
// Do utf16 -> utf8 conversion here
if (!WideCharToMultiByte(CP_UTF8, 0, title_w, -1, title, length, NULL, NULL)) {
_winapi_perror("WideCharToMultiByte");
delete title_w;
free(title);
return NULL;
}
delete title_w;
return title;
}

48
src/platform_win32.cc

@ -94,15 +94,21 @@ char** Platform::SetupArgs(int argc, char *argv[]) {
}
// Max title length; the only thing MSDN tells us about the maximum length
// of the console title is that it is smaller than 64K. However in practice
// it is much smaller, and there is no way to figure out what the exact length
// of the title is or can be, at least not on XP. To make it even more
// annoying, GetConsoleTitle failes when the buffer to be read into is bigger
// than the actual maximum length. So we make a conservative guess here;
// just don't put the novel you're writing in the title, unless the plot
// survives truncation.
#define MAX_TITLE_LENGTH 8192
void Platform::SetProcessTitle(char *title) {
// We need to convert _title_ to UTF-16 first, because that's what windows uses internally.
// It would be more efficient to use the UTF-16 value that we can obtain from v8,
// but it's not accessible from here.
// Max title length; according to the specs it should be 64K but in practice it's a little over 30000,
// but who needs titles that long anyway?
const int MAX_TITLE_LENGTH = 30001;
int length;
WCHAR *title_w;
@ -139,61 +145,37 @@ void Platform::SetProcessTitle(char *title) {
static inline char* _getProcessTitle() {
WCHAR *title_w;
WCHAR title_w[MAX_TITLE_LENGTH];
char *title;
int length, length_w;
length_w = GetConsoleTitleW((WCHAR*)L"\0", sizeof(WCHAR));
// If length is zero, there may be an error or the title may be empty
if (!length_w) {
if (GetLastError()) {
winapi_perror("GetConsoleTitleW");
return NULL;
}
else {
// The title is empty, so return empty string
process_title = strdup("\0");
return process_title;
}
}
int result, length;
// Room for \0 terminator
length_w++;
result = GetConsoleTitleW(title_w, sizeof(title_w) / sizeof(WCHAR));
title_w = new WCHAR[length_w];
if (!GetConsoleTitleW(title_w, length_w * sizeof(WCHAR))) {
if (result == 0) {
winapi_perror("GetConsoleTitleW");
delete title_w;
return NULL;
}
// Find out what the size of the buffer is that we need
length = WideCharToMultiByte(CP_UTF8, 0, title_w, length_w, NULL, 0, NULL, NULL);
length = WideCharToMultiByte(CP_UTF8, 0, title_w, -1, NULL, 0, NULL, NULL);
if (!length) {
winapi_perror("WideCharToMultiByte");
delete title_w;
return NULL;
}
title = (char *) malloc(length);
if (!title) {
perror("malloc");
delete title_w;
return NULL;
}
// Do utf16 -> utf8 conversion here
if (!WideCharToMultiByte(CP_UTF8, 0, title_w, -1, title, length, NULL, NULL)) {
winapi_perror("WideCharToMultiByte");
delete title_w;
free(title);
return NULL;
}
delete title_w;
return title;
}

Loading…
Cancel
Save