source: trunk/ab5.0/ablib/src/Classes/ActiveBasic/Windows/Windows.ab@ 603

Last change on this file since 603 was 603, checked in by イグトランス (egtra), 16 years ago

[601][602]でのコミットし忘れ分と細かい修正

File size: 3.5 KB
Line 
1'Classes/ActiveBasic/Windows/Windows.ab
2
3Namespace ActiveBasic
4Namespace Windows
5
6Function GetPathFromIDList(pidl As LPITEMIDLIST) As String
7 Dim buf[ELM(MAX_PATH)] As TCHAR
8 If SHGetPathFromIDList(pidl, buf) Then
9 Return New String(buf)
10 Else
11 Return ""
12 End If
13End Function
14
15Function GetFolderPath(hwnd As HWND, folder As Long) As String
16 Dim pidl As LPITEMIDLIST
17 Dim hr = SHGetSpecialFolderLocation(hwnd, folder, pidl)
18 If SUCCEEDED(hr) Then
19 GetFolderPath = GetPathFromIDList(pidl)
20 CoTaskMemFree(pidl)
21 Else
22 GetFolderPath = ""
23 End If
24End Function
25
26Function GetFolderPath(folder As Long) As String
27 Return GetFolderPath(0, folder)
28End Function
29/*
30Function MessageBox(hw As HWND, s As PCSTR, t As PCSTR, b As DWord) As DWord
31 Return MessageBoxA(hw, s, t, b)
32End Function
33
34Function MessageBox(hw As HWND, s As PCWSTR, t As PCWSTR, b As DWord) As DWord
35 Return MessageBoxW(hw, s, t, b)
36End Function
37*/
38
39Namespace Detail
40Function _System_MessageBox(hw As HWND, s As PCSTR, t As PCSTR, b As DWord) As DWord
41 Return MessageBoxA(hw, s, t, b)
42End Function
43
44Function _System_MessageBox(hw As HWND, s As PCWSTR, t As PCWSTR, b As DWord) As DWord
45 Return MessageBoxW(hw, s, t, b)
46End Function
47End Namespace
48
49Function HResultToString(hr As HRESULT) As String
50 Dim pszMsg As PCSTR
51 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER Or FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS,
52 0, hr, LANG_USER_DEFAULT, VarPtr(pszMsg) As PTSTR, 0, 0)
53 If pszMsg <> 0 Then
54 HResultToString = New String(pszMsg)
55 LocalFree(pszMsg)
56 End If
57End Function
58
59/*!
60@brief Windows/COMのエラーを伝える例外クラス
61@date 2008/07/13
62@auther Egtra
63*/
64Class WindowsException
65 Inherits System.Exception
66Public
67 /*!
68 @biref コンストラクタ
69 */
70 Sub WindowsException()
71 Exception()
72 HResult = E_FAIL
73 End Sub
74 /*!
75 @biref コンストラクタ
76 @param[in] hr エラー値
77 */
78 Sub WindowsException(hr As HRESULT)
79 Exception(HResultToString(hr))
80 HResult = hr
81 End Sub
82 /*!
83 @biref コンストラクタ
84 @param[in] message エラーメッセージ
85 */
86 Sub WindowsException(message As String)
87 Exception(message)
88 HResult = E_FAIL
89 End Sub
90 /*!
91 @biref コンストラクタ
92 @param[in] hr エラー値
93 @param[in] message エラーメッセージ
94 */
95 Sub WindowsException(hr As HRESULT, message As String)
96 Exception(message)
97 HResult = hr
98 End Sub
99 /*!
100 @biref コンストラクタ
101 @param[in] message エラーメッセージ
102 @param[in] innerException 内部例外
103 */
104 Sub WindowsException(message As String, innerException As Exception)
105 Exception(message, innerException)
106 HResult = E_FAIL
107 End Sub
108Private
109End Class
110
111/*!
112@brief Windowsのエラー値を基に例外を投げる
113@param[in] dwErrorCode Win32エラーコード
114@throw WindowsException 常に投げられる。
115@date 2008/07/13
116@auther Egtra
117*/
118Sub ThrowWithErrorCode(dwErrorCode As DWord)
119 Throw New WindowsException(HRESULT_FROM_WIN32(dwErrorCode))
120End Sub
121
122/*!
123@brief 内部でGetLastErrorを呼んで、その値を基に例外を投げる。
124@throw WindowsException 常に投げられる。
125@date 2008/08/26
126@auther Egtra
127*/
128Sub ThrowWithLastError()
129 ThrowWithErrorCode(GetLastError())
130End Sub
131/*!
132@brief HRESULT値を基に例外を投げる。
133@date 2008/07/13
134@param[in] hr HRESULT値
135@throw WindowsException FAILED(hr)が真の場合
136@auther Egtra
137hrが成功値 (FAILED(hr) = False) の場合、この関数は何も行わない。
138*/
139Sub ThrowIfFailed(hr As HRESULT)
140 If FAILED(hr) Then
141 Throw New WindowsException(hr)
142 End If
143End Sub
144
145End Namespace 'Widnows
146End Namespace 'ActiveBasic
Note: See TracBrowser for help on using the repository browser.