Alternate e697dbe9c5997e35395fe158628dd8c5209481da
for Visual Studio 2022 and Windows 11.
読み取り中…
検索中…
一致する文字列を見つけられません
Utility.cpp
[詳解]
1// ----------------------------------------------------------------------------
6
7#include "pch.h"
8#include "Utility.h"
9#include "File.h"
10
11using namespace alt;
12
14{
15 GUID guid;
16
17 HRESULT res = ::CoCreateGuid (&guid);
18
19 return guid;
20}
21
23{
24 GUID guid = CreateGUID ();
25
26 TString ret;
27 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
28 ret.Format (
29 _T ("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"),
30 guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1],
31 guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5],
32 guid.Data4[6], guid.Data4[7]);
33
34 return ret;
35}
36
38{
39 SYSTEMTIME systemTime;
40
41 ::GetSystemTime (&systemTime);
42
43 return systemTime;
44}
45
47{
48 SYSTEMTIME systemTime;
49
50 ::GetLocalTime (&systemTime);
51
52 return systemTime;
53}
54
55TString Utility::GetFormatTime (const SYSTEMTIME& systemTime)
56{
57 TString response;
58 response.Format (
59 _T ("%04d/%02d/%02d %02d:%02d:%02d.%03d"), systemTime.wYear,
60 systemTime.wMonth, systemTime.wDay, systemTime.wHour,
61 systemTime.wMinute, systemTime.wSecond, systemTime.wMilliseconds);
62
63 return response;
64}
65
67{
68 SYSTEMTIME systemTime = Utility::GetLocalTime ();
69
70 FILETIME fileTime;
71 BOOL ret = ::SystemTimeToFileTime (&systemTime, &fileTime);
72
73 ULARGE_INTEGER response{ 0, 0 };
74
75 if (ret)
76 {
77 response.HighPart = fileTime.dwHighDateTime;
78 response.LowPart = fileTime.dwLowDateTime;
79 }
80
81 return response;
82}
83
84SYSTEMTIME Utility::GetTimeByFileTime (const FILETIME& fileTime)
85{
86 SYSTEMTIME systemlTime{ 0, 0, 0, 0, 0, 0, 0, 0 };
87
88 ::FileTimeToSystemTime (&fileTime, &systemlTime);
89
90 return systemlTime;
91}
92
93VOID Utility::GetSystemInfo (SYSTEM_INFO& systemInfo)
94{
95 ::GetSystemInfo (&systemInfo);
96}
97
99 LPBYTE lpbyString, DWORD dwLength, TString& response)
100{
101 BOOL bResponse = FALSE;
102 DWORD dwBuffer = 0;
103 LPTSTR lptszBuffer;
104
105 if (::CryptBinaryToString (
106 lpbyString, dwLength, CRYPT_STRING_BASE64, NULL, &dwBuffer))
107 {
108 lptszBuffer = new TCHAR[dwBuffer + sizeof (TCHAR)];
109 if (::CryptBinaryToString (
110 lpbyString, dwLength, CRYPT_STRING_BASE64, lptszBuffer, &dwBuffer))
111 {
112 response = lptszBuffer;
113 bResponse = TRUE;
114 }
115
116 delete[] lptszBuffer;
117 }
118
119 return bResponse;
120}
121
122BOOL Utility::Base64Decode (TString& source, LPBYTE lpbyBuffer, DWORD& dwBuffer)
123{
124 BOOL bResponse = FALSE;
125
126 if (lpbyBuffer == NULL)
127 {
128 bResponse = ::CryptStringToBinary (
129 source.Ctr (), source.Len (), CRYPT_STRING_BASE64, NULL, &dwBuffer,
130 NULL, NULL);
131 }
132 else
133 {
134 bResponse = ::CryptStringToBinary (
135 source.Ctr (), source.Len (), CRYPT_STRING_BASE64, lpbyBuffer,
136 &dwBuffer, NULL, NULL);
137 }
138
139 return bResponse;
140}
141
142// @sa https://docs.microsoft.com/ja-jp/windows/win32/procthread/changing-environment-variables#example-3
144{
146
147 LPTCH lptchEnv = ::GetEnvironmentStrings ();
148 if (lptchEnv == NULL) return response;
149
150 LPTSTR lptszVariable = (LPTSTR)lptchEnv;
151
152 while (*lptszVariable)
153 {
154 TString string (lptszVariable);
155 response.Add (string);
156 lptszVariable += (lstrlen (lptszVariable) + (SIZE_T)1);
157 }
158
159 FreeEnvironmentStrings (lptchEnv);
160
161 return response;
162}
163
164TString Utility::GetEnv (LPCTSTR lpctszKeyword)
165{
166 DWORD dwRet;
167 TString enpty;
168
169 dwRet = ::GetEnvironmentVariable (lpctszKeyword, NULL, 0);
170 if (dwRet == 0) return enpty;
171
172 TString response (dwRet);
173 ::GetEnvironmentVariable (lpctszKeyword, response.Ptr (), dwRet);
174
175 return response;
176}
177
178BOOL Utility::ReadIniFile (LPCTSTR lpctszIniFile, LPCTSTR lpctszSection, LPCTSTR lpctszKeyword, TString& response)
179{
180 BOOL ret;
181 TCHAR tszBuffer[MAX_PATH];
182
183 DWORD dwSize = ::GetPrivateProfileString (lpctszSection, lpctszKeyword, _T (""), tszBuffer, MAX_PATH, lpctszIniFile);
184
185 if (dwSize > 0)
186 {
187 response = tszBuffer;
188 ret = TRUE;
189 }
190 else
191 {
192 response = _T ("");
193 ret = FALSE;
194 }
195
196 return ret;
197}
198
199DWORD Utility::ReadIniFile (LPCTSTR lpctszIniFile, LPCTSTR lpctszSection, LPCTSTR lpctszKeyword, DWORD dwDefault)
200{
201 return ::GetPrivateProfileInt (lpctszSection, lpctszKeyword, dwDefault, lpctszIniFile);
202}
203
204BOOL Utility::WriteIniFile (LPCTSTR lpctszIniFile, LPCTSTR lpctszSection, LPCTSTR lpctszKeyword, TString& strValue)
205{
206 return ::WritePrivateProfileString (lpctszSection, lpctszKeyword, strValue.Ctr (), lpctszIniFile);
207}
208
209BOOL Utility::WriteIniFile (LPCTSTR lpctszIniFile, LPCTSTR lpctszSection, LPCTSTR lpctszKeyword, INT iValue)
210{
211 TString strValue;
212
213 strValue << iValue;
214
215 return ::WritePrivateProfileString (lpctszSection, lpctszKeyword, strValue.Ctr (), lpctszIniFile);
216}
217
218BOOL Utility::CreateMD5 (LPBYTE lpbyData, DWORD dwSize, LPBYTE lpbyMD5)
219{
220 HCRYPTPROV hProv;
221 HCRYPTHASH hHash;
222 DWORD dwHashSize = 16;
223 BYTE byHash[16];
224 CHAR szDigits[] = "0123456789ABCDEF";
225 BOOL ret = FALSE;
226
227 do
228 {
229 ret = ::CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
230 if (!ret) break;
231
232 ret = ::CryptCreateHash (hProv, CALG_MD5, 0, 0, &hHash);
233 if (!ret) break;
234
235 ret = ::CryptHashData (hHash, lpbyData, dwSize, 0);
236 if (!ret) break;
237
238 ret = CryptGetHashParam (hHash, HP_HASHVAL, byHash, &dwHashSize, 0);
239 if (!ret) break;
240
241 int n = 0;
242 for (DWORD i = 0; i < dwHashSize; i++)
243 {
244 CHAR cData1 = szDigits[byHash[i] >> 4];
245 CHAR cData2 = szDigits[byHash[i] & 0x0f];
246 lpbyMD5[n++] = cData1;
247 lpbyMD5[n++] = cData2;
248 }
249
250 ret = ::CryptDestroyHash (hHash);
251 if (!ret) break;
252
253 ret = ::CryptReleaseContext (hProv, 0);
254 if (!ret) break;
255 } while (false);
256
257 return ret;
258}
259
260BOOL Utility::CreateMD5 (LPCTSTR lpctszFileName, LPBYTE lpbyMD5)
261{
262 HCRYPTPROV hProv;
263 HCRYPTHASH hHash;
264 DWORD dwHashSize = 16;
265 BYTE byHash[16];
266 const CHAR cszDigits[] = "0123456789ABCDEF";
267 alt::File checkFile;
268 BYTE byBuffer[8192]{ 0 };
269 DWORD dwReadSize;
270 BOOL ret = FALSE;
271
272 do
273 {
274 ret = checkFile.Create (lpctszFileName, GENERIC_READ, 0, OPEN_EXISTING);
275 if (!ret) break;
276
277 ret = ::CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
278 if (!ret) break;
279
280 ret = ::CryptCreateHash (hProv, CALG_MD5, 0, 0, &hHash);
281 if (!ret) break;
282
283 while ((dwReadSize = checkFile.Read (byBuffer, sizeof (byBuffer))) > 0)
284 {
285 ret = ::CryptHashData (hHash, byBuffer, dwReadSize, 0);
286 if (!ret) break;
287 }
288
289 ret = checkFile.Close ();
290 if (!ret) break;
291
292 ret = CryptGetHashParam (hHash, HP_HASHVAL, byHash, &dwHashSize, 0);
293 if (!ret) break;
294
295 int n = 0;
296 for (DWORD i = 0; i < dwHashSize; i++)
297 {
298 const CHAR ccData1 = cszDigits[byHash[i] >> 4];
299 const CHAR ccData2 = cszDigits[byHash[i] & 0x0f];
300 lpbyMD5[n++] = ccData1;
301 lpbyMD5[n++] = ccData2;
302 }
303
304 ret = ::CryptDestroyHash (hHash);
305 if (!ret) break;
306
307 ret = ::CryptReleaseContext (hProv, 0);
308 if (!ret) break;
309 } while (false);
310
311 return ret;
312}
ファイルIOに関するWindowsAPIを集約したクラス
汎用的に使えるユーティリティクラス
プリコンパイル済みヘッダー ファイルです。
ファイルIOに関するWindowsAPIを集約したクラス
Definition: File.h:16
BOOL APIENTRY Create(LPCTSTR lpctszFileName, DWORD dwDesiredAccess, DWORD dwShareMode, DWORD dwCreationDisposition)
ファイルを作成、オープンします。
Definition: File.cpp:12
BOOL APIENTRY Close()
使用しなくなったハンドルはこれでクローズします。
DWORD APIENTRY Read(LPVOID lpvBuffer, DWORD dwSize) const
HANDLEを使ってデータを読み込みます。
文字列に関するWindowsAPIを集約したクラス
Definition: TString.h:17
TString &APIENTRY Format(LPCTSTR format,...)
フォーマットに従ってパラメータを文字列化します。
Definition: TString.cpp:333
INT APIENTRY Len() const
内部で確保している文字列数を取得します。
Definition: TString.cpp:39
LPCTSTR APIENTRY Ctr() const
内部で確保している文字列ポインタを取得します。
Definition: TString.h:46
LPTSTR APIENTRY Ptr() const
内部で確保している文字列ポインタを取得します。
Definition: TString.h:42
static BOOL APIENTRY Base64Decode(TString &source, LPBYTE lpbyBuffer, DWORD &dwBuffer)
BASE64データをデコード
Definition: Utility.cpp:122
static const TString APIENTRY MakeGUID()
GUIDの作成
Definition: Utility.cpp:22
static skeleton::Array< TString > APIENTRY GetEnvironments()
環境変数の取得
Definition: Utility.cpp:143
static ULARGE_INTEGER APIENTRY GetLocalTimeQuad()
ローカル時間を64ビット値で取得
Definition: Utility.cpp:66
static BOOL APIENTRY CreateMD5(LPBYTE lpbyData, DWORD dwSize, LPBYTE lpbyMD5)
データからMD5を計算
Definition: Utility.cpp:218
static const GUID APIENTRY CreateGUID()
GUIDの作成
Definition: Utility.cpp:13
static BOOL APIENTRY ReadIniFile(LPCTSTR lpctszIniFile, LPCTSTR lpctszSection, LPCTSTR lpctszKeyword, TString &response)
設定フィルの読み込み
Definition: Utility.cpp:178
static TString APIENTRY GetEnv(LPCTSTR lpctszKeyword)
環境変数の取得
Definition: Utility.cpp:164
static BOOL APIENTRY Base64Encode(LPBYTE lpbyString, DWORD dwLength, TString &response)
BASE64でデータをエンコード
Definition: Utility.cpp:98
static SYSTEMTIME APIENTRY GetLocalTime()
ローカル時間の取得
Definition: Utility.cpp:46
static VOID APIENTRY GetSystemInfo(SYSTEM_INFO &systemInfo)
システム情報を取得します。
Definition: Utility.cpp:93
static TString APIENTRY GetFormatTime(const SYSTEMTIME &systemTime)
SYSTEMTIME構造体の値を文字列化
Definition: Utility.cpp:55
static SYSTEMTIME APIENTRY GetTimeByFileTime(const FILETIME &fileTime)
FILETIME型からSYSTEMTIMEを取得
Definition: Utility.cpp:84
static BOOL APIENTRY WriteIniFile(LPCTSTR lpctszIniFile, LPCTSTR lpctszSection, LPCTSTR lpctszKeyword, TString &strValue)
設定フィルの書き込み
Definition: Utility.cpp:204
static SYSTEMTIME APIENTRY GetSystemTime()
システム時間の取得
Definition: Utility.cpp:37
サイズ可変の配列を具現したクラス
Definition: Array.hpp:20
VOID APIENTRY Add(T &item)
Array<T>へ値を追加
Definition: Array.hpp:83
Definition: DBLibrary.h:12