-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileFunctions.cpp
98 lines (89 loc) · 2.35 KB
/
FileFunctions.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
#include <windows.h>
#include <strsafe.h>
#include <io.h>
#include <fcntl.h>
#include <Commdlg.h>
BOOL OpenSaveGetFileName(HWND hwnd, LPCWSTR filter, LPCWSTR title, UCHAR selection, LPWSTR filepathname, LPWSTR filename)
{
OPENFILENAME openfilename;
// Initialize OPENFILENAME
ZeroMemory(&openfilename, sizeof(openfilename));
openfilename.lStructSize = sizeof(openfilename);
openfilename.hwndOwner = hwnd;
openfilename.lpstrFile = filepathname;
openfilename.lpstrFile[0] = '\0';
openfilename.nMaxFile = MAX_PATH;
openfilename.lpstrFilter = filter;
openfilename.nFilterIndex = 1;
openfilename.lpstrFileTitle = NULL;
openfilename.nMaxFileTitle = 0;
openfilename.lpstrInitialDir = NULL;
openfilename.lpstrFileTitle = filename;
openfilename.nMaxFileTitle = MAX_PATH;
openfilename.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_EXPLORER | OFN_OVERWRITEPROMPT;
openfilename.lpstrTitle = title;
if (selection == 1)
{
if (!GetOpenFileName(&openfilename))
return FALSE;
}
else
{
if (!GetSaveFileName(&openfilename))
return FALSE;
}
return TRUE;
}
DWORD GetSrcBufSize(LPCWSTR pssrcfname)
{
HANDLE hfile;
LARGE_INTEGER filesize = { 0 };
hfile = CreateFile(pssrcfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
if (hfile == INVALID_HANDLE_VALUE)
{
return 0;
}
GetFileSizeEx(hfile, &filesize);
CloseHandle(hfile);
return filesize.LowPart;
}
BOOL ReadFromFile(LPCWSTR pssrcfname, PBYTE psrcbuf, int ibufsize)
{
HANDLE hfile;
DWORD dwreadsize = { 0 };
hfile = CreateFile(pssrcfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hfile == INVALID_HANDLE_VALUE)
{
return 0;
}
if (FALSE == ReadFile(hfile, psrcbuf, ibufsize, &dwreadsize, NULL))
{
CloseHandle(hfile);
return FALSE;
}
CloseHandle(hfile);
if (dwreadsize == 0)
{
return FALSE;
}
return TRUE;
}
BOOL Write2File(LPCWSTR psdestfname, PBYTE pdestbuf, int idestsize)
{
HANDLE hfile;
DWORD dwwrotesize = { 0 };
hfile = CreateFile(psdestfname, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (hfile == INVALID_HANDLE_VALUE)
{
return 0;
}
if (FALSE == WriteFile(hfile, pdestbuf, idestsize, &dwwrotesize, NULL))
{
CloseHandle(hfile);
return FALSE;
}
CloseHandle(hfile);
if (dwwrotesize != idestsize)
return FALSE;
return TRUE;
}