[c++] 新手windows programming [更新左, 麻煩巴打再睇下]

本帖最後由 影雪仔 於 2011-11-19 12:33 編輯

我想寫一個program, 可以log低晒所有run緊既process 同 process
而為每一個process都produce一個叫"Process"既object
我既寫法係用EnumWindows()
以下係我既code
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <windows.h>

  5. class Process;
  6. BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);

  7. class Process
  8. {       
  9.         private:
  10.                 HWND PID;
  11.                 wchar_t* WNDTXT;
  12.        
  13.         public:
  14.                 static int ProcessCount;

  15.                 Process()
  16.                 {
  17.                         PID = NULL;
  18.                         WNDTXT = L"NA";
  19.                         //std::cout << "Constructor" << std::endl;
  20.                 }

  21.                 ~Process()
  22.                 {
  23.                         //std::cout << "Destructor" << std::endl;
  24.                 }

  25.                 void setPID(HWND hwnd)
  26.                 {
  27.                         ++ProcessCount;
  28.                         PID = hwnd;
  29.                 }

  30.                 void setWNDTXT(wchar_t* text)
  31.                 {
  32.                         //std::wcout << text << std::endl;
  33.                         WNDTXT = text;
  34.                 }
  35.                
  36.                 int getPID()
  37.                 {
  38.                         return int(PID);
  39.                 }

  40.                 wchar_t* getWindowText()
  41.                 {
  42.                         return WNDTXT;
  43.                 }
  44. };

  45. int Process::ProcessCount = 0;
  46. Process process[500];

  47. int main()
  48. {

  49.         WNDENUMPROC PCallBackFunc = EnumWindowsProc;
  50.         LPARAM lparam = NULL;

  51.         EnumWindows(PCallBackFunc, lparam);
  52.        
  53.         std::wofstream log("log.txt");
  54.        
  55.         if (log.is_open())
  56.         {
  57.                 for (int i=0; i<Process::ProcessCount; i++)
  58.                 {
  59.                         log << i+1 << L"\t"
  60.                                 << process[i].getPID() << L"\t\t"
  61.                                 << process[i].getWindowText() << std::endl;
  62.                 }
  63.         }

  64.         log.close();

  65.         return 0;
  66. }



  67. BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
  68. {
  69.         wchar_t WNDTXT[256];
  70.         GetWindowText(hwnd, WNDTXT, 256-1);
  71.        
  72.         std::wcout << WNDTXT << std::endl;
  73.         process[Process::ProcessCount].setPID(hwnd);
  74.         process[Process::ProcessCount].setWNDTXT(WNDTXT);
  75.         return true;
  76. }
複製代碼
但係就有好多問題, 想請大家指教下

1) 我其實想process依個object係 main裡面生產
   但係如果係main生產, 我就pass唔到去EnumWindows() 同 EnumWindowsProc()
   有咩辦法可以做到?

2) 我用Process process[500]黎delcare左500件object
   但係其實我係想佢每有1個proess先生產一個process object
   再入落個array
   但係我就唔識點樣做

3) 我run個個program, 但係出唔到我想要既result
   我估計係第89行出現問題, 我唔知點講...

4) 因為我係自學, 所以d programming pattern都想請教下大家

TOP

回復 2# 影雪仔

2)  可以用std::vector
http://www.cplusplus.com/reference/stl/vector/

TOP

http://www.codeproject.com/KB/threads/enumprocnt5.aspx
可以用 CreateToolhelp32Snapshot 記得所有process
但係冇HWND,唔知啱唔啱你使

答下你D問題先
1)
可以PASS 到, EnumWindows 的第二個 parameter 會pass左去你個callback function的第二個 parameter度, 即係
  1. EnumWindows(PCallBackFunc, &process);
複製代碼
咁係 EnumWindowsProc 的 lParam 就會等於 &process, 你cast 返佢就用到
2) 可以用 deque,list,vector,... 自己search 下 google
3) 其實 HWND同process ID係唔同的野, HWND 係Windows kernel 入面的一個handler 負責GUI componet eg button, editbox, dialog box
如果你想由HWND拎到佢 process ID可以用 http://msdn.microsoft.com/en-us/ ... 33522(v=vs.85).aspx

TOP

回復  影雪仔

2)  可以用std::vector
icarus-c 發表於 2011-11-16 22:46


我之前學C++, 飛左STL依個chapter, 睇黎要睇番XD

