Solved

how to get WM_DRAWITEM message info. from other window?

Posted on 2004-08-29
16
689 Views
Last Modified: 2012-06-27
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
Comment
Question by:sumn79
  • 10
  • 6
16 Comments
 
LVL 16

Accepted Solution

by:
nonubik earned 140 total points
ID: 11931222
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
 

Author Comment

by:sumn79
ID: 11937846
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
 
LVL 16

Expert Comment

by:nonubik
ID: 11939835
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
 
LVL 16

Expert Comment

by:nonubik
ID: 11939842
And try to see whether wParam is 0 (the WM_DRAWITEM message was sent from a menu).
0
 

Author Comment

by:sumn79
ID: 11939988
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
 
LVL 16

Expert Comment

by:nonubik
ID: 11940051
Well, wParam MUST be 0 for a menu item.
0
 

Author Comment

by:sumn79
ID: 11940092
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
 
LVL 16

Expert Comment

by:nonubik
ID: 11940114
How do you hook and how is the callback?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:sumn79
ID: 11940247
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
 
LVL 16

Expert Comment

by:nonubik
ID: 11940294
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
 

Author Comment

by:sumn79
ID: 11940968
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
 
LVL 16

Expert Comment

by:nonubik
ID: 11940997
Fore more info on hooks, you can take a look here: http:Q_20989953.html or http:Q_21017098.html
0
 
LVL 16

Expert Comment

by:nonubik
ID: 11949565
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
 

Author Comment

by:sumn79
ID: 11950020
sorry about that, i thought i can accept of each comment, really sorry about that, and thanks for your advice
0
 
LVL 16

Expert Comment

by:nonubik
ID: 11950048
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
 
LVL 16

Expert Comment

by:nonubik
ID: 11950055
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now