@@ -178,22 +178,43 @@ public static void closeHandle(HANDLE h) {
178
178
}
179
179
}
180
180
181
+ /**
182
+ * Format a message from a code.
183
+ *
184
+ * @param code The error code
185
+ * @return Formatted message in the default locale.
186
+ */
187
+ public static String formatMessage (int code ) {
188
+ return formatMessage (code , 0 , 0 );
189
+ }
190
+
181
191
/**
182
192
* Format a message from the value obtained from
183
193
* {@link Kernel32#GetLastError()} or {@link Native#getLastError()}.
184
194
*
195
+ * <p>If you pass in zero, FormatMessage looks for a message for LANGIDs in the following order:</p>
196
+ * <ol>
197
+ * <li>Language neutral</li>
198
+ * <li>Thread LANGID, based on the thread's locale value</li>
199
+ * <li>User default LANGID, based on the user's default locale value</li>
200
+ * <li>System default LANGID, based on the system default locale value</li>
201
+ * <li>US English</li>
202
+ * </ol>
203
+ *
185
204
* @param code The error code
186
- * @return Formatted message.
205
+ * @param primaryLangId The primary language identifier
206
+ * @param sublangId The sublanguage identifier
207
+ * @return Formatted message in the specified locale.
187
208
*/
188
- public static String formatMessage (int code ) {
209
+ public static String formatMessage (int code , int primaryLangId , int sublangId ) {
189
210
PointerByReference buffer = new PointerByReference ();
190
211
int nLen = Kernel32 .INSTANCE .FormatMessage (
191
212
WinBase .FORMAT_MESSAGE_ALLOCATE_BUFFER
192
213
| WinBase .FORMAT_MESSAGE_FROM_SYSTEM
193
214
| WinBase .FORMAT_MESSAGE_IGNORE_INSERTS ,
194
215
null ,
195
216
code ,
196
- 0 , // TODO: // MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT)
217
+ WinNT . LocaleMacros . MAKELANGID (primaryLangId , sublangId ),
197
218
buffer , 0 , null );
198
219
if (nLen == 0 ) {
199
220
throw new LastErrorException (Native .getLastError ());
@@ -213,32 +234,71 @@ public static String formatMessage(int code) {
213
234
*
214
235
* @param code
215
236
* HRESULT
216
- * @return Formatted message.
237
+ * @return Formatted message in the default locale .
217
238
*/
218
239
public static String formatMessage (HRESULT code ) {
219
240
return formatMessage (code .intValue ());
220
241
}
221
242
243
+ /**
244
+ * Format a message from an HRESULT.
245
+ *
246
+ * @param code
247
+ * HRESULT
248
+ * @param primaryLangId
249
+ * The primary language identifier
250
+ * @param sublangId
251
+ * The primary language identifier
252
+ * @return Formatted message in the specified locale.
253
+ */
254
+ public static String formatMessage (HRESULT code , int primaryLangId , int sublangId ) {
255
+ return formatMessage (code .intValue (), primaryLangId , sublangId );
256
+ }
257
+
222
258
/**
223
259
* Format a system message from an error code.
224
260
*
225
261
* @param code
226
262
* Error code, typically a result of GetLastError.
227
- * @return Formatted message.
263
+ * @return Formatted message in the default locale .
228
264
*/
229
265
public static String formatMessageFromLastErrorCode (int code ) {
230
266
return formatMessage (W32Errors .HRESULT_FROM_WIN32 (code ));
231
267
}
232
268
269
+ /**
270
+ * Format a system message from an error code.
271
+ *
272
+ * @param code
273
+ * Error code, typically a result of GetLastError.
274
+ * @param primaryLangId
275
+ * The primary language identifier
276
+ * @param sublangId
277
+ * The primary language identifier
278
+ * @return Formatted message in the specified locale.
279
+ */
280
+ public static String formatMessageFromLastErrorCode (int code , int primaryLangId , int sublangId ) {
281
+ return formatMessage (W32Errors .HRESULT_FROM_WIN32 (code ), primaryLangId , sublangId );
282
+ }
283
+
233
284
/**
234
285
* @return Obtains the human-readable error message text from the last error
235
- * that occurred by invocating {@code Kernel32.GetLastError()}.
286
+ * that occurred by invocating {@code Kernel32.GetLastError()} in the default locale .
236
287
*/
237
288
public static String getLastErrorMessage () {
238
289
return Kernel32Util .formatMessageFromLastErrorCode (Kernel32 .INSTANCE
239
290
.GetLastError ());
240
291
}
241
292
293
+ /**
294
+ * @return Obtains the human-readable error message text from the last error
295
+ * that occurred by invocating {@code Kernel32.GetLastError()} in the specified locale.
296
+ */
297
+ public static String getLastErrorMessage (int primaryLangId , int sublangId ) {
298
+ return Kernel32Util .formatMessageFromLastErrorCode (Kernel32 .INSTANCE
299
+ .GetLastError (), primaryLangId , sublangId );
300
+ }
301
+
242
302
/**
243
303
* Return the path designated for temporary files.
244
304
*
0 commit comments