Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 719
  • Last Modified:

how to get WM_DRAWITEM message info. from other window?

Hi,
      I have a program that plugin to other application to get extra featuer, the window that i plugin is using ownder draw menu, im' success get the callback, notify message like WM_COMMAND and so on is correctly,
      but the problem is i get the WM_DRAWITEM also, but the data from lParam is wrong, all data is negative value, any idea? plesae help

code :

//this code only get the drawitem structure and write the data to the file, but all is negative value like -845559840
case WM_DRAWITEM:
{
  DRAWITEMSTRUCT drawItem;
  RECT rect;
  CopyMemory(&drawItem,(const void *)lParam,sizeof(DRAWITEMSTRUCT));
  CopyRect(&rect,&drawItem.rcItem);
  char str[256],str1[256];
  WriteLog(DRAWITEMLOG,"ITEM");
  itoa(drawItem.CtlID,str,10);
  WriteLog(DRAWITEMLOG,"      Ctl ID   : %s - %d",str,drawItem.CtlID);
  itoa(drawItem.itemID,str,10);
  WriteLog(DRAWITEMLOG,"      Item ID  : %s - %d",str,drawItem.itemID);
  itoa(drawItem.itemAction,str,10);
  WriteLog(DRAWITEMLOG,"      Item Act : %s - %d",str,drawItem.itemAction);
  WriteLog(DRAWITEMLOG,"      Item Rc  : %d,%d,%d,%d",(UINT)rect.left,(UINT)rect.top,(UINT)rect.right,(UINT)rect.bottom);
}

Thanks
0
sumn79
Asked:
sumn79
  • 10
  • 6
1 Solution
 
nonubikCommented:
I think the data is good, but the way you write it to lgo may be wrong. The reaso is that most of struct members are UINT (unsigned int) like 'CtlID', 'itemID', and you try to write them as integer (signed). An UINT has range from 0 to 2^32 - 1. An INT has a range from -(2^31) to 2^31 -1. So when you convert a value greater than 2^31-1 to INT, you'll get a negative value.

The fix will be the '%u' instead of '%d'
>WriteLog(DRAWITEMLOG,"     Ctl ID   : %s - %u",str,drawItem.CtlID);
0
 
sumn79Author Commented:
hi, thanks for ur reply, although i use %u, still get 3435973836, i try many way like HIWORD LOWORD, type cast (UINT), but still getting a wrong info

the result in file something like :
2004/08/31 01:01:47 _       Ctl ID   : -858993460 - LO(52428):HI(52428)
2004/08/31 01:01:47 _       Item ID  : -858993460 - LO(52428):HI(52428)
2004/08/31 01:01:47 _       Item Act : -858993460 - LO(52428):HI(52428)

actually there are more than one owner draw menu item but all of them getting a same value, if the data is correct, is not possible that the controlID/itemID and Item Act getting the same value

please help if any idea, i try to get this done more than a week

thanks
0
 
nonubikCommented:
3435973836 is the decimal value of 0xCCCCCCCC hex value. This looks like some uninitalized struct member. Try to print them all in hexa. '%x'

You don't need to CopyMemory, just a simple cast is enough.

