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

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

ThreadPoolの実装、WaitHandle.WaitAny/WaitAllのまともな実装、ほか。

File size: 3.5 KB
RevLine 
[269]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
[303]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
[269]33
[303]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
[559]49
50/*!
51@brief Windows/COMのエラーを伝える例外クラス
52@date 2008/07/13
53@auther Egtra
54*/
55Class WindowsException
56 Inherits System.Exception
57Public
58 /*!
59 @biref コンストラクタ
60 */
61 Sub WindowsException()
62 Exception()
63 HResult = E_FAIL
64 End Sub
65 /*!
66 @biref コンストラクタ
67 @param[in] hr エラー値
68 */
69 Sub WindowsException(hr As HRESULT)
70 Exception(hresultToString(hr))
71 HResult = hr
72 End Sub
73 /*!
74 @biref コンストラクタ
75 @param[in] message エラーメッセージ
76 */
77 Sub WindowsException(message As String)
78 Exception(message)
79 HResult = E_FAIL
80 End Sub
81 /*!
82 @biref コンストラクタ
83 @param[in] hr エラー値
84 @param[in] message エラーメッセージ
85 */
86 Sub WindowsException(hr As HRESULT, message As String)
87 Exception(message)
88 HResult = hr
89 End Sub
90 /*!
91 @biref コンストラクタ
92 @param[in] message エラーメッセージ
93 @param[in] innerException 内部例外
94 */
95 Sub WindowsException(message As String, innerException As Exception)
96 Exception(message, innerException)
97 HResult = E_FAIL
98 End Sub
99Private
100 Static Function hresultToString(hr As HRESULT) As String
101 Dim pszMsg As PCSTR
102 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER Or FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS,
[575]103 0, hr, LANG_USER_DEFAULT, VarPtr(pszMsg) As PTSTR, 0, 0)
[559]104 If pszMsg <> 0 Then
105 hresultToString = New String(pszMsg)
106 LocalFree(pszMsg)
107 End If
108 End Function
109End Class
110
111/*!
[599]112@brief Windowsのエラー値を基に例外を投げる
[559]113@param[in] dwErrorCode Win32エラーコード
114@throw WindowsException 常に投げられる。
[599]115@date 2008/07/13
116@auther Egtra
[559]117*/
[599]118Sub ThrowWithErrorCode(dwErrorCode As DWord)
[559]119 Throw New WindowsException(HRESULT_FROM_WIN32(dwErrorCode))
120End Sub
121
122/*!
[599]123@brief 内部でGetLastErrorを呼んで、その値を基に例外を投げる。
124@throw WindowsException 常に投げられる。
125@date 2008/08/26
126@auther Egtra
127*/
128Sub ThrowWithLastError()
129 ThrowWithErrorCode(GetLastError())
130End Sub
131/*!
[559]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*/
[561]139Sub ThrowIfFailed(hr As HRESULT)
[559]140 If FAILED(hr) Then
141 Throw New WindowsException(hr)
142 End If
143End Sub
144
[269]145End Namespace 'Widnows
146End Namespace 'ActiveBasic
Note: See TracBrowser for help on using the repository browser.