Skip to content
This repository
Browse code

Windows/cygwin: no more GetConsoleTitleW errors on XP

  • Loading branch information...
commit 78487b6256ebbd8c3c0e16abe18f26c2db23d0a4 1 parent 691497b
Bert Belder authored July 22, 2011
47  src/platform_cygwin.cc
@@ -64,15 +64,21 @@ char** Platform::SetupArgs(int argc, char *argv[]) {
64 64
 }
65 65
 
66 66
 
  67
+// Max title length; the only thing MSDN tells us about the maximum length
  68
+// of the console title is that it is smaller than 64K. However in practice
  69
+// it is much smaller, and there is no way to figure out what the exact length
  70
+// of the title is or can be, at least not on XP. To make it even more
  71
+// annoying, GetConsoleTitle failes when the buffer to be read into is bigger
  72
+// than the actual maximum length. So we make a conservative guess here;
  73
+// just don't put the novel you're writing in the title, unless the plot
  74
+// survives truncation.
  75
+#define MAX_TITLE_LENGTH 8192
  76
+
67 77
 void Platform::SetProcessTitle(char *title) {
68 78
   // We need to convert _title_ to UTF-16 first, because that's what windows uses internally.
69 79
   // It would be more efficient to use the UTF-16 value that we can obtain from v8,
70 80
   // but it's not accessible from here.
71 81
 
72  
-  // Max title length; according to the specs it should be 64K but in practice it's a little over 30000,
73  
-  // but who needs titles that long anyway?
74  
-  const int MAX_TITLE_LENGTH = 30001;
75  
-
76 82
   int length;
77 83
   WCHAR *title_w;
78 84
 
@@ -109,60 +115,37 @@ void Platform::SetProcessTitle(char *title) {
109 115
 
110 116
 
111 117
 static inline char* _getProcessTitle() {
112  
-  WCHAR *title_w;
  118
+  WCHAR title_w[MAX_TITLE_LENGTH];
113 119
   char *title;
114  
-  int length, length_w;
115  
-
116  
-  length_w = GetConsoleTitleW((WCHAR*)L"\0", sizeof(WCHAR));
117  
-
118  
-  // If length is zero, there may be an error or the title may be empty
119  
-  if (!length_w) {
120  
-    if (GetLastError()) {
121  
-      _winapi_perror("GetConsoleTitleW");
122  
-      return NULL;
123  
-    } else {
124  
-      // The title is empty, so return empty string
125  
-      process_title = strdup("\0");
126  
-      return process_title;
127  
-    }
128  
-  }
129  
-
130  
-  // Room for \0 terminator
131  
-  length_w++;
  120
+  int result, length;
132 121
 
133  
-  title_w = new WCHAR[length_w];
  122
+  result = GetConsoleTitleW(title_w, sizeof(title_w) / sizeof(WCHAR));
134 123
 
135  
-  if (!GetConsoleTitleW(title_w, length_w * sizeof(WCHAR))) {
  124
+  if (result == 0) {
136 125
     _winapi_perror("GetConsoleTitleW");
137  
-    delete title_w;
138 126
     return NULL;
139 127
   }
140 128
 
141 129
   // Find out what the size of the buffer is that we need
142  
-  length = WideCharToMultiByte(CP_UTF8, 0, title_w, length_w, NULL, 0, NULL, NULL);
  130
+  length = WideCharToMultiByte(CP_UTF8, 0, title_w, -1, NULL, 0, NULL, NULL);
143 131
   if (!length) {
144 132
     _winapi_perror("WideCharToMultiByte");
145  
-    delete title_w;
146 133
     return NULL;
147 134
   }
148 135
 
149 136
   title = (char *) malloc(length);
150 137
   if (!title) {
151 138
     perror("malloc");
152  
-    delete title_w;
153 139
     return NULL;
154 140
   }
155 141
 
156 142
   // Do utf16 -> utf8 conversion here
157 143
   if (!WideCharToMultiByte(CP_UTF8, 0, title_w, -1, title, length, NULL, NULL)) {
158 144
     _winapi_perror("WideCharToMultiByte");
159  
-    delete title_w;
160 145
     free(title);
161 146
     return NULL;
162 147
   }
163 148
 
164  
-  delete title_w;
165  
-
166 149
   return title;
167 150
 }
168 151
 
48  src/platform_win32.cc
@@ -93,15 +93,21 @@ char** Platform::SetupArgs(int argc, char *argv[]) {
93 93
 }
94 94
 
95 95
 
  96
+// Max title length; the only thing MSDN tells us about the maximum length
  97
+// of the console title is that it is smaller than 64K. However in practice
  98
+// it is much smaller, and there is no way to figure out what the exact length
  99
+// of the title is or can be, at least not on XP. To make it even more
  100
+// annoying, GetConsoleTitle failes when the buffer to be read into is bigger
  101
+// than the actual maximum length. So we make a conservative guess here;
  102
+// just don't put the novel you're writing in the title, unless the plot
  103
+// survives truncation.
  104
+#define MAX_TITLE_LENGTH 8192
  105
+
96 106
 void Platform::SetProcessTitle(char *title) {
97 107
   // We need to convert _title_ to UTF-16 first, because that's what windows uses internally.
98 108
   // It would be more efficient to use the UTF-16 value that we can obtain from v8,
99 109
   // but it's not accessible from here.
100 110
 
101  
-  // Max title length; according to the specs it should be 64K but in practice it's a little over 30000,
102  
-  // but who needs titles that long anyway?
103  
-  const int MAX_TITLE_LENGTH = 30001;
104  
-
105 111
   int length;
106 112
   WCHAR *title_w;
107 113
 
@@ -138,61 +144,37 @@ void Platform::SetProcessTitle(char *title) {
138 144
 
139 145
 
140 146
 static inline char* _getProcessTitle() {
141  
-  WCHAR *title_w;
  147
+  WCHAR title_w[MAX_TITLE_LENGTH];
142 148
   char *title;
143  
-  int length, length_w;
144  
-
145  
-  length_w = GetConsoleTitleW((WCHAR*)L"\0", sizeof(WCHAR));
146  
-
147  
-  // If length is zero, there may be an error or the title may be empty
148  
-  if (!length_w) {
149  
-    if (GetLastError()) {
150  
-      winapi_perror("GetConsoleTitleW");
151  
-      return NULL;
152  
-    }
153  
-    else {
154  
-      // The title is empty, so return empty string
155  
-      process_title = strdup("\0");
156  
-      return process_title;
157  
-    }
158  
-  }
  149
+  int result, length;
159 150
 
160  
-  // Room for \0 terminator
161  
-  length_w++;
  151
+  result = GetConsoleTitleW(title_w, sizeof(title_w) / sizeof(WCHAR));
162 152
 
163  
-  title_w = new WCHAR[length_w];
164  
-
165  
-  if (!GetConsoleTitleW(title_w, length_w * sizeof(WCHAR))) {
  153
+  if (result == 0) {
166 154
     winapi_perror("GetConsoleTitleW");
167  
-    delete title_w;
168 155
     return NULL;
169 156
   }
170 157
 
171 158
   // Find out what the size of the buffer is that we need
172  
-  length = WideCharToMultiByte(CP_UTF8, 0, title_w, length_w, NULL, 0, NULL, NULL);
  159
+  length = WideCharToMultiByte(CP_UTF8, 0, title_w, -1, NULL, 0, NULL, NULL);
173 160
   if (!length) {
174 161
     winapi_perror("WideCharToMultiByte");
175  
-    delete title_w;
176 162
     return NULL;
177 163
   }
178 164
 
179 165
   title = (char *) malloc(length);
180 166
   if (!title) {
181 167
     perror("malloc");
182  
-    delete title_w;
183 168
     return NULL;
184 169
   }
185 170
 
186 171
   // Do utf16 -> utf8 conversion here
187 172
   if (!WideCharToMultiByte(CP_UTF8, 0, title_w, -1, title, length, NULL, NULL)) {
188 173
     winapi_perror("WideCharToMultiByte");
189  
-    delete title_w;
190 174
     free(title);
191 175
     return NULL;
192 176
   }
193 177
 
194  
-  delete title_w;
195  
-
196 178
   return title;
197 179
 }
198 180
 

0 notes on commit 78487b6

Please sign in to comment.
Something went wrong with that request. Please try again.