1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.uruma.util.win32;
17
18 import java.io.StringWriter;
19
20 import org.eclipse.swt.internal.win32.OS;
21 import org.seasar.uruma.exception.Win32ApiException;
22
23
24
25
26
27
28 public class Win32Util {
29 private Win32Util() {
30
31 }
32
33
34
35
36
37
38
39
40
41
42
43 public static String formatMessage(final int rc) {
44 final int[] lpMsgBuf = new int[2048];
45 final int retCode = OS.FormatMessage(OS.FORMAT_MESSAGE_FROM_SYSTEM, 0,
46 rc, 0, lpMsgBuf, lpMsgBuf.length, 0);
47 if (retCode == 0) {
48 throw new Win32ApiException("FormatMessage", Integer.valueOf(
49 OS.GetLastError()).toString());
50 }
51
52 return lpmsgbuf2String(lpMsgBuf) + "(" + rc + ")";
53 }
54
55
56
57
58
59
60
61
62
63
64
65 public static String lpmsgbuf2String(final int[] lpMsgBuf) {
66 final StringWriter result = new StringWriter();
67 for (int index = 0; index < lpMsgBuf.length; index++) {
68 if (lpMsgBuf[index] == 0) {
69
70 break;
71 }
72 result.write(lpMsgBuf[index]);
73
74 if (lpMsgBuf[index] / 0x10000 == 0) {
75
76 break;
77 }
78 result.write(lpMsgBuf[index] / 0x10000);
79 }
80 result.flush();
81 return result.toString();
82 }
83 }