Get the (resource) ID from hWnd

前端 未结 2 1608
情歌与酒
情歌与酒 2021-01-13 10:46

In ResourceHacker, when you open an executable (windows), you can see identifiers associated with dialogs. Does anyone have an idea from where they come? I mean, how can I d

2条回答
  •  -上瘾入骨i
    2021-01-13 11:01

    Here you find a good answer: http://blogs.msdn.com/b/oldnewthing/archive/2005/07/08/436815.aspx

    It's like asking, "Given a plate of food, how do I recover the original cookbook and page number for the recipe?" By doing a chemical analysis of the food, you might be able to recover "a" recipe, but there is nothing in the food itself that says, "I came from The Joy of Cooking, page 253."

    So the answer is that there is no way provided by Microsoft to obtain the dialog ID. They could have easily stored it anywhere to make it available, but they did not.

    But there would still be a way to do it, although it is not bulletproof. You could:

    1.) Get the creator file of the dialog via GetWindowModuleFileName()

    2.) Load this Exe or Dll via LoadLibraryEx(..., LOAD_LIBRARY_AS_IMAGE_RESOURCE)

    3.) Enumerate all RT_DIALOG resources in the Exe or Dll via EnumResourceNames() where the dialog ID is in the name: ResourceName = MAKEINTRESOURCE(IDD_DIALOG_ID)

    4.) Create each enumerated dialog invisibly via LoadResource(), LockResource(), CreateDialogIndirect() but without showing the dialog with ShowWindow().

    5.) Enumerate the child controls in each dialog via EnumChildWindows() and compare them to your dialog.

    6.) Release all handles and destroy the dialogs.

    It is not very probable that there are two identical dialogs in a Exe/Dll file. But the problem is that in WM_INITDIALOG the programmer may eliminate (destroy) or add or modify child controls. So your search algorithm would have to be fault tolerant. This would be possible by counting the congruency between each dialog from the resources and your dialog. You could count for how many child controls the ID (GetDlgCtrlID())and class name (GetClassName()) match. (e.g. Class="BUTTON" and ID = 311") While a programmer can easily change the text of a control or move it around, changing the ID is not very probable and does not make much sense and changing the class of a child control is even impossible.

    As I said: It is not bullet proof, but you will find the ID of the resource that has most probably been used to create the dialog.

    Be aware that not all dialogs come from a Microsoft resource. They can be created by a GUI framework that uses its own type of templates. In this case you will never find the Dialog ID because it simply does not exist.

提交回复
热议问题