例:EnumWindows +構造体使用例
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <windows.h> #include <iostream> struct MyData { int count; }; BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM 1Param) { MyData* data = reinterpret_cast<MyData*>(1Param); data->count++; // 確認のためウィンドウタイトルを表示 char title[256]; GetWindowTextA(hwnd, title, sizeof(title)); std:: cout «< "Window: " « title « std:: endl; return TRUE; //続行 } int main() { MyData data = { 0 }; EnumWindows (EnumWindowsProc, reinterpret_cast<LPARAM>(&data)); std::cout << "Window count: " << data.count << std: :endl; return 0; } |
補足ポイント
- reinterpret_cast を使って LPARAM に構造体ポインタを渡し、コールバック内で元の型に戻して使用します。
- LPARAM は64bit 環境でも対応できるように設計された型なので、ポインタの受け渡しに安全に使えます。

コメント