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
| #include <stdio.h>
#include <windows.h>
#include "resource.h"
#define true 1
#define false 0
typedef struct tagFILEDATA
{
char szLine1[30];
char szLine2[30];
char szLine3[30];
int byte1;
int byte2;
int byte3;
} FILEDATA, *LPFILEDATA;
HRESULT CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
void OnOpenFile(HWND);
void DoAsynAction(HWND hwnd, FILEDATA *lpFileData);
HANDLE hFile;
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd )
{
HWND hwndDlg;
MSG msg;
DWORD dwVersion;
dwVersion = GetVersion();
if ( ! (dwVersion < 0x80000000) )
{
// Windows 95, Windows 98
MessageBox(NULL, "The application is running olny on NT stations", "Warning", MB_ICONEXCLAMATION);
return 0;
}
hwndDlg = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG), 0, DlgProc);
if (!hwndDlg)
return 0;
ShowWindow(hwndDlg, SW_SHOW);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
HRESULT CALLBACK DlgProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hwndButton;
HWND hwndFileName;
HWND hwndLine;
HWND hwndBytes;
HWND hwndStatus;
FILEDATA FileData;
char szBuf[30];
switch(message)
{
case WM_INITDIALOG:
hFile = NULL;
hwndButton = GetDlgItem(hwndDlg, IDC_START);
EnableWindow(hwndButton, false);
hwndFileName = GetDlgItem(hwndDlg, IDC_FILENAME);
SendMessage(hwndFileName, WM_SETTEXT, (long)5, (LPARAM)"none");
hwndStatus = GetDlgItem(hwndDlg, IDC_STATUS);
SendMessage(hwndStatus, WM_SETTEXT, (long)6, (LPARAM)"ready to start");
return true;
case WM_COMMAND:
switch(wParam)
{
case IDC_OPEN:
OnOpenFile(hwndDlg);
break;
case IDC_START:
if (hFile)
{
hwndLine = GetDlgItem(hwndDlg, IDC_LINE1);
SendMessage(hwndLine, EM_GETLINE, 0, (LPARAM)szBuf);
strcpy(FileData.szLine1, szBuf);
hwndLine = GetDlgItem(hwndDlg, IDC_LINE2);
SendMessage(hwndLine, EM_GETLINE, 0, (LPARAM)szBuf);
strcpy(FileData.szLine2, szBuf);
hwndLine = GetDlgItem(hwndDlg, IDC_LINE3);
SendMessage(hwndLine, EM_GETLINE, 0, (LPARAM)szBuf);
strcpy(FileData.szLine3, szBuf);
hwndBytes = GetDlgItem(hwndDlg, IDC_BYTE1);
SendMessage(hwndBytes, EM_GETLINE, 0, (LPARAM)szBuf);
FileData.byte1 = atoi(szBuf);
hwndBytes = GetDlgItem(hwndDlg, IDC_BYTE2);
SendMessage(hwndBytes, EM_GETLINE, 0, (LPARAM)szBuf);
FileData.byte2 = atoi(szBuf);
hwndBytes = GetDlgItem(hwndDlg, IDC_BYTE3);
SendMessage(hwndBytes, EM_GETLINE, 0, (LPARAM)szBuf);
FileData.byte3 = atoi(szBuf);
DoAsynAction(hwndDlg, &FileData);
} else
MessageBox(hwndDlg, "File not open!", "Error", MB_OK);
break;
case IDC_QUIT:
SendMessage(hwndDlg, WM_CLOSE, 0, 0);
break;
}
return true;
case WM_CLOSE:
if (hFile)
{
CloseHandle(hFile);
hFile = NULL;
}
EndDialog(hwndDlg, 0);
DestroyWindow(hwndDlg);
PostQuitMessage(0);
return true;
}
return false;
}
void OnOpenFile(HWND hwnd)
{
TCHAR szFileName[256] = "";
OPENFILENAME ofn;
HWND hwndFileName;
HWND hwndRes;
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "TXT File\0*.txt\0\0";
ofn.lpstrCustomFilter = NULL;
ofn.lpstrFile = szFileName;
ofn.nMaxFile = 255;
ofn.lpstrFileTitle = NULL;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = "Open text file";
ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
ofn.lpstrDefExt = "txt";
if(GetOpenFileName(&ofn))
{
hFile = CreateFile(ofn.lpstrFile,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);
if(hFile)
{
hwndFileName = GetDlgItem(hwnd, IDC_FILENAME);
SendMessage(hwndFileName, WM_SETTEXT, sizeof(ofn.lpstrFile), (LPARAM)ofn.lpstrFile);
hwndRes = GetDlgItem(hwnd, IDC_START);
EnableWindow(hwndRes, true);
}
}
}
void DoAsynAction(HWND hwnd, FILEDATA *lpFileData)
{
HWND hwndStatus;
HWND hwndStart;
OVERLAPPED OverLine1;
OVERLAPPED OverLine2;
OVERLAPPED OverLine3;
int test;
OverLine1.Offset = lpFileData->byte1;
OverLine1.OffsetHigh = 0;
OverLine1.hEvent = NULL;
test = strlen(lpFileData->szLine1);
OverLine2.Offset = lpFileData->byte2;
OverLine2.OffsetHigh = 0;
OverLine2.hEvent = NULL;
OverLine3.Offset = lpFileData->byte3;
OverLine3.OffsetHigh = 0;
OverLine3.hEvent = NULL;
WriteFile(hFile, lpFileData->szLine1, sizeof(char)*strlen(lpFileData->szLine1), NULL, &OverLine1);
WriteFile(hFile, lpFileData->szLine2, sizeof(char)*strlen(lpFileData->szLine2), NULL, &OverLine2);
WriteFile(hFile, lpFileData->szLine3, sizeof(char)*strlen(lpFileData->szLine3), NULL, &OverLine3);
CloseHandle(hFile);
hFile = NULL;
hwndStatus = GetDlgItem(hwnd, IDC_STATUS);
SendMessage(hwndStatus, WM_SETTEXT, (long)10, (LPARAM)"file modifyded");
hwndStart = GetDlgItem(hwnd, IDC_START);
EnableWindow(hwndStart, false);
} |