Application Notification

I need to somehow notify the first instance of my app about an event from another instance. Basically, if somebody starts a new instance of my app, then the first instance should be activated. What's the best way to do this Tried to use Win32 "SendInput" func, and it didn't work as expected on WinXP x64.

Here is the code I used before on Win32, maybe somebody will see what's wrong with it (doesn't work on WinXP x64).

// --------------------------->

internal enum DeviceMessageType : uint
{
Mouse = 0,
Keyboard = 1,
}

[StructLayout(LayoutKind.Sequential, Pack = 4)]
internal struct Point
{
public int X; // LONG
public int Y; // LONG
}

[StructLayout(LayoutKind.Sequential)]
internal struct MouseMessage // MOUSEINPUT
{
public Point Position; // LONG X, LONG Y
public uint MouseData; // DWORD
public uint Flags; // DWORD
public uint TimeStamp; // DWORD
public UIntPtr ExtraInfo; // ULONG_PTR
}

internal enum KeyEvent : uint // DWORD
{
KeyUp = 0x0002
}

[StructLayout(LayoutKind.Sequential)]
internal struct KeyboardMessage // KEYBDINPUT
{
public ushort VirtualKey; // WORD
public ushort ScanCode; // WORD
public KeyEvent KeyEvent; // DWORD
public uint TimeStamp; // DWORD
public UIntPtr ExtraInfo; // ULONG_PTR
}

[StructLayout(LayoutKind.Explicit)]
internal struct DeviceMessageInfo // INPUT
{
[FieldOffset(0)]
public DeviceMessageType DeviceMessageType;

[FieldOffset(4)]
public MouseMessage MouseMessage;

[FieldOffset(4)]
public KeyboardMessage KeyboardMessage;

public DeviceMessageInfo(DeviceMessageType DeviceMessageType)
{
this.DeviceMessageType = DeviceMessageType;
this.MouseMessage = new MouseMessage();
this.KeyboardMessage = new KeyboardMessage();
}

public static readonly int SizeOf = Marshal.SizeOf(typeof(DeviceMessageInfo));
}

[DllImport("User32.dll", EntryPoint = "SendInput", ExactSpelling = true)]
public static extern uint Send(uint Count, [In] DeviceMessageInfo[] DeviceMessageInfo, int DeviceMessageInfoSize);

// <---------------------------

For more info about WinXP x64 glitch read this post: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=604188&SiteID=1

I would really appreciate it if somebody could make the code above work on WinXP x64




Answer this question

Application Notification

  • bookysmell2004

    Thanks for the reply... Before posting my code I tried using just integers instead of Point and it didn't work. The thing is WinXP x64 does process the messages becauses the SendInput func returns the correct number of messages processed. I was thinking maybe this is a bug in the OS, or something. I only use KeyboardMessage structure, and it doesn't really matter what's inside the MouseMessage, the only reason I included the MouseMessage is to dynamically adjust the DeviceMessageInfo structure size for 32/64 bit OSes; otherwise, the SendInput func wouldn't work. I really don't know what's wrong with the code.

  • ashopson

    I'm sorry, but your response doesn't make sense. How else can I use P/Invoke without managed types (not sure about which types exactly you are talking about), I mean P/Invoke was made to support Win32 with your own types. In fact my code does work on Windows XP 32 bit and WinXP 64 bit WOW. The only problem I have is running natively on WinXP 64 bit. The web site you specified provides almost identical code. I would really appreciate if you provided a working version of the code.

  • efratian

    You're right, sorry, I was thrown by "new MouseMessage()" in the constructor. Not really sure what that does, I guess it just initializes DeviceMessageInfo.MouseMessage to zeros from the boxed structure.

    I checked your declarations with WinUser.h, the only thing that's off is the use of Point in MouseMessage. Structure member alignment in 64-bit .NET might be throwing this off. Try going back to specifying these structure members in-line or use FieldOffset...



  • Jason Bolstad

    Your DeviceMessageInfo declaration doesn't look good, you can't put managed types in a structure and expect Windows to work with it. Check this webpage for the proper P/Invoke declarations. I hope they are suitable to 64-bit Windows...


  • Application Notification