开发者

Problem with calling native API from C# on x64 Platform

I use this code to iterate through native Windows Explorer menu from C#.

this is import of GetMenuItemInfo function:

[DllImport("user32.dll",SetLastError = true)]
public static extern bool GetMenuItemInfo(IntPtr hMenu, int uItem,
                                  bool fByPosition, ref MENUITEMINFO lpmii);

with this code I iterate through menu:

int count = User32.GetMenuItemCount(menu);
var itemInfo = new MENUITEMINFO();

itemInfo.cbSize = Marshal.SizeOf(itemInfo);
itemInfo.fMask = MIIM.MIIM_ID | MIIM.MIIM_STRING;

bool previousDeleted = false;

for (int n = 0; n < count; n++)
{
   itemInfo.dwTypeData = new string(' ', 100);
   itemInfo.cch = 100;

   if(!User32.GetMenuItemInfo(menu, n, true, ref itemInfo))
   {
       Debug.Print("Error={0}",Marshal.GetLastWin32Error());
   }

...

When I call this code from x86 application - it works perfectly, But when I call this code from "Any CPU" application it is not working, and returns error code 87 (ERROR_INVALID_开发者_Python百科PARAMETER).

Any ideas?

Thanks.


You must be using the wrong definition of MENUITEMINFO. See here for the correct C# signature: PInvoke MENUITEMINFO. Look further down the page to see what you are probably using instead (e.g. hSubMenu).


I've seen a solution with 2 separate projects-wrappers for x86 and x64 platforms. There was an additional project with interfaces and a factory to resolve proper implementation at runtime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