1 /*
2 * Copyright 2004-2008 the Seasar Foundation and the Others.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13 * either express or implied. See the License for the specific language
14 * governing permissions and limitations under the License.
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 * Win32API コールに関するユーティリティクラスです。<br />
25 *
26 * @author y-komori
27 */
28 public class Win32Util {
29 private Win32Util() {
30
31 }
32
33 /**
34 * OSから与えられた戻り値をもとにメッセージを取得します。<br />
35 * <p>
36 * 出典 : http://homepage2.nifty.com/igat/igapyon/diary/2005/ig051228.html
37 * </p>
38 *
39 * @param rc
40 * Win32APIから返却されるDWORD値
41 * @return OSから得られたメッセージ
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 * <code>LPMSGBUF</code> を {@link String} オブジェクトに変換します。<br />
57 * <p>
58 * 出典 : http://homepage2.nifty.com/igat/igapyon/diary/2005/ig051228.html
59 * </p>
60 *
61 * @param lpMsgBuf
62 * C言語上としての文字列
63 * @return {@link String} 化された文字列
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 // NULLが現れたら中断。
70 break;
71 }
72 result.write(lpMsgBuf[index]);
73
74 if (lpMsgBuf[index] / 0x10000 == 0) {
75 // NULLが現れたら中断。
76 break;
77 }
78 result.write(lpMsgBuf[index] / 0x10000);
79 }
80 result.flush();
81 return result.toString();
82 }
83 }