[c++] 新手windows programming [更新左, 麻煩巴打再睇下]
本帖最後由 影雪仔 於 2011-11-19 12:33 編輯
我想寫一個program, 可以log低晒所有run緊既process 同 process
而為每一個process都produce一個叫"Process"既object
我既寫法係用EnumWindows()
以下係我既code- #include <iostream>
- #include <fstream>
- #include <string>
- #include <windows.h>
- class Process;
- BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
- class Process
- {
- private:
- HWND PID;
- wchar_t* WNDTXT;
-
- public:
- static int ProcessCount;
- Process()
- {
- PID = NULL;
- WNDTXT = L"NA";
- //std::cout << "Constructor" << std::endl;
- }
- ~Process()
- {
- //std::cout << "Destructor" << std::endl;
- }
- void setPID(HWND hwnd)
- {
- ++ProcessCount;
- PID = hwnd;
- }
- void setWNDTXT(wchar_t* text)
- {
- //std::wcout << text << std::endl;
- WNDTXT = text;
- }
-
- int getPID()
- {
- return int(PID);
- }
- wchar_t* getWindowText()
- {
- return WNDTXT;
- }
- };
- int Process::ProcessCount = 0;
- Process process[500];
- int main()
- {
- WNDENUMPROC PCallBackFunc = EnumWindowsProc;
- LPARAM lparam = NULL;
- EnumWindows(PCallBackFunc, lparam);
-
- std::wofstream log("log.txt");
-
- if (log.is_open())
- {
- for (int i=0; i<Process::ProcessCount; i++)
- {
- log << i+1 << L"\t"
- << process[i].getPID() << L"\t\t"
- << process[i].getWindowText() << std::endl;
- }
- }
- log.close();
- return 0;
- }
- BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
- {
- wchar_t WNDTXT[256];
- GetWindowText(hwnd, WNDTXT, 256-1);
-
- std::wcout << WNDTXT << std::endl;
- process[Process::ProcessCount].setPID(hwnd);
- process[Process::ProcessCount].setWNDTXT(WNDTXT);
- return true;
- }
複製代碼 但係就有好多問題, 想請大家指教下 |
|
|