473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Help on Windows API please??

Hi:

I'm trying to find someone who knows the Windows API, especially as used in C# .NET.

Specifically, I'm trying to use:
GetMenuItemInfo(hMenu, MenuItemID, false, ref mif);

I need to specify also that I'm trying to control one application from within another one. The platform is Windows 2000 Pro.

I have the MenuID in hMenu, checked by IsMenu and the MenuItemID is also a valid ID (I also tried this using the position with the 3rd param set to true).
The param mif is a structure (see below).

I even used AttachThreadInput(nhWndThreadID, nThisThreadID, 1);
This returns 1, indicating success.

No matter what I do, GetMenuItemInfo returns 0 and does not produce any information on any given menu item.

[StructLayout(LayoutKind.Sequential)]
public struct MENUITEMINFO
{
public uint cbSize;
public uint fMask;
public uint fType;
public uint fState;
public int wID;
public int hSubMenu;
public int hbmpChecked;
public int hbmpUnchecked;
public int dwItemData;
public string dwTypeData;
public uint cch;
public int hbmpItem;
}
Apr 27 '06 #1
8 5362
Banfa
9,065 Expert Mod 8TB
When calling functions like this you need to set the size parameter in the structure first before the call so that the API can identify what version of the strcuture you have passed it.

In c this would be done by

Expand|Select|Wrap|Line Numbers
  1.    mif.cbSize = sizeof(mif);
  2.  
beforecalling GetMenuItemInfo
Apr 27 '06 #2
Thanks for the reply.
I am filling the cbSize variable with the size of mif as you suggested.
Any other ideas?

Here is the whole proc:

private void setMenuCommand(int hWnd, int hMenu, uint nPos, int MenuItemID)
{
string sLabel = "";
StringBuilder sBuffer = new StringBuilder(nBuffLen);
WinAPI.MENUITEMINFO mif = new WinAPI.MENUITEMINFO();

mif.dwTypeData = null;// sBuffer.ToString();
try
{
int a = WinAPI.GetMenuItemInfo(hMenu, MenuItemID, false, ref mif);
if(a == 0)
{
//throw new Win32Exception(Marshal.GetLastWin32Error());
return;
}
sBuffer = new StringBuilder((int)(mif.cch + 1));
mif.cbSize = (uint)Marshal.SizeOf(typeof(WinAPI.MENUITEMINFO));
mif.fMask = 0x10;
mif.dwTypeData = sBuffer.ToString();
a = WinAPI.GetMenuItemInfo(hMenu, MenuItemID, false, ref mif);
if(a != 0)
{
mif.dwTypeData = new String('\0', (int)(mif.cch + 1));
sLabel = mif.dwTypeData.Trim();
if(sLabel != "" && sLabel != "-")
{
MenuCommand oMnuCmd = new MenuCommand();
oMnuCmd.sLabel = sLabel;
oMnuCmd.hMenu = hMenu;
oMnuCmd.nPos = nPos;
oMnuCmd.hMenuItemID = MenuItemID;
oMnuCmd.hWin = hWnd;
clMenuCommands.Add(sLabel, oMnuCmd);
}
}
}
catch(Exception exp)
{
throw new Exception("Error setting menu command: " + exp.Message);
}
}
Apr 28 '06 #3
Banfa
9,065 Expert Mod 8TB
See embedded comments in the code in itallic bold
Expand|Select|Wrap|Line Numbers
  1. private void setMenuCommand(int hWnd, int hMenu, uint nPos, int MenuItemID)
  2. {
  3.     string sLabel = "";
  4.     StringBuilder sBuffer = new StringBuilder(nBuffLen);
  5.     WinAPI.MENUITEMINFO mif = new WinAPI.MENUITEMINFO();
  6.  
  7.     mif.dwTypeData = null;// sBuffer.ToString();
  8.     try
  9.     {
  10.         // 1. You call GetMenuItemInfo here
  11.         int a = WinAPI.GetMenuItemInfo(hMenu, MenuItemID, false, ref mif);
  12.         if(a == 0)
  13.         {
  14.             //throw new Win32Exception(Marshal.GetLastWin32Error());
  15.             // 3. so the function returns here
  16.             return;
  17.         }
  18.         sBuffer = new StringBuilder((int)(mif.cch + 1));
  19.         // 2. Before you set the cbSize parameter here
  20.         mif.cbSize = (uint)Marshal.SizeOf(typeof(WinAPI.MENUITEMINFO));
  21.         mif.fMask = 0x10;
  22.         mif.dwTypeData = sBuffer.ToString();
  23.         a = WinAPI.GetMenuItemInfo(hMenu, MenuItemID, false, ref mif);
  24.         if(a != 0)
  25.         {
  26.             mif.dwTypeData = new String('\0', (int)(mif.cch + 1));
  27.             sLabel = mif.dwTypeData.Trim();
  28.             if(sLabel != "" && sLabel != "-")
  29.             {
  30.                 MenuCommand oMnuCmd = new MenuCommand();
  31.                 oMnuCmd.sLabel = sLabel;
  32.                 oMnuCmd.hMenu = hMenu;
  33.                 oMnuCmd.nPos = nPos;
  34.                 oMnuCmd.hMenuItemID = MenuItemID;
  35.                 oMnuCmd.hWin = hWnd;
  36.                 clMenuCommands.Add(sLabel, oMnuCmd);
  37.             }
  38.         }
  39.     }
  40.     catch(Exception exp)
  41.     {
  42.         throw new Exception("Error setting menu command: " + exp.Message);
  43.     }
  44. }
  45.  
