Solved

how to get WM_DRAWITEM message info. from other window?

Posted on 2004-08-29
16
694 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
simple way to manipulate chinese characters in vb6 application. 13 281
PDF library for Delphi 2 107
Add values of each row in an array 3 50
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 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

20 Experts available now in Live!

Get 1:1 Help Now