Help me please!

  1. You need to ensure that the keyboard messages that come to my window are written to one file.
  2. Consider the fact that the application can be launched many times and still the recording should be carried out in one file.

I coped with the first part, help, if you can, with the second:

program Project4; uses Windows, Messages, SysUtils; var WndClass: TWndClass; szAppName, buf: PChar; WinHandle: HWnd; Msg: TMsg; hFile: THandle; number: cardinal; ch: char; nice: boolean; function MyWndProc(hwnd: hwnd; msg: UINT; wparam: wparam; lparam: lparam): longint;stdcall; begin case Msg of WM_KEYDOWN: begin writefile(hFile, ' WM_KEYDOWN ',13, number, nil); end; WM_KEYUP: begin writefile(hFile, ' WM_KEYUP ',10, number, nil); end; WM_SYSKEYDOWN: begin writefile(hFile, ' WM_SYSKEYDOWN ',16, number, nil); end; WM_SYSKEYUP: begin writefile(hFile, ' WM_SYSKEYUP ',14, number, nil); end; WM_CHAR: begin writefile(hFile, ' WM_CHAR ',9, number, nil); end; WM_DEADCHAR: begin writefile(hFile, ' WM_DEADCHAR ',13, number, nil); end; WM_SYSCHAR: begin writefile(hFile, ' WM_SYSCHAR ',12, number, nil); end; WM_SYSDEADCHAR: begin writefile(hFile, ' WM_SYSDEADCHAR ',16, number, nil); end; WM_DESTROY: begin PostQuitMessage(0); Result := 0; end; { WM_CHAR: begin ch := chr(wparam); writefile(hFile, ch, sizeof(ch) * SizeOf(Char), number, nil); Result := 0; end; } else Result := DefWindowProc(hwnd, msg, wparam, lparam); end; end; begin szAppName := 'Hello windows.'; WndClass.style := CS_HREDRAW or CS_VREDRAW; WndClass.lpfnWndProc := @MyWndProc; WndClass.cbClsExtra := 0; WndClass.cbWndExtra := 0; WndClass.hInstance := hInstance; WndClass.hIcon := LoadIcon(hInstance, IDI_APPLICATION); WndClass.hCursor := LoadCursor(hInstance, IDC_ARROW); WndClass.hbrBackground := CreateSolidBrush($FFFFFF); WndClass.lpszMenuName := nil; WndClass.lpszClassName := szAppName; RegisterClass(WndClass); WinHandle := CreateWindow(WndClass.lpszClassName, 'Окно', WS_OVERLAPPEDWINDOW, 400, 400, 550, 250, 0, 0, hInstance, nil); ShowWindow(WinHandle, SW_SHOW); UpdateWindow(WinHandle); hFile := CreateFile('myfile.txt', GENERIC_READ or GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); while GetMessage(Msg, 0, 0, 0) do begin TranslateMessage(Msg); DispatchMessage(Msg); end; end. 

    1 answer 1

    To provide the second part, you consider about synchronization of the processes. You need to place the file in the critical section, or you can create an event that shows whether you can write to the file now or not, or a mutex, or a semaphore, ...