Apr 28 '06 #4
DUMB!! Sheesh, I can't believe I didn't see that.
THANKS!!

It returns a 1 now and is apparantly getting the information.
When I look at dwDataType I'm getting "\0\0\0\0\0\0\0\0\".

How do I get the label on the meni item?
Apr 28 '06 #5
I've made a couple changes hoping to get some information from the label:
mif.fMask = 0x10;
mif.dwTypeData = new String('\0', (int)(mif.cch + 1));
a = WinAPI.GetMenuItemInfo(hMenu, MenuItemID, false, ref mif);

It's returning a null now.
I've read Microsoft's docs and some other stuff but frankly, I'm pretty confused as to how I'm to get the menu item's text.
Can you help?

Oh, and how did you encase the code above so it looks like code?
Apr 28 '06 #6
Banfa
9,065 Expert Mod 8TB
I did this (again in C rather than C#), note I accessed the menu by position rather than by command but that shouldn't make any difference. This worked so I guess it's just a matter of setting the MENUITEMINFO structure correctly before calling the function.

Expand|Select|Wrap|Line Numbers
  1.     MENUITEMINFO mif;
  2.     char name[50];
  3.  
  4.     mif.cbSize = sizeof(mif);
  5.     mif.fMask = MIIM_TYPE;
  6.     mif.fType = MFT_STRING;
  7.     mif.dwTypeData = name;
  8.     mif.cch = sizeof(name);
  9.  
  10.     if ( GetMenuItemInfo(GetMenu(hwnd), 0, TRUE, &mif ) )
  11.     {
  12.         // Here we have the menu item text in name
  13.     }
  14.  
Format you code by putting it between [code] and [/code] when you post it.
Apr 28 '06 #7
Hmm, I sure appreciate your help.
It's still not working for me.

dwDataType always returns null.
Also, I noticed that even though I set dwDataType = sLabel when the function returns it does not change sLabel. As you can see, mif is sent as a reference.

Expand|Select|Wrap|Line Numbers
  1. string sLabel = new String('\0', 265);
  2.             WinAPI.MENUITEMINFO mif = new WinAPI.MENUITEMINFO();
  3.  
  4.             mif.cbSize = (uint)Marshal.SizeOf(typeof(WinAPI.MENUITEMINFO));
  5.             mif.fMask = WinAPI.MIIM_TYPE;
  6.             mif.fType = WinAPI.MFT_STRING;            
  7.             mif.dwTypeData = sLabel;
  8.  
  9.             try
  10.             {
  11.                 int a = WinAPI.GetMenuItemInfo(hMenu, nPos, true, ref mif);
  12.                 if(a != 0)
  13.                 {                    
  14.                     //mif.dwTypeData = new String('\0', (int)(mif.cch + 1));
  15.                     sLabel = mif.dwTypeData;
  16.                     if(sLabel != "" && sLabel != "-")
  17.                     {
  18.                         MenuCommand oMnuCmd = new MenuCommand();
  19.                         oMnuCmd.sLabel = sLabel;
  20.                         oMnuCmd.hMenu = hMenu;
  21.                         oMnuCmd.nPos = nPos;
  22.                         oMnuCmd.hMenuItemID = MenuItemID;
  23.                         oMnuCmd.hWin = hWnd;
  24.  
  25.                         //clMenuCommands.Add(sLabel, oMnuCmd);
  26.                     }
  27.                 }
  28.             }
  29.  
Is MIIM_TYPE = 16 and MFT_STRING = 0??
Does cbSize end up equaling 45?
Also, is it possible that I'm having trouble because the menu is in an app other than mine?
Apr 28 '06 #8
I did one more thing and it looks like I've finally got it.
I called the proc twice (which is what MS says to do).
The first time to get the size of dwDataType and the second time to fill it.

Thanks very much for your help.
Apr 28 '06 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jagdeesh | last post by:
Hai Colleagues, I am using Tomcat 4.1.24 and JDK 1.4.0_03 in my winXP machine. I've transferred a set of folders(containing jsp files) into tomcat's webapps directory(to /webapps/bob ,...
2
by: Sudheer Kareem | last post by:
Dear All Please tell me how to assosiate help files with my Vb.net Project. Regards Sudheer
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
4
by: pshindle | last post by:
DB2 Team - I just downloaded and unzipped the new Fixpack 9 for DB2 ESE V8 for Windows (FP9_WR21350_ESE.exe). I then burned the unzipped Fixpack files to a CD. I proceded to install this...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
7
by: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value Display Value 123.3400 123.34...
1
by: Alpha | last post by:
I have a Window based application that shows up still running in the task manager when I close it. It reaches the "this.close" statement and then it stops at the "}" at the section of the...
7
by: Juan Romero | last post by:
Hey guys, please HELP I am going nuts with the datagrid control. I cannot get the damn control to refresh. I am using soap to get information from a web service. I have an XML writer output...
5
by: sajin | last post by:
Hi All.. We are using VB .Net 2005 for implementing an API. API needs to generate events. For this client wants us to use Windows Callback (delegate implementation). The intention of using...
12
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.