Solved

how to get WM_DRAWITEM message info. from other window?

Posted on 2004-08-29
16
709 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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
 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

623 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