S. Bauermann Geschrieben 12. Januar 2002 Geschrieben 12. Januar 2002 Ich habe leider noch keine große Erfahrung beim Dateihandling mit C++. Ich möchte die Funktionen JournalPlaybackFunc und JournalRecordFunc für das Aufzeichnen von SystemwideHooks nutzen. Es gibt in den technical Articles der MSDN ein Example zu diesen Thema. Dort werden die Funktionen so definiert das die Recorddaten in ein Array gespeichert werden. So gehen sie gleich nach einmaligen Playback wieder verloren. Ich möchte diese Daten in eine Datei speichern und von dort für das Playback wieder auslesen. Ich habe schon ziehlich viel probiert und kann langsam nicht mehr auf meinem Stuhl sitzen. Könnte mir vielleicht jemand erklären wie das bei diesen Beispiel aussehen muß. Die beiden Funktionen sind für eine dll geschrieben, wegen der Hooks. Kann man überhaupt von einer dll aus Dateien anlegen ? Vielen Dank sagt ein FIA Schüler. Grüße von Sven //--------------------------------------------------------------------------- // JournalPlaybackFunc // // Filter function for the WH_JOURNALPLAYBACK // //--------------------------------------------------------------------------- LRESULT CALLBACK JournalPlaybackFunc (int nCode, WPARAM wParam, LPARAM lParam ) { static int nRepeatRequests; static DWORD dwTimeAdjust; static DWORD dwLastEventTime; HDC hDC; LPEVENTMSG lpEvent; long lReturnValue; HMENU hMenu; if ( nCode >= 0 ) { // No Playback if we haven't recorded an Event // // No Playback While recording. // This is not a limitation of the hooks. // This is only because of the simple event storage used in this example // // We should never get here since the enable / disable menu stuff should // make getting here impossible // if ( lpEventChain == NULL || HookStates[JOURNALRECORDINDEX]) { InstallFilter(JOURNALPLAYBACKINDEX, FALSE); hMenu = GetMenu(hwndMain); CheckMenuItem(hMenu, IDM_JOURNALPLAYBACK, MF_UNCHECKED | MF_BYCOMMAND); EnableMenuItem(hMenu, IDM_JOURNALPLAYBACK, MF_DISABLED | MF_GRAYED | MF_BYCOMMAND); wsprintf((LPSTR)szFilterLine[JOURNALPLAYBACKINDEX], "WH_JOURNALPLAYBACK -- No recorded Events to Playback or JournalRecord in Progress "); hDC = GetDC(hwndMain); TabbedTextOut(hDC, 1, nLineHeight * JOURNALPLAYBACKINDEX, (LPSTR)szFilterLine[JOURNALPLAYBACKINDEX], strlen(szFilterLine[JOURNALPLAYBACKINDEX]), 0, NULL, 1); ReleaseDC(hwndMain, hDC); return( (int)CallNextHookEx(hhookHooks[JOURNALPLAYBACKINDEX], nCode, wParam, lParam )); } if ( lpPlayEvent == NULL ) { lpPlayEvent = lpEventChain; lpLastEvent = NULL; // For the next time we start the recorder dwTimeAdjust = GetTickCount() - dwStartRecordTime; dwLastEventTime = (DWORD) GetTickCount(); nRepeatRequests = 1; } if (nCode == HC_SKIP ) { nRepeatRequests = 1; if ( lpPlayEvent->lpNextEvent == NULL ) { wsprintf((LPSTR)szFilterLine[JOURNALPLAYBACKINDEX], "WH_JOURNALPLAYBACK -- Done Recorded Events"); hDC = GetDC(hwndMain); TabbedTextOut(hDC, 1, nLineHeight * JOURNALPLAYBACKINDEX, (LPSTR)szFilterLine[JOURNALPLAYBACKINDEX], strlen(szFilterLine[JOURNALPLAYBACKINDEX]), 0, NULL, 1); ReleaseDC(hwndMain, hDC); free(lpEventChain); lpEventChain = lpPlayEvent = NULL ; InstallFilter(JOURNALPLAYBACKINDEX, FALSE); hMenu = GetMenu(hwndMain); CheckMenuItem(hMenu, IDM_JOURNALPLAYBACK, MF_UNCHECKED | MF_BYCOMMAND); EnableMenuItem(hMenu, IDM_JOURNALPLAYBACK, MF_DISABLED | MF_GRAYED | MF_BYCOMMAND); } else { dwLastEventTime = lpPlayEvent->Event.time; lpPlayEvent = lpPlayEvent->lpNextEvent; free(lpEventChain); lpEventChain = lpPlayEvent; } } else if ( nCode == HC_GETNEXT) { lpEvent = (LPEVENTMSG) lParam; lpEvent->message = lpPlayEvent->Event.message; lpEvent->paramL = lpPlayEvent->Event.paramL; lpEvent->paramH = lpPlayEvent->Event.paramH; lpEvent->time = lpPlayEvent->Event.time + dwTimeAdjust; wsprintf((LPSTR)szFilterLine[JOURNALPLAYBACKINDEX], "WH_JOURNALPLAYBACK -- Playing Event %d times ", nRepeatRequests++); hDC = GetDC(hwndMain); TabbedTextOut(hDC, 1, nLineHeight * JOURNALPLAYBACKINDEX, (LPSTR)szFilterLine[JOURNALPLAYBACKINDEX], strlen(szFilterLine[JOURNALPLAYBACKINDEX]), 0, NULL, 1); ReleaseDC(hwndMain, hDC); lReturnValue = lpEvent->time - GetTickCount(); // // No Long ( negative ) waits // if ( lReturnValue < 0L ) { lReturnValue = 0L; lpEvent->time = GetTickCount(); } return ( (DWORD) lReturnValue ); } hDC = GetDC(hwndMain); TabbedTextOut(hDC, 1, nLineHeight * JOURNALPLAYBACKINDEX, (LPSTR)szFilterLine[JOURNALPLAYBACKINDEX], strlen(szFilterLine[JOURNALPLAYBACKINDEX]), 0, NULL, 1); ReleaseDC(hwndMain, hDC); } return( CallNextHookEx(hhookHooks[JOURNALPLAYBACKINDEX], nCode, wParam, lParam)); } //--------------------------------------------------------------------------- // JournalRecordFunc // // Filter function for the WH_JOURNALRECORD // //--------------------------------------------------------------------------- LRESULT CALLBACK JournalRecordFunc (int nCode, WPARAM wParam, LPARAM lParam ) { HDC hDC; EventNode *lpEventNode; LPEVENTMSG lpEvent; HMENU hMenu; if ( nCode >= 0 ) { lpEvent = (LPEVENTMSG) lParam; // // Skip recording while playing back // This is not a limitation of the hooks. // This is only because of the simple event storage used in this example // if ( HookStates[JOURNALPLAYBACKINDEX] ) { wsprintf((LPSTR)szFilterLine[JOURNALRECORDINDEX], "WH_JOURNALRECORD\tSkipping Recording during Playback "); hDC = GetDC(hwndMain); TabbedTextOut(hDC, 1, nLineHeight * JOURNALRECORDINDEX, (LPSTR)szFilterLine[JOURNALRECORDINDEX], strlen(szFilterLine[JOURNALRECORDINDEX]), 0, NULL, 1); ReleaseDC(hwndMain, hDC); return 0; } // // Stop recording ? // if ( lpEvent->message == WM_KEYDOWN && LOBYTE(lpEvent->paramL) == VK_F2 ) { wsprintf((LPSTR)szFilterLine[JOURNALRECORDINDEX], "WH_JOURNALRECORD\tRecording Stopped with F2 "); InstallFilter(JOURNALRECORDINDEX, FALSE); hMenu = GetMenu(hwndMain); CheckMenuItem(hMenu, IDM_JOURNALRECORD, MF_UNCHECKED | MF_BYCOMMAND); EnableMenuItem(hMenu, IDM_JOURNALPLAYBACK, MF_ENABLED | MF_BYCOMMAND); return 0; } if ( (lpEventNode = (EventNode *)malloc(sizeof(EventNode))) == NULL ) { wsprintf((LPSTR)szFilterLine[JOURNALRECORDINDEX], "WH_JOURNALRECORD\tNo Memory Available"); InstallFilter(JOURNALRECORDINDEX, FALSE); hMenu = GetMenu(hwndMain); CheckMenuItem(hMenu, IDM_JOURNALRECORD, MF_UNCHECKED | MF_BYCOMMAND); EnableMenuItem(hMenu, IDM_JOURNALPLAYBACK, MF_ENABLED | MF_BYCOMMAND); } if ( lpLastEvent == NULL ) { dwStartRecordTime = (DWORD) GetTickCount(); lpEventChain = lpEventNode; } else { lpLastEvent->lpNextEvent = lpEventNode; } lpLastEvent = lpEventNode; lpLastEvent->lpNextEvent = NULL; lpLastEvent->Event.message = lpEvent->message; lpLastEvent->Event.paramL = lpEvent->paramL; lpLastEvent->Event.paramH = lpEvent->paramH; lpLastEvent->Event.time = lpEvent->time; wsprintf((LPSTR)szFilterLine[JOURNALRECORDINDEX], "WH_JOURNALRECORD\tRecording\tTime:%d\tPRESS F2 To Stop Recording\t%s ", lpEvent->time,szMessageString(lpEvent->message)); hDC = GetDC(hwndMain); TabbedTextOut(hDC, 1, nLineHeight * JOURNALRECORDINDEX, (LPSTR)szFilterLine[JOURNALRECORDINDEX], strlen(szFilterLine[JOURNALRECORDINDEX]), 0, NULL, 1); ReleaseDC(hwndMain, hDC); return 0; } return (CallNextHookEx(hhookHooks[JOURNALRECORDINDEX], nCode, wParam, lParam)); } Zitieren
paule22 Geschrieben 13. Januar 2002 Geschrieben 13. Januar 2002 // This library allows to make arrays on disk. #include <stdio.h> #include <string.h> #include <stdlib.h> template <class T> class disk_arr_line; template <class T> class disk_arr_element; template <class T> class disk_arr { private: FILE *memfile; char *file_name; const long xsize, ysize; const bool ftype; const bool rcheck; bool fstatus; T* zero_value; void range_error(long x, long y) const; friend class disk_arr_line<T>; friend class disk_arr_element<T>; public: disk_arr(char *, long, long, const T&, bool type=false, bool check=false); disk_arr(const disk_arr&); ~disk_arr(); bool operator!() const { return (!fstatus); } disk_arr_line<T> operator[](long x) { disk_arr_line<T> temp(x, this); return temp; } }; template <class T> disk_arr<T>::disk_arr(char *fname, long x, long y, const T& zero, bool type=false, bool check=false): xsize(x), ysize(y), ftype(type), rcheck(check) { zero_value = new T(zero); char* bytes; file_name = new char[strlen(fname)+1]; strcpy(file_name, fname); if (ftype) { memfile = fopen(fname, "r+b"); if (memfile) { fstatus = true; return; } } memfile = fopen(fname, "w+b"); if (!memfile) { fstatus = false; return; } fstatus = true; bytes=(char*)zero_value; for(long c1=0; c1<(xsize*ysize); c1++) for(int c2=0; c2<sizeof(T); c2++) fprintf(memfile, "%c", bytes[c2]); return; } template <class T> disk_arr<T>::disk_arr(const disk_arr& a): xsize(0), ysize(0), ftype(a.ftype), rcheck(0) { printf("\a\aError: disk_arr objects cannot be copied, \r\n"); printf("passed to function by value or returned from\r\n"); printf("function by value. \r\n"); printf("Other words: copy constructor can't be called.\r\n"); fclose(memfile); abort(); } template <class T> disk_arr<T>::~disk_arr() { fclose(memfile); if (!ftype) remove(file_name); delete []file_name; delete zero_value; return; } template <class T> void disk_arr<T>::range_error(long x, long y) const { fprintf(stderr, "Range check error in file %s,\r\n", file_name); fprintf(stderr, "X=%li, Y=%li, maxX=%li, maxY=%li.\r\n",x, y, xsize-1, ysize-1); abort(); } template <class T> class disk_arr_line { private: const long x; disk_arr<T>* a; disk_arr_line(long x1, disk_arr<T>* a1): x(x1), a(a1) {} friend class disk_arr<T>; public: disk_arr_element<T> operator[](long y) { disk_arr_element<T> temp(x, y, a); return temp; } }; template <class T> class disk_arr_element { private: const long x; const long y; disk_arr<T>* a; disk_arr_element(long x1, long y1, disk_arr<T>* a1): x(x1), y(y1), a(a1) {} friend disk_arr_line<T>; public: disk_arr_element<T> operator= (const T& r); disk_arr_element<T> operator= (disk_arr_element<T> el) { return (operator=(T(el))); } operator T() const; }; template <class T> disk_arr_element<T> disk_arr_element<T>::operator=(const T& r) { char* bytes; if ((x>=a->xsize)||(x<0)||(y<0)||(y>=a->ysize)) if(a->rcheck==true) a->range_error(x, y); else return (*this); fseek(a->memfile, sizeof(T)*(a->ysize*x+y), 0); bytes=(char*)&r; for(unsigned int c2=0; c2<sizeof(T); c2++) fprintf(a->memfile, "%c", bytes[c2]); return (*this); } template <class T> disk_arr_element<T>::operator T() const { if ((x>=a->xsize)||(x<0)||(y<0)||(y>=a->ysize)) if(a->rcheck==true) a->range_error(x, y); else return *(a->zero_value); char* bytes; T* r; r = new T; bytes=(char*)r; fseek(a->memfile, sizeof(T)*(a->ysize*x+y), 0); for(unsigned int c2=0; c2<sizeof(T); c2++) fscanf(a->memfile, "%c", &bytes[c2]); delete r; return (*r); } ------------------------------------------------------------------------- #include <iostream.h> #include "disk_arr.h" disk_arr<int> a("c:\\disk_arr1.tmp",10L,10L, 7, false, true); main() { int b; ios::sync_with_stdio; // Here I put it because disk_arr.h use stdio.h // for file input/output, but I am using iosteam.h // This line synchronize this two I/O libraries if(!a) return 0; // Check that file is opened good, if not - exit. cout << "This example checks disk_arr library" << endl; a[1][6] = 6; b = a[2][8]; a[2][9] = b + a[1][6]; a[9][0] = a[1][6]; a[5][5] = a[9][0] + a[2][9]*b - b*b; int sum=0; for(long x=0; x<10; x++) for(long y=0; y<10; y++) sum = sum + a[x][y]; cout << "Value below should be 745 if library works correctly" << endl; cout << "Value = " << sum << endl; return 0; } good luck ... Zitieren
Empfohlene Beiträge
Dein Kommentar
Du kannst jetzt schreiben und Dich später registrieren. Wenn Du ein Konto hast, melde Dich jetzt an, um unter Deinem Benutzernamen zu schreiben.