Win32 中的 SendInput 键Win64机器
我在 xp 32 位下使用 sendInput() 使用 web 服务来推送当前焦点窗口的 F5。现在在 Vista win64 下我无法获得这个结果。有些文章指出使用 4 位或 8 位的 uint 问题,但这并不能通过差异编译和 FieldOffset(4) 或 (8) 解决 vista 下的问题。其他人则表示使用此 SendInput() 方法,Vista 屏幕和窗口之间不再有交互。有人可以指出在 win32 和 win64 机器上按 F5 的解决方案吗?谢谢。
uint intReturn = 0;
NativeWIN32.INPUT structInput;
structInput = new NativeWIN32.INPUT();
structInput.type = (uint)1;
structInput.ki.wScan = 0;
structInput.ki.time = 0;
structInput.ki.dwFlags = 0;
structInput.ki.dwExtraInfo = IntPtr.Zero;
// Key down the actual key-code
structInput.ki.wVk = (ushort)NativeWIN32.VK.F5;
//vk;
intReturn = NativeWIN32.SendInput((uint)1, ref structInput, Marshal.SizeOf(structInput));
// Key up the actual key-code
structInput.ki.dwFlags = NativeWIN32.KEYEVENTF_KEYUP;
structInput.ki.wVk = (ushort)NativeWIN32.VK.F5;
//vk;
intReturn = NativeWIN32.SendInput((uint)1, ref structInput, Marshal.SizeOf(structInput));
public class NativeWIN32
{
public const ushort KEYEVENTF_KEYUP = 0x0002;
public enum VK : ushort
{
F5 = 0x74,
}
public struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public long time;
public uint dwExtraInfo;
};
[StructLayout(LayoutKind.Explicit,Size=28)]
public struct INPUT
{
[FieldOffset(0)]
public uint type;
#if x86
//32bit
[FieldOffset(4)]
#else
//64bit
[FieldOffset(8)]
#endif
public KEYBDINPUT ki;
};
[DllImport("user32.dll")]
public static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
}
I have used sendInput() under xp 32bits using webservices to push F5 of current focused windows. Now under Vista win64 i can´t obtain this result. Some articles point uint problems using 4bits or 8bits but this is not fixing the problem under vista with differential compilation and FieldOffset(4)or(8). Others speak about no more interaction beetween Vista screen and the window using this SendInput() method. Can someone point the solution to push F5 in win32 and win64 machines. Thanks.
uint intReturn = 0;
NativeWIN32.INPUT structInput;
structInput = new NativeWIN32.INPUT();
structInput.type = (uint)1;
structInput.ki.wScan = 0;
structInput.ki.time = 0;
structInput.ki.dwFlags = 0;
structInput.ki.dwExtraInfo = IntPtr.Zero;
// Key down the actual key-code
structInput.ki.wVk = (ushort)NativeWIN32.VK.F5;
//vk;
intReturn = NativeWIN32.SendInput((uint)1, ref structInput, Marshal.SizeOf(structInput));
// Key up the actual key-code
structInput.ki.dwFlags = NativeWIN32.KEYEVENTF_KEYUP;
structInput.ki.wVk = (ushort)NativeWIN32.VK.F5;
//vk;
intReturn = NativeWIN32.SendInput((uint)1, ref structInput, Marshal.SizeOf(structInput));
public class NativeWIN32
{
public const ushort KEYEVENTF_KEYUP = 0x0002;
public enum VK : ushort
{
F5 = 0x74,
}
public struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public long time;
public uint dwExtraInfo;
};
[StructLayout(LayoutKind.Explicit,Size=28)]
public struct INPUT
{
[FieldOffset(0)]
public uint type;
#if x86
//32bit
[FieldOffset(4)]
#else
//64bit
[FieldOffset(8)]
#endif
public KEYBDINPUT ki;
};
[DllImport("user32.dll")]
public static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 XP 上使用 32/64 时遇到了这个问题,这是我想出的解决方案。我不是 pInvoke 专家,因此可能有更优雅的解决方案。
根本原因似乎是两种架构之间的字长不同。这会导致从外部调用中使用的数据结构中解析出一些复杂的数据。我必须为 64 位和 32 位声明两组独立的结构和外部调用。
这是稍微有点混乱的部分。使用哪种结构取决于应用程序运行的架构。您可以针对 32 位或 64 位目标进行编译,但仍然可以在 64 位 Windows 上运行 32 位编译的应用程序。如果您希望 32 位编译的应用程序在 64 位计算机上使用 SendInput,则必须弄清楚在运行时要使用哪个结构。我通过在调用发送输入的公共方法时检查字长来做到这一点。
我还没有在 Vista 中尝试过此操作,但它可以在 32/64 位 Windows XP 和 32/64 位 Windows 7 中运行。
I ran into this problem with 32/64 on XP and this is the solution I came up with. I'm not a pInvoke expert, so there may be a more elegant solution.
The root cause seems to be that word size is different between the two architectures. This throws off where some of the complex data gets parsed out of the data structures used in the external call. I had to declare two separate sets of structures and external calls for 64 bit and 32 bit.
Here comes the slightly kludgy part. Which structure to use is determined by the architecture the app runs on. You can compile for a 32 or 64 bit target, but you can still run a 32 bit compiled app on 64 bit Windows. If you want your 32 bit compiled app to use SendInput on a 64 bit machine you have to figure out which struct to use at run time. I did this by checking the word size when my public method to send input was called.
I haven't tried this in Vista, but it works in 32/64 Windows XP and 32/64 Windows 7.
编译 32 位项目可以解决这个问题。
请参考:http:// /social.msdn.microsoft.com/Forums/en-SG/Vsexpressvb/thread/69e5529e-372b-4d70-bb94-556507a2358e
Compile your project for 32 bit would fix this.
Please refer to : http://social.msdn.microsoft.com/Forums/en-SG/Vsexpressvb/thread/69e5529e-372b-4d70-bb94-556507a2358e