Click here to Skip to main content
16,007,885 members
Home / Discussions / C#
   

C#

 
GeneralRe: Global variables in windows forms??? Pin
oakcool27-Oct-04 13:28
oakcool27-Oct-04 13:28 
AnswerRe: Global variables in windows forms??? Pin
Gary Thom26-Oct-04 4:40
Gary Thom26-Oct-04 4:40 
Generalneed help with optical character recognition Pin
tom_dx25-Oct-04 14:20
tom_dx25-Oct-04 14:20 
GeneralRe: need help with optical character recognition Pin
Christian Graus25-Oct-04 14:33
protectorChristian Graus25-Oct-04 14:33 
GeneralSendInput Pin
Keith La Force25-Oct-04 13:06
Keith La Force25-Oct-04 13:06 
GeneralRe: SendInput [EDITED] Pin
Dave Kreskowiak25-Oct-04 15:05
mveDave Kreskowiak25-Oct-04 15:05 
GeneralRe: SendInput [EDITED] Pin
Keith La Force25-Oct-04 15:55
Keith La Force25-Oct-04 15:55 
GeneralRe: SendInput Pin
Heath Stewart25-Oct-04 21:59
protectorHeath Stewart25-Oct-04 21:59 
Your struct isn't declared correctly. The mouse, keyboard, and (other) hardware input is a union. Right now you're not only mis-aligning your data but more than likely clobbering the stack. To declare unions in .NET, you must use an explicit layout like so:
[StructLayout(LayoutKind.Explicit)]
public struct INPUT
{
  [FieldOffset(0), MarshalAs(UnmanagedType.U4)] public int type;
  [FieldOffset(4)] public MOUSEINPUT mi;
  [FieldOffset(4)] public KEYBOARDINPUT ki;
  // No need to declare HARDWAREINPUT since this is a union.
  // You would for proper bit alignment if it wasn't a union;
  // otherwise, you're shorting the execution stack.
}
 
[StructLayout(LayoutKind.Sequential)]
public struct MOUSEINPUT
{
  public int dx; // Unmanaged longs are managed ints (both 32 bits)
  public int dy;
  [MarshalAs(UnmanagedType.U4)] public int mouseData;
  [MarshalAs(UnmanagedType.U4)] public int dwFlags;
  [MarshalAs(UnmanagedType.U4)] public int time;
  [MarshalAs(UnmanagedType.SysUInt)] public IntPtr dwExtraInfo;
}
 
[StructLayout(LayoutKind.Sequential)]
public struct KEYBOARDINPUT
{
  public short wVk; // Could cast managed Keys enumeration to (short)
  public short wScan;
  [MarshalAs(UnmanagedType.U4)] public int dwFlags;
  [MarshalAs(UnmanagedType.U4)] public int time;
  [MarshalAs(UnmanagedType.SysUInt)] public IntPtr dwExtraInfo;
}
The way you've declared it your keyboard data would not even be read because the SendInput function will read the data immediately after INPUT.type, which is your unused MOUSEINPUT. A union is declare in unmanaged code so that, depending on some variable (like type), data representing different structures is packed onto the stack in the same location (this may, however, affect the bit alignment of fields after the union, which is why unions are typically declared last in a structure - but not always).

This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
GeneralRe: SendInput Pin
Keith La Force26-Oct-04 12:44
Keith La Force26-Oct-04 12:44 
GeneralRe: SendInput Pin
Heath Stewart27-Oct-04 13:32
protectorHeath Stewart27-Oct-04 13:32 
GeneralOverride string toString() --HELP Pin
Member 138504925-Oct-04 12:24
Member 138504925-Oct-04 12:24 
GeneralRe: Override string toString() --HELP Pin
Christian Graus25-Oct-04 12:48
protectorChristian Graus25-Oct-04 12:48 
GeneralRe: Override string toString() --HELP Pin
leppie25-Oct-04 23:02
leppie25-Oct-04 23:02 
GeneralRe: Override string toString() --HELP Pin
Christian Graus26-Oct-04 9:13
protectorChristian Graus26-Oct-04 9:13 
GeneralRe: Override string toString() --HELP Pin
Nick Parker25-Oct-04 13:18
protectorNick Parker25-Oct-04 13:18 
Questionwhat can keep a C# form app from closing down properly? Pin
vista2725-Oct-04 11:08
vista2725-Oct-04 11:08 
AnswerRe: what can keep a C# form app from closing down properly? Pin
vista2725-Oct-04 11:17
vista2725-Oct-04 11:17 
GeneralRe: what can keep a C# form app from closing down properly? Pin
Stefan Troschuetz25-Oct-04 20:58
Stefan Troschuetz25-Oct-04 20:58 
AnswerRe: what can keep a C# form app from closing down properly? Pin
Colin Angus Mackay25-Oct-04 11:19
Colin Angus Mackay25-Oct-04 11:19 
Questiondelete row in datagrid? Pin
xiaowenjie25-Oct-04 10:36
xiaowenjie25-Oct-04 10:36 
AnswerRe: delete row in datagrid? Pin
Stanciu Vlad25-Oct-04 19:53
Stanciu Vlad25-Oct-04 19:53 
GeneralSave database password with crystal report Pin
tomc25-Oct-04 8:56
tomc25-Oct-04 8:56 
GeneralServer.Mappath equivalent in windows application Pin
sunsmile25-Oct-04 6:07
sunsmile25-Oct-04 6:07 
GeneralRe: Server.Mappath equivalent in windows application Pin
Colin Angus Mackay25-Oct-04 6:17
Colin Angus Mackay25-Oct-04 6:17 
GeneralRe: Server.Mappath equivalent in windows application Pin
sunsmile25-Oct-04 8:07
sunsmile25-Oct-04 8:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.