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;
}
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 -
mif.cbSize = sizeof(mif);
-
beforecalling GetMenuItemInfo
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);
}
}
Banfa 9,065
Expert Mod 8TB
See embedded comments in the code in itallic bold -
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
-
{
- // 1. You call GetMenuItemInfo here
-
int a = WinAPI.GetMenuItemInfo(hMenu, MenuItemID, false, ref mif);
-
if(a == 0)
-
{
-
//throw new Win32Exception(Marshal.GetLastWin32Error());
- // 3. so the function returns here
-
return;
-
}
-
sBuffer = new StringBuilder((int)(mif.cch + 1));
- // 2. Before you set the cbSize parameter here
-
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);
-
}
-
}
-
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?
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?
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. -
MENUITEMINFO mif;
-
char name[50];
-
-
mif.cbSize = sizeof(mif);
-
mif.fMask = MIIM_TYPE;
-
mif.fType = MFT_STRING;
-
mif.dwTypeData = name;
-
mif.cch = sizeof(name);
-
-
if ( GetMenuItemInfo(GetMenu(hwnd), 0, TRUE, &mif ) )
-
{
-
// Here we have the menu item text in name
-
}
-
Format you code by putting it between [code] and [/code] when you post it.
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. -
string sLabel = new String('\0', 265);
-
WinAPI.MENUITEMINFO mif = new WinAPI.MENUITEMINFO();
-
-
mif.cbSize = (uint)Marshal.SizeOf(typeof(WinAPI.MENUITEMINFO));
-
mif.fMask = WinAPI.MIIM_TYPE;
-
mif.fType = WinAPI.MFT_STRING;
-
mif.dwTypeData = sLabel;
-
-
try
-
{
-
int a = WinAPI.GetMenuItemInfo(hMenu, nPos, true, ref mif);
-
if(a != 0)
-
{
-
//mif.dwTypeData = new String('\0', (int)(mif.cch + 1));
-
sLabel = mif.dwTypeData;
-
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);
-
}
-
}
-
}
-
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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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 ,...
|
by: Sudheer Kareem |
last post by:
Dear All
Please tell me how to assosiate help files with my Vb.net
Project.
Regards
Sudheer
|
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
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
| |