-
Notifications
You must be signed in to change notification settings - Fork 135
/
system.cpp
572 lines (527 loc) · 17.9 KB
/
system.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#include <system.h>
#include <sstream>
#include <tchar.h>
#include <shellapi.h>
#include <ShlObj.h>
#include "base64.h"
wstring GetExtW(wstring path){
int index=path.find_last_of('.');
if(index==-1){
return L"";
}
else{
wstring type=path.substr(index+1);
return replace_allW(type,L"jpg",L"jpeg");
}
}
string GetExt(string path){
int index=path.find_last_of('.');
if(index==-1){
return "";
}
else{
string type=path.substr(index+1);
return replace_all(type,"jpg","jpeg");
}
}
BOOL EnableShutdownPrivilege()
{
HANDLE hProcess = NULL;
HANDLE hToken = NULL;
LUID uID = {0};
TOKEN_PRIVILEGES stToken_Privileges = {0};
hProcess = ::GetCurrentProcess(); //获取当前应用程序进程句柄
if(!::OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES,&hToken)) //打开当前进程的访问令牌句柄(OpenProcessToken函数调用失败返回值为零)
return FALSE;
if(!::LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&uID)) //获取权限名称为"SeShutdownPrivilege"的LUID(LookupPrivilegeValue函数调用失败返回值为零)
return FALSE;
stToken_Privileges.PrivilegeCount = 1; //欲调整的权限个数
stToken_Privileges.Privileges[0].Luid = uID; //权限的LUID
stToken_Privileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; //权限的属性,SE_PRIVILEGE_ENABLED为使能该权限
if(!::AdjustTokenPrivileges(hToken,FALSE,&stToken_Privileges,sizeof stToken_Privileges,NULL,NULL)) //调整访问令牌里的指定权限(AdjustTokenPrivileges函数调用失败返回值为零)
return FALSE;
if(::GetLastError() != ERROR_SUCCESS) //查看权限是否调整成功
return FALSE;
::CloseHandle(hToken);
return TRUE;
}
//关机函数
BOOL Shutdown(BOOL bForce)
{
EnableShutdownPrivilege(); //使能关机特权函数
if(bForce)
return ::ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,0); //强制关机
else
return ::ExitWindowsEx(EWX_SHUTDOWN,0);
}
//注销函数
BOOL Logoff(BOOL bForce)
{
if(bForce)
return ::ExitWindowsEx(EWX_LOGOFF | EWX_FORCE,0); //强制注销
else
return ::ExitWindowsEx(EWX_LOGOFF,0);
}
//重启函数
BOOL Reboot(BOOL bForce)
{
EnableShutdownPrivilege(); //使能关机特权函数
if(bForce)
return ::ExitWindowsEx(EWX_REBOOT | EWX_FORCE,0); //强制重启
else
return ::ExitWindowsEx(EWX_REBOOT,0);
}
int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat, ...)
{
TCHAR szBuffer [1024] ;
va_list pArgList ;
// The va_start macro (defined in STDARG.H) is usually equivalent to:
// pArgList = (char *) &szFormat + sizeof (szFormat) ;
va_start (pArgList, szFormat) ;
// The last argument to wvsprintf points to the arguments
_vsntprintf ( szBuffer, sizeof (szBuffer) / sizeof (TCHAR),
szFormat, pArgList) ;
// The va_end macro just zeroes out pArgList for no good reason
va_end (pArgList) ; //Defoe.Tu tyysoft@yahoo.com.cn Windows 程序设计 PDF 美化版
return MessageBox (NULL, szBuffer, szCaption, 0) ;
}
BOOL SaveFileDialog(HWND hWnd, const TCHAR* fileNameStr, TCHAR* szFile)
{
//TCHAR szFile[2048];
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
// must !
ofn.lpstrFile = szFile;
ofn.lpstrTitle = TEXT("保存文件");
ofn.lpstrFileTitle=(LPWSTR)fileNameStr;
ofn.nMaxFile = 2048;//sizeof(szFile);
//
ofn.lpstrFile[0] = '\0';
wcscpy(ofn.lpstrFile, (LPWSTR)fileNameStr);
ofn.Flags = OFN_OVERWRITEPROMPT;
//no extention file! ofn.lpstrFilter="Any file(*.*)\0*.*\0ddfs\0ddfs*\0";
return(GetSaveFileName((LPOPENFILENAME)&ofn));
}
BOOL OpenFileDialog(HWND hWnd, const TCHAR* fileNameStr, TCHAR* szFile)
{
//TCHAR szFile[2048];
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
// must !
ofn.lpstrFile = szFile;
ofn.lpstrTitle = TEXT("打开文件");
ofn.lpstrFileTitle=(LPWSTR)fileNameStr;
ofn.nMaxFile = 2048;//sizeof(szFile);
//
ofn.lpstrFile[0] = '\0';
wcscpy(ofn.lpstrFile, (LPWSTR)fileNameStr);
ofn.Flags = OFN_FILEMUSTEXIST;
//no extention file! ofn.lpstrFilter="Any file(*.*)\0*.*\0ddfs\0ddfs*\0";
return(GetOpenFileName((LPOPENFILENAME)&ofn));
}
BOOL OpenMultiFilesDialog(HWND hWnd, const TCHAR* fileNameStr, TCHAR* szFile)
{
//TCHAR szFile[2048];
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
// must !
ofn.lpstrFile = szFile;
ofn.lpstrTitle = TEXT("打开文件");
ofn.lpstrFileTitle=(LPWSTR)fileNameStr;
ofn.nMaxFile = 2048;//sizeof(szFile);
//
ofn.lpstrFile[0] = '\0';
wcscpy(ofn.lpstrFile, (LPWSTR)fileNameStr);
ofn.Flags = OFN_FILEMUSTEXIST|OFN_ALLOWMULTISELECT|OFN_EXPLORER;
//no extention file! ofn.lpstrFilter="Any file(*.*)\0*.*\0ddfs\0ddfs*\0";
return(GetOpenFileName((LPOPENFILENAME)&ofn));
}
void GetFolder (HWND hWnd, TCHAR* szSelFolder)
{
BROWSEINFO bi = {NULL};
LPITEMIDLIST lpIDList = NULL;
// Setup BROWSEINFO structure
bi.hwndOwner = hWnd;
bi.pidlRoot = NULL;
bi.lpszTitle = TEXT("Save self-extract data file to...");
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.pszDisplayName = szSelFolder;
// Load the Browse folder dialog
lpIDList = SHBrowseForFolder(&bi);
// Get the selected path from IDList
SHGetPathFromIDList(lpIDList, szSelFolder);
// Free the resources
CoTaskMemFree(lpIDList);
// Check the selected folder
if (wcslen(szSelFolder) > 0)
{
// Check is the last character is "\", if not, an extra "\" was append on it.
if (92 != szSelFolder[wcslen(szSelFolder) - 1]) {wcsncat(szSelFolder, L"\\", 1);}
// Display the selected folder in the edit control
}
}
//可同时处理目录和文件:path可以是路径,也可以是文件名,或者文件通配符
wstring find(wstring path,bool cursive)
{
//取路径名最后一个"//"之前的部分,包括"//"
replace_allW(path,L"\\",L"/");
UINT index=path.find_last_of('/');
if(index+1==path.length()){
path.append(L"*.*");
}
wstring prefix=path.substr(0,path.find_last_of('/')+1);
WIN32_FIND_DATA FindFileData;
HANDLE hFind=::FindFirstFile(path.data(),&FindFileData);
std::wstringstream ss;
if(INVALID_HANDLE_VALUE == hFind)
{
ss<<L"[]";
FindClose(hFind);
return ss.str();
}
else{
ss<<L"[";
}
while(TRUE)
{
bool flag=false;;
//目录
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//不是当前目录,也不是父目录
if(cursive&&FindFileData.cFileName[0]!='.')
{
wstring temp=prefix+FindFileData.cFileName;
ss<<L"{\"name\":\""<<FindFileData.cFileName<<L"\",\"list\":"<<find(temp+L"/*.*",cursive).data()<<L"}";
flag=true;
}
}
//文件
else
{
ss<<L"\""<<FindFileData.cFileName<<L"\"";
flag=true;
}
if(!FindNextFile(hFind,&FindFileData))
break;
else if(flag){
ss<<L",";
}
}
ss<<L"]";
FindClose(hFind);
return ss.str();
}
BOOL ModifyStyle(HWND hWnd,
_In_ DWORD dwRemove,
_In_ DWORD dwAdd,
_In_ UINT nFlags)
{
DWORD dwStyle = ::GetWindowLong(hWnd, GWL_STYLE);
DWORD dwNewStyle = (dwStyle & ~dwRemove) | dwAdd;
if(dwStyle == dwNewStyle)
return FALSE;
::SetWindowLong(hWnd, GWL_STYLE, dwNewStyle);
if(nFlags != 0)
{
::SetWindowPos(hWnd, NULL, 0, 0, 0, 0,
SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | nFlags);
}
return TRUE;
}
void SetDefaultBrowser(const TCHAR *strAppName){
HKEY key;
RegOpenKey(HKEY_CLASSES_ROOT,_T("http\\shell\\open\\command"),&key);
RegSetValue(key,NULL,REG_SZ,strAppName,sizeof(strAppName));
RegCloseKey(key);
RegOpenKey(HKEY_CLASSES_ROOT,_T("HKEY_CLASSES_ROOT\\htmlfile\\shell\\open\\command"),&key);
RegSetValue(key,NULL,REG_SZ,strAppName,sizeof(strAppName));
RegCloseKey(key);
RegOpenKey(HKEY_CLASSES_ROOT,_T("HKEY_CLASSES_ROOT\\mhtmlfile\\shell\\open\\command"),&key);
RegSetValue(key,NULL,REG_SZ,strAppName,sizeof(strAppName));
RegCloseKey(key);
RegOpenKey(HKEY_CLASSES_ROOT,_T("HKEY_CLASSES_ROOT\\InternetShortcut\\shell\\open\\command"),&key);
RegSetValue(key,NULL,REG_SZ,strAppName,sizeof(strAppName));
RegCloseKey(key);
}
//---------------------------------------------------------------------------
// 检测文件关联情况
// strExt: 要检测的扩展名(例如: ".txt")
// strAppKey: ExeName扩展名在注册表中的键值(例如: "txtfile")
// 返回TRUE: 表示已关联,FALSE: 表示未关联
BOOL CheckFileRelation(const TCHAR *strExt, const TCHAR *strAppKey, TCHAR *strAppName, TCHAR *strDefaultIcon, TCHAR *strDescribe)
{
int nRet=TRUE;
HKEY hExtKey;
TCHAR szPath[_MAX_PATH];
DWORD dwSize=sizeof(szPath);
TCHAR strTemp[_MAX_PATH];
if(RegOpenKey(HKEY_CLASSES_ROOT,strExt,&hExtKey)==ERROR_SUCCESS)
{
RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize);
if(_wcsicmp(szPath,strAppKey)!=0)
{
nRet=FALSE;
RegSetValue(hExtKey,L"",REG_SZ,strAppKey,wcslen(strAppKey)+1);
//return nRet;
}
RegCloseKey(hExtKey);
}
dwSize=sizeof(szPath);
if(RegOpenKey(HKEY_CLASSES_ROOT,strAppKey,&hExtKey)==ERROR_SUCCESS)
{
RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize);
if(_wcsicmp(szPath,strDescribe)!=0)
{
nRet=FALSE;
RegSetValue(hExtKey,L"",REG_SZ,strDescribe,wcslen(strDescribe)+1);
}
RegCloseKey(hExtKey);
}
dwSize=sizeof(szPath);
wsprintf(strTemp,L"%s\\DefaultIcon",strAppKey);
if(RegOpenKey(HKEY_CLASSES_ROOT,strTemp,&hExtKey)==ERROR_SUCCESS)
{
RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize);
if(_wcsicmp(szPath,strDefaultIcon)!=0)
{
nRet=FALSE;
RegSetValue(hExtKey,L"",REG_SZ,strDefaultIcon,wcslen(strDefaultIcon)+1);
}
RegCloseKey(hExtKey);
}
dwSize=sizeof(szPath);
wsprintf(strTemp,L"%s\\Shell",strAppKey);
if(RegOpenKey(HKEY_CLASSES_ROOT,strTemp,&hExtKey)==ERROR_SUCCESS)
{
RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize);
if(_wcsicmp(szPath,L"Open")!=0)
{
nRet=FALSE;
RegSetValue(hExtKey,L"",REG_SZ,L"Open",wcslen(L"Open")+1);
}
RegCloseKey(hExtKey);
}
dwSize=sizeof(szPath);
wsprintf(strTemp,L"%s\\Shell\\Open\\Command",strAppKey);
if(RegOpenKey(HKEY_CLASSES_ROOT,strTemp,&hExtKey)==ERROR_SUCCESS)
{
wsprintf(strTemp,L"%s \"%%1\"",strAppName);
RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize);
if(_wcsicmp(szPath,strTemp)!=0)
{
nRet=FALSE;
RegSetValue(hExtKey,L"",REG_SZ,strTemp,wcslen(strTemp)+1);
}
RegCloseKey(hExtKey);
}
dwSize=sizeof(szPath);
if(RegOpenKey(HKEY_CURRENT_USER,strExt,&hExtKey)==ERROR_SUCCESS)
{
RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize);
if(_wcsicmp(szPath,strAppKey)!=0)
{
nRet=FALSE;
RegSetValue(hExtKey,L"",REG_SZ,strAppKey,wcslen(strAppKey)+1);
}
RegCloseKey(hExtKey);
}
dwSize=sizeof(szPath);
if(RegOpenKey(HKEY_CURRENT_USER,strAppKey,&hExtKey)==ERROR_SUCCESS)
{
RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize);
if(_wcsicmp(szPath,strDescribe)!=0)
{
nRet=FALSE;
RegSetValue(hExtKey,L"",REG_SZ,strDescribe,wcslen(strDescribe)+1);
}
RegCloseKey(hExtKey);
}
dwSize=sizeof(szPath);
wsprintf(strTemp,L"%s\\DefaultIcon",strAppKey);
if(RegOpenKey(HKEY_CURRENT_USER,strTemp,&hExtKey)==ERROR_SUCCESS)
{
RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize);
if(_wcsicmp(szPath,strDefaultIcon)!=0)
{
nRet=FALSE;
RegSetValue(hExtKey,L"",REG_SZ,strDefaultIcon,wcslen(strDefaultIcon)+1);
}
RegCloseKey(hExtKey);
}
dwSize=sizeof(szPath);
wsprintf(strTemp,L"%s\\Shell",strAppKey);
if(RegOpenKey(HKEY_CURRENT_USER,strTemp,&hExtKey)==ERROR_SUCCESS)
{
RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize);
if(_wcsicmp(szPath,L"Open")!=0)
{
nRet=FALSE;
RegSetValue(hExtKey,L"",REG_SZ,L"Open",wcslen(L"Open")+1);
}
RegCloseKey(hExtKey);
}
dwSize=sizeof(szPath);
wsprintf(strTemp,L"%s\\Shell\\Open\\Command",strAppKey);
if(RegOpenKey(HKEY_CURRENT_USER,strTemp,&hExtKey)==ERROR_SUCCESS)
{
wsprintf(strTemp,L"%s \"%%1\"",strAppName);
RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize);
if(_wcsicmp(szPath,strTemp)!=0)
{
nRet=FALSE;
RegSetValue(hExtKey,L"",REG_SZ,strTemp,wcslen(strTemp)+1);
}
RegCloseKey(hExtKey);
}
dwSize=sizeof(szPath);
wsprintf(strTemp,L"Software\\Classes\\%s_auto_file\\Shell\\Open\\Command",strExt+1);
if(RegOpenKey(HKEY_CURRENT_USER,strTemp,&hExtKey)==ERROR_SUCCESS)
{
wsprintf(strTemp,L"%s \"%%1\"",strAppName);
RegQueryValueEx(hExtKey,NULL,NULL,NULL,(LPBYTE)szPath,&dwSize);
if(_wcsicmp(szPath,strTemp)!=0)
{
nRet=FALSE;
RegSetValue(hExtKey,L"",REG_SZ,strTemp,wcslen(strTemp)+1);
}
RegCloseKey(hExtKey);
}
return nRet;
}
//---------------------------------------------------------------------------
// 注册文件关联
// strExe: 要检测的扩展名(例如: ".txt")
// strAppName: 要关联的应用程序名(例如: "C:\MyApp\MyApp.exe")
// strAppKey: ExeName扩展名在注册表中的键值(例如: "txtfile")
// strDefaultIcon: 扩展名为strAppName的图标文件(例如: "C:\MyApp\MyApp.exe,0")
// strDescribe: 文件类型描述
void RegisterFileRelation(TCHAR *strExt, TCHAR *strAppName, TCHAR *strAppKey, TCHAR *strDefaultIcon, TCHAR *strDescribe)
{
TCHAR strTemp[_MAX_PATH];
HKEY hKey;
RegCreateKey(HKEY_CLASSES_ROOT,strExt,&hKey);
RegSetValue(hKey,L"",REG_SZ,strAppKey,wcslen(strAppKey)+1);
RegCloseKey(hKey);
RegCreateKey(HKEY_CLASSES_ROOT,strAppKey,&hKey);
RegSetValue(hKey,L"",REG_SZ,strDescribe,wcslen(strDescribe)+1);
RegCloseKey(hKey);
wsprintf(strTemp,L"%s\\DefaultIcon",strAppKey);
RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey);
RegSetValue(hKey,L"",REG_SZ,strDefaultIcon,wcslen(strDefaultIcon)+1);
RegCloseKey(hKey);
wsprintf(strTemp,L"%s\\Shell",strAppKey);
RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey);
RegSetValue(hKey,L"",REG_SZ,L"Open",wcslen(L"Open")+1);
RegCloseKey(hKey);
wsprintf(strTemp,L"%s\\Shell\\Open\\Command",strAppKey);
RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey);
wsprintf(strTemp,L"%s \"%%1\"",strAppName);
RegSetValue(hKey,L"",REG_SZ,strTemp,wcslen(strTemp)+1);
RegCloseKey(hKey);
wsprintf(strTemp,L"%s\\DefaultIcon",strAppKey);
RegCreateKey(HKEY_CURRENT_USER,strTemp,&hKey);
RegSetValue(hKey,L"",REG_SZ,strDefaultIcon,wcslen(strDefaultIcon)+1);
RegCloseKey(hKey);
wsprintf(strTemp,L"%s\\Shell",strAppKey);
RegCreateKey(HKEY_CURRENT_USER,strTemp,&hKey);
RegSetValue(hKey,L"",REG_SZ,L"Open",wcslen(L"Open")+1);
RegCloseKey(hKey);
wsprintf(strTemp,L"%s\\Shell\\Open\\Command",strAppKey);
RegCreateKey(HKEY_CURRENT_USER,strTemp,&hKey);
wsprintf(strTemp,L"%s \"%%1\"",strAppName);
RegSetValue(hKey,L"",REG_SZ,strTemp,wcslen(strTemp)+1);
RegCloseKey(hKey);
wsprintf(strTemp,L"Software\\Classes\\%s_auto_file\\Shell\\Open\\Command",strExt+1);
RegCreateKey(HKEY_CURRENT_USER,strTemp,&hKey);
wsprintf(strTemp,L"%s \"%%1\"",strAppName);
RegSetValue(hKey,L"",REG_SZ,strTemp,wcslen(strTemp)+1);
RegCloseKey(hKey);
SHChangeNotify(SHCNE_ASSOCCHANGED,SHCNF_FLUSHNOWAIT,0, 0);
}
Bitmap* GetImageFromBase64(string encodedImage)
{
int imageSize = int((encodedImage.length()/3)+1)*4;
char* t=new char[imageSize];
base64_decode(encodedImage.c_str(),encodedImage.length(), t, &imageSize); // using the base64
HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, imageSize);
LPVOID pImage = ::GlobalLock(hMem);
memcpy(pImage, t, imageSize);
IStream* pStream = NULL;
::CreateStreamOnHGlobal(hMem, FALSE, &pStream);
Bitmap *img = new Bitmap(pStream);
pStream->Release();
GlobalUnlock(hMem);
GlobalFree(hMem);
delete []t;
return img;
}
//TransparentWnd::connection.RegisterConnection(L"AlloyDesktopSever");
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
void SaveBitmap(Bitmap* pbm, wstring path){
CLSID tiffClsid;
GetEncoderClsid((L"image/"+ GetExtW(path)).data(), &tiffClsid);
pbm->Save(path.data(), &tiffClsid);
}
long GetFileSize(const TCHAR* filename){
HANDLE hFile = CreateFile(filename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
// Check the return handle value
if (NULL != hFile && INVALID_HANDLE_VALUE != hFile)
{
// Get the current file size
long l=GetFileSize(hFile, 0);
CloseHandle(hFile);
return l;
}
else{
return 0;
}
}
void ChangeFileSize(const TCHAR* filename,DWORD size)
{
HANDLE hFile = ::CreateFile(filename, //创建文件的名称。
GENERIC_WRITE|GENERIC_READ, // 写和读文件。
0, // 不共享读写。
NULL, // 缺省安全属性。
OPEN_EXISTING, // 如果文件存在,也创建。
FILE_ATTRIBUTE_NORMAL, // 一般的文件。
NULL);// 模板文件为空。
if (hFile != INVALID_HANDLE_VALUE){
DWORD dwPtr = SetFilePointer(hFile, size, NULL, FILE_BEGIN);
SetEndOfFile(hFile);
CloseHandle(hFile);
}
}