#include <windows.h> class kWnd { public: kWnd(LPCTSTR windowName, HINSTANCE hInst, int cmdShow, LRESULT(WINAPI *pWndProc)(HWND, UINT, WPARAM, LPARAM), LPCTSTR menuName = NULL, int x = CW_USEDEFAULT, int y = 0, int width = CW_USEDEFAULT, int height = 0, UINT classStyle = CS_HREDRAW | CS_VREDRAW, DWORD windowStyle = WS_OVERLAPPEDWINDOW, HWND hParent = NULL); HWND GetHWnd() {return hWnd;} protected: HWND hWnd; WNDCLASSEX wc; };
KWnd.cpp #include "KWnd.h" kWnd::kWnd(LPCTSTR windowName, HINSTANCE hInst, int cmdShow, LRESULT (WINAPI *pWndProc)(HWND,UINT,WPARAM,LPARAM), LPCTSTR menuName, int x, int y, int width, int height, UINT classStyle, DWORD windowStyle, HWND hParent) { char szClassName[] = "KWndClass"; wc.cbSize = sizeof(wc); wc.style = classStyle; wc.lpfnWndProc = pWndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInst; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = menuName; wc.lpszClassName = szClassName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // if (!RegisterClassEx(&wc)) { char msg[100] = "Cannot register class: "; strcat(msg, szClassName); MessageBox(NULL, msg, "Error", MB_OK); return; } // hWnd = CreateWindow(szClassName, windowName, windowStyle, x, y, width, height, hParent, (HMENU)NULL, hInst, NULL); if (!hWnd) { char text[100] = "Cannot create window: "; strcat(text, windowName); MessageBox(NULL, text, "Error", MB_OK); return; } // ShowWindow(hWnd, cmdShow); }
#include <windows.h> #include <stdio.h> #include "resource.h" #include "kWnd.h" #define MYDISPLAY 1 char a1[100]; char razn[100]; char colvo[100]; int i; int n = 0; int summ = 0; typedef struct tagMYREC { char a1[100]; char razn[100]; char colvo[100]; char n[100]; char summ[100]; } MYREC; COPYDATASTRUCT MyCDS; MYREC MyRec; PCOPYDATASTRUCT pMyCDS; BOOL CALLBACK DialogProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //======================================================================= int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; kWnd mainWnd("Application A", hInstance, nCmdShow, WndProc, MAKEINTRESOURCE(IDR_MENU1), 100, 100, 450, 150); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (msg.wParam); } //======================================================================= LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { static HINSTANCE hInst; switch(uMsg) { case WM_CREATE: DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, DialogProc); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return 0; } BOOL CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { HWND hwndServer; WIN32_FIND_DATA FindFileData; HANDLE hFile; switch(uMsg) { case WM_INITDIALOG: hwndServer = FindWindow(NULL, "Application B"); if(!hwndServer) { hFile = FindFirstFile("Application B.exe", &FindFileData); WinExec("Application B.exe", 1); FindClose(hFile); } break; case WM_COPYDATA: pMyCDS = (PCOPYDATASTRUCT) lParam; SetDlgItemText(hDlg, IDC_EDIT4, (LPCSTR)((MYREC *)(pMyCDS->lpData))->n); SetDlgItemText(hDlg, IDC_EDIT5, (LPCSTR)((MYREC *)(pMyCDS->lpData))->summ); return true; case WM_COMMAND: switch(LOWORD(wParam)) { case IDEXIT: EndDialog(hDlg, 0); PostQuitMessage(0); case IDOK: GetDlgItemText(hDlg, IDC_EDIT1, MyRec.a1, 22); GetDlgItemText(hDlg, IDC_EDIT2, MyRec.razn, 22); GetDlgItemText(hDlg, IDC_EDIT3, MyRec.colvo, 22); hwndServer = FindWindow(NULL, "Application B"); if(!hwndServer) { MessageBox(hDlg, " !", "Application A", MB_OK); break; } MyCDS.dwData = MYDISPLAY; MyCDS.cbData = sizeof( MyRec ); MyCDS.lpData = &MyRec; SendMessage( hwndServer, WM_COPYDATA, (WPARAM)(HWND) hDlg, (LPARAM) (LPVOID) &MyCDS ); break; return TRUE; } } return FALSE; }
typedef struct tagMYREC { char a1[100]; char razn[100]; char colvo[100]; char n[100]; char summ[100]; } MYREC; COPYDATASTRUCT MyCDS; MYREC MyRec; PCOPYDATASTRUCT pMyCDS;
COPYDATASTRUCT MyCDS - , . MYREC MyRec - . PCOPYDATASTRUCT pMyCDS - ( ).
case WM_INITDIALOG: hwndServer = FindWindow(NULL, "Application B"); if(!hwndServer) { hFile = FindFirstFile("Application B.exe", &FindFileData); WinExec("Application B.exe", 1); FindClose(hFile); }
GetDlgItemText(hDlg, IDC_EDIT1, MyRec.a1, 22); GetDlgItemText(hDlg, IDC_EDIT2, MyRec.razn, 22); GetDlgItemText(hDlg, IDC_EDIT3, MyRec.colvo, 22);
MyCDS.dwData = MYDISPLAY; // MyCDS.cbData = sizeof( MyRec ); // MyCDS.lpData = &MyRec; // SendMessage( hwndServer, WM_COPYDATA, // // (WPARAM)(HWND) hDlg, (LPARAM) (LPVOID) &MyCDS ); // COPYDATASTRUCT
#include <windows.h> #include <stdio.h> #include "kWnd.h" #include "resource.h" #define MYDISPLAY 1 PCOPYDATASTRUCT pMyCDS; // int i; int a1; int razn; int colvo; int n = 0; int summ = 0; char str[100], str1[100], str2[100], str3[100], str4[100]; // DWORD dwSize; HANDLE File; // typedef struct tagMYREC // { char a1[100]; char razn[100]; char colvo[100]; char n[100]; char summ[100]; } MYREC; COPYDATASTRUCT MyCDS; MYREC MyRec; //=============== =================== LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //===================================================== int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { MSG msg; // kWnd mainWnd("Application B", hInstance, nShowCmd, WndProc, NULL, 100, 100, 450, 220); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (msg.wParam); } //===================================================== LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { static HINSTANCE hInst; HDC hdc; // PAINTSTRUCT ps; // // HWND hwndServer; static HANDLE hThreadA; ThreadManager* pTm; WIN32_FIND_DATA FindFileData; HANDLE hFile; HWND edit; char buff[260]; switch(uMsg) { case WM_CREATE: hwndServer = FindWindow(NULL, "Application A"); if(!hwndServer) { hFile = FindFirstFile("Application A.exe", &FindFileData); WinExec("Application B.exe", 1); FindClose(hFile); } edit = CreateWindow("EDIT","text.txt", // WS_CHILD | WS_VISIBLE |WS_BORDER, 10, 10, 50, 20, hWnd, (HMENU) 123, (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE), NULL); break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); TextOut(hdc, 100, 50, " :", sizeof(" :")); TextOut(hdc, 120, 80, str1, sizeof(str1)); TextOut(hdc, 120, 100, str2, sizeof(str2)); TextOut(hdc, 120, 120, str3, sizeof(str3)); TextOut(hdc, 50, 140, str4, sizeof(str4)); EndPaint(hWnd, &ps); break; case WM_COPYDATA: pMyCDS = (PCOPYDATASTRUCT) lParam; // n- a1 = atol(((MYREC *)(pMyCDS->lpData))->a1); razn = atol(((MYREC *)(pMyCDS->lpData))->razn); colvo = atol(((MYREC *)(pMyCDS->lpData))->colvo); n = a1; summ = a1; for(int i = 0; i<colvo-1; i++) { n = n * razn; summ = n + summ; } sprintf(((MYREC *)(pMyCDS->lpData))->n, "%d", n); sprintf(((MYREC *)(pMyCDS->lpData))->summ, "%d", summ); hwndServer = FindWindow(NULL, "Application A"); if(!hwndServer) { MessageBox(hWnd, " !", "Application B", MB_OK); break; } strcpy(MyRec.n, ((MYREC *)(pMyCDS->lpData))->n); strcpy(MyRec.summ, ((MYREC *)(pMyCDS->lpData))->summ); MyCDS.dwData = MYDISPLAY; MyCDS.cbData = sizeof( MyRec ); // size of data MyCDS.lpData = &MyRec; // data structure SendMessage( hwndServer, WM_COPYDATA, (WPARAM)(HWND) hWnd, (LPARAM) (LPVOID) &MyCDS ); sprintf(str1, " : %s", ((MYREC *)(pMyCDS->lpData))->a1); sprintf(str2, " : %s", ((MYREC *)(pMyCDS->lpData))->razn); sprintf(str3, " : %s", ((MYREC *)(pMyCDS->lpData))->colvo); // edit = FindWindowEx(hWnd, NULL, "Edit", NULL); SendMessage(edit, WM_GETTEXT, (WPARAM)260, (LPARAM)buff); sprintf(str, " = %s, = %s, n = %s, = %s, = %s\n\r", ((MYREC *)(pMyCDS->lpData))->a1, ((MYREC *)(pMyCDS->lpData))->razn, ((MYREC *)(pMyCDS->lpData))->colvo, MyRec.n, MyRec.summ); // File=CreateFile(buff, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); SetFilePointer(File, 0, 0, FILE_END); // WriteFile(File,&str,sizeof(str),&dwSize,NULL); sprintf(str4, " : %s", buff); InvalidateRect(hWnd, NULL, TRUE); break; case UM_THREAD_DONE: pTm = (ThreadManager*)wParam; sprintf(str1, "%s: count = %d", pTm->name, pTm->nValue); InvalidateRect(hWnd, NULL, FALSE); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return 0; } //=====================================================
pMyCDS = (PCOPYDATASTRUCT) lParam; //
a1 = atol(((MYREC *)(pMyCDS->lpData))->a1)
Source: https://habr.com/ru/post/149299/
All Articles