14. MFC UI
14.17 Xóa Icon trong Cache
118 14.18 Mutex #include <mutex> std::mutex mLock_Exit; mLock_Exit.lock(); // Nội dụng cần mutex mLock_Exit.unlock();
15. Định nghĩa rồi không cần định nghĩa lại
#ifndef _CCONVERT_H_ // Có thể sửa tên #define _CCONVERT_H_ // Có thể sửa tên // Nội dung
#endif
http://www.smart-arab.com/2014/07/encrypt-decrypt-binary-files-using-des-crypt-in-c/
15.11 Set tilte in window
AfxGetMainWnd()->SetWindowText(L"PaseriPC Current status");
16. Build MFC release
Sửa đường dẫn tương đối : $(SolutionDir)\..\ Bước 1 : Cấu hình property Solution
119
120
127
128
17.SERVER – CLIENT - PIPE
SERVER
#include <iostream> #include <windows.h> using namespace std;
int main(int argc, const char **argv) {
while (1) {
wcout << "SERVER" << endl;
wcout << "Creating an instance of a named pipe..." << endl; // Create a pipe to send data
HANDLE pipe = CreateNamedPipe(
L"\\\\.\\pipe\\my_pipe", // name of the pipe PIPE_ACCESS_DUPLEX, // 1-way pipe -- send only PIPE_TYPE_BYTE, // send data as a byte stream 1, // only allow 1 instance of this pipe
129
0, // no outbound buffer 0, // no inbound buffer 0, // use default wait time
NULL // use default security attributes );
if (pipe == NULL || pipe == INVALID_HANDLE_VALUE) {
wcout << "Failed to create outbound pipe instance."; // look up error code here using GetLastError() system("pause");
return 1; }
wcout << "Waiting for a client to connect to the pipe..." << endl; // This call blocks until a client process connects to the pipe BOOL result = ConnectNamedPipe(pipe, NULL);
if (!result) {
wcout << "Failed to make connection on named pipe." << endl; // look up error code here using GetLastError()
CloseHandle(pipe); // close the pipe system("pause");
return 1; }
wcout << "Sending data to pipe..." << endl;
// This call blocks until a client process reads all the data const wchar_t *data = L"*** Hello Pipe World ***";
DWORD numBytesWritten = 0; result = WriteFile(
pipe, // handle to our outbound pipe data, // data to send
wcslen(data) * sizeof(wchar_t), // length of data to send (bytes) &numBytesWritten, // will store actual amount of data sent
NULL // not using overlapped IO );
if (result) {
wcout << "Number of bytes sent: " << numBytesWritten << endl; }
else {
wcout << "Failed to send data." << endl;
// look up error code here using GetLastError() }
// The read operation will block until there is data to read wchar_t buffer[128];
DWORD numBytesRead = 0; result = ReadFile(
pipe,
buffer, // the data from the pipe will be put here 127 * sizeof(wchar_t), // number of bytes allocated
&numBytesRead, // this will store number of bytes actually read NULL // not using overlapped IO
130
if (result) {
buffer[numBytesRead / sizeof(wchar_t)] = NULL; // null terminate the string
wcout << "Number of bytes read: " << numBytesRead << endl; wcout << "Message: " << buffer << endl;
} else {
wcout << "Failed to read data from the pipe." << endl; }
CloseHandle(pipe);
wcout << "Done." << endl; }
// Close the pipe (automatically disconnects client too)
system("pause"); return 0; } CLIENT ///// CLIENT PROGRAM ///// #include <iostream> #include <windows.h> using namespace std;
int main(int argc, const char **argv) {
wcout << "CLIENT" << endl;
wcout << "Connecting to pipe..." << endl; // Open the named pipe
// Most of these parameters aren't very relevant for pipes. HANDLE pipe = CreateFile(
L"\\\\.\\pipe\\my_pipe",
GENERIC_READ | GENERIC_WRITE, // only need read access FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if (pipe == INVALID_HANDLE_VALUE) {
wcout << "Failed to connect to pipe." << endl; // look up error code here using GetLastError() system("pause");
131
return 1; }
wcout << "Reading data from pipe..." << endl;
// The read operation will block until there is data to read wchar_t buffer[128];
DWORD numBytesRead = 0; BOOL result = ReadFile(
pipe,
buffer, // the data from the pipe will be put here 127 * sizeof(wchar_t), // number of bytes allocated
&numBytesRead, // this will store number of bytes actually read NULL // not using overlapped IO
); if (result) {
buffer[numBytesRead / sizeof(wchar_t)] = NULL; // null terminate the string wcout << "Number of bytes read: " << numBytesRead << endl;
wcout << "Message: " << buffer << endl; }
else {
wcout << "Failed to read data from the pipe." << endl; }
const wchar_t *data = L"2"; // L"*** Hello Pipe World ***"; DWORD numBytesWritten = 0;
result = WriteFile(
pipe, // handle to our outbound pipe data, // data to send
wcslen(data) * sizeof(wchar_t), // length of data to send (bytes) &numBytesWritten, // will store actual amount of data sent
NULL // not using overlapped IO );
if (result) {
wcout << "Number of bytes sent: " << numBytesWritten << endl; }
else {
wcout << "Failed to send data." << endl;
// look up error code here using GetLastError() }
// Close our pipe handle CloseHandle(pipe);
wcout << "Done." << endl; system("pause");
return 0; }
132