TOP

本帖最後由 影雪仔 於 2011-11-17 12:58 編輯
可以用 CreateToolhelp32Snapshot 記得所有process
但係冇HWND,唔知啱唔啱你使

答下你D問題先
1)
可以PA ...
KamSing 發表於 2011-11-17 00:40


感謝你花時間贂我d code!!
1)
  1. EnumWindows(PCallBackFunc, &process);
複製代碼
'EnumWindows' : cannot convert parameter 2 from 'Process *' to 'LPARAM'
   係咪要cast左先再pass?
3)拎 ProcessID係我第一步想做既野
   我最後想做到一個list左所有process出黎
   再睇到每個process 裡面有乜野handle
   之後直接send message去某d handle 去做野

TOP

本帖最後由 影雪仔 於 2011-11-19 12:48 編輯
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <windows.h>

  5. using std::wcout;
  6. using std::cout;
  7. using std::endl;

  8. class Process;
  9. BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);

  10. class Process
  11. {       
  12.         private:
  13.                 HWND PID;
  14.                 wchar_t* WNDTXT;
  15.        
  16.         public:
  17.                 static int ProcessCount;

  18.                 Process()
  19.                 {
  20.                         PID = NULL;
  21.                         WNDTXT = L"NA";
  22.                 }

  23.                 ~Process(){}

  24.                 void setPID(HWND hwnd)
  25.                 {
  26.                         ++ProcessCount;
  27.                         PID = hwnd;
  28.                 }

  29.                 void setWNDTXT(wchar_t* text)
  30.                 {
  31.                         WNDTXT = text;
  32.                         //wcout << WNDTXT << endl;
  33.                 }
  34.                
  35.                 int getPID()
  36.                 {
  37.                         return int(PID);
  38.                 }

  39.                 wchar_t* getWindowText()
  40.                 {
  41.                         //wcout << WNDTXT << endl;
  42.                         return WNDTXT;
  43.                 }
  44. };

  45. int Process::ProcessCount = 0;


  46. int main()
  47. {
  48.         Process process[500];

  49.         WNDENUMPROC PCallBackFunc = EnumWindowsProc;
  50.         LPARAM lparam = NULL;

  51.         EnumWindows(PCallBackFunc, LPARAM(&process));

  52.         for (int i=0; i<500; i++)
  53.         {
  54.                 wcout << process[i].getPID() << "\t\t" << endl;
  55.                 //wcout << process[i].getWindowText() << endl;
  56.         }
  57.         return 0;
  58. }



  59. BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
  60. {
  61.         wchar_t WNDTXT[256];
  62.         GetWindowText(hwnd, WNDTXT, 256-1);

  63.         Process* p2process = (Process*)lParam;

  64.         p2process[Process::ProcessCount].setPID(hwnd);
  65.         p2process[(Process::ProcessCount) - 1].setWNDTXT(WNDTXT);

  66.         return true;
  67. }
複製代碼

TOP

本帖最後由 影雪仔 於 2011-11-19 12:55 編輯

首先, 依個program就咁run, 出到一個幾似樣既result

但係uncomment左第69行, 就變成

點解就咁?

我懷疑係setWDNTXT出現問題, 於是我係第39行加一句, 其他冇用既output delete晒
就出左咁

證明WNDTXT已經改左

最後我係getWNDTXT中加多句黎test(49行)


問題應該就係係依個位
但係我唔明點解之前已經write 左落WNDTXT, 之後會冇左
另外, 就算WNDTXT有問題, endl 點都會有掛, 點解都冇埋
請巴打指教一下
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

TOP

char, wchar_t array的string 係唔可以用 = 去copy, 要用strncpy, wstrncpy
所以你個setWINTXT 失敗左

TOP

本帖最後由 影雪仔 於 2011-11-19 13:38 編輯
char, wchar_t array的string 係唔可以用 = 去copy, 要用strncpy, wstrncpy
所以你個setWINTXT 失敗左 ...
KamSing 發表於 2011-11-19 13:06
  1. #include <string>
  2. #include <iostream>
  3. using namespace std;

  4. void main()
  5. {
  6.         char* a = "qwerty";
  7.         char* b;

  8.         b = a;
  9.        
  10.         cout << b << endl;
  11. }
複製代碼
咁樣冇問題wo...

wstrcpy, 係咪應該要include <wstring.h>
點解我好似冇依個library

TOP