case WM_DRAWITEM:
{
  DRAWITEMSTRUCT drawItem = *((LPDRAWITEMSTRUCT)lParam);
  RECT rect = drawItem.rcItem;
   
   WriteLog(DRAWITEMLOG,"     Ctl ID   : %x",drawItem.CtlID);
  .....
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nonubikCommented:
And try to see whether wParam is 0 (the WM_DRAWITEM message was sent from a menu).
0
 
sumn79Author Commented:
Hi, thanks for your advice, i change some of my code according to your suggestion :

Code :
case WM_DRAWITEM:
{
   DRAWITEMSTRUCT drawItem = *((LPDRAWITEMSTRUCT)lParam);
   RECT rect;
   if (wParam!=0)
   {
      CopyRect(&rect,&drawItem.rcItem);
      char str[256],str1[256];
      WriteLog(DRAWITEMLOG,"ITEM");
      itoa(drawItem.CtlID,str,10);
      WriteLog(DRAWITEMLOG,"      Ctl ID   : %s - %s HEX : 0x%X",str,LOHIWORD(drawItem.CtlID,str1),drawItem.CtlID);
      itoa(drawItem.itemID,str,10);
      WriteLog(DRAWITEMLOG,"      Item ID  : %s - %s",str,LOHIWORD(drawItem.itemID,str1));
      itoa(drawItem.itemAction,str,10);
      WriteLog(DRAWITEMLOG,"      Item Act : %s - %s",str,LOHIWORD(drawItem.itemAction,str1));
      WriteLog(DRAWITEMLOG,"      Item Rc  : %u,%u,%u,%u",(UINT)rect.left,(UINT)rect.top,(UINT)rect.right,(UINT)rect.bottom);

   }
   else
      WriteLog(DRAWITEMLOG,"!!!!!!!!!Warning : Zero");
}break;


Result in file:
2004/08/31 08:59:06 _ ITEM
2004/08/31 08:59:06 _       Ctl ID   : -858993460 - LO(52428):HI(52428) HEX : 0xCCCCCCCC
2004/08/31 08:59:06 _       Item ID  : -858993460 - LO(52428):HI(52428)
2004/08/31 08:59:06 _       Item Act : -858993460 - LO(52428):HI(52428)
2004/08/31 08:59:06 _       Item Rc  : 3435973836,3435973836,3435973836,3435973836
2004/08/31 08:59:07 _ !!!!!!!!!Warning : Zero
2004/08/31 08:59:07 _ !!!!!!!!!Warning : Zero
2004/08/31 08:59:07 _ !!!!!!!!!Warning : Zero
2004/08/31 08:59:07 _ !!!!!!!!!Warning : Zero
2004/08/31 08:59:07 _ !!!!!!!!!Warning : Zero

please give me more help, thanks alot!



0
 
nonubikCommented:
Well, wParam MUST be 0 for a menu item.
0
 
sumn79Author Commented:
i getting a same things :

2004/08/31 08:59:06 _ ITEM
2004/08/31 08:59:06 _      Ctl ID   : -858993460 - LO(52428):HI(52428) HEX : 0xCCCCCCCC
2004/08/31 08:59:06 _      Item ID  : -858993460 - LO(52428):HI(52428)
2004/08/31 08:59:06 _      Item Act : -858993460 - LO(52428):HI(52428)
2004/08/31 08:59:06 _      Item Rc  : 3435973836,3435973836,3435973836,3435973836

no matter the wParam is zero or not, and another things is that the callback proc is from another application, which i just hook to, and i try to create normal application is able to the the data correctly.

thanks again.
0
 
nonubikCommented:
How do you hook and how is the callback?
0
 
sumn79Author Commented:
the hook function is from dscwpmsg.dll (get from somewhere), where the function like SetCWPMSGHook(HWND hWndToHook,void *callbackProc,void *callbackProc);

WINMAIN WinMain(........)
{
//........
   HWND toHookhWnd=FindRequriedWindowToHook(); //any window like IE/MSN/Excel, as long as is able to found by either FindWindow/WindowFromPoint/EnumWindow
   SetCWPMSGHook(toHookhWnd,callbackProc,callbackProc);
//........
}

//the other window application will come here, include WM_COMMAND/WM_CLOSE/LBN_SELCHANGE and so on......
HRESULT WINAPI callbackProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
   switch (uMsg)
   {
      case WM_COMMAND:
         //working area
         break;
      case WM_DRAWITEM:
         //where the data get wrong here
         break;
   }
}

the reason of why i use other people library is i don't know how to get the callback on other windows

thanks
0
 
nonubikCommented:
Ok.

Firts of all you should check drawItem.CtlType, for a menu should be ODT_MENU (1). Now you know whether the DRAWITEM message is sent for an owner draw menu. Then, drawItem.CtlID is not used for a menu (read carefully the MSDN!). If even now you son't have regular values in your lParam, I suspect the callback is not properly working.
0
 
sumn79Author Commented:
i try to write the CtlID but the value is same, so if something wrong on the callback, i think i have to create my own, because i can't find a working sample that work on Win32 (im' using VisualC++ none MFC), i don't know wheter im' correct that i have to use SetWindowsHookEx( ) with a .dll to get this done?

if anybody have a sample code, or know how to hook to other window, please help

thanks
0
 
nonubikCommented:
Fore more info on hooks, you can take a look here: http:Q_20989953.html or http:Q_21017098.html
0
 
nonubikCommented:
Well, if you were not pleased by the comments, you shouldn't accept a C answer. I'm rather pleased by non-accepting than a C.
0
 
sumn79Author Commented:
sorry about that, i thought i can accept of each comment, really sorry about that, and thanks for your advice
0
 
nonubikCommented:
For how to accept an answer in the future, please take a look here
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/help.jsp#hi68

If you would like to 'repair' this question grade, you can post a question in Community Support TA requesting this.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now