如何使用Rundll32来交换鼠标按钮?

9

我在这里转述另一个论坛的问题,因为我想得到相同的答案。

来自MSDN的SwapMouseButton函数

How do I pass boolean data from command prompt through rundll32.exe to a boolean type argument in a command running from user32.dll?

I'm trying to run this from CMD (the command prompt)

RUNDLL32.EXE user32.dll,SwapMouseButton *

Where the asterisk is here is where the argument should go. I already ran it without an argument and it swapped my left and right mouse buttons (seems TRUE is the default entry for the boolean argument). Now I want to undo it. However I've tried each of these for passing FALSE in the argument, and none have worked (none set my mouse buttons back to normal).

  • F
  • f
  • false
  • False
  • FALSE
  • "false"
  • "False"
  • "FALSE"
  • 0
  • -1

Please help me pass the argument as needed. Thanks in advance.


我现在不是在Windows机上,但你尝试过1或什么都没试吗? - user114600
3
rundll32 始终将参数作为字符串传递,因此在你的情况下,一个字符串指针不等于 FALSE 就等于 TRUE 传给 SwapMouseButtons。rundll32 不能神奇地知道如何解析你的参数为整数。 - wj32
6个回答

17

非常感谢提供的C#解决方案。它完美地解决了我的问题。

我进行了轻微修改,以便我可以通过单击桌面快捷方式来切换主鼠标按钮,而无需传入参数。如果我的方法能帮助其他人,那么这就是那个版本:

using System.Runtime.InteropServices;
using System;

class SwapMouse
{
    [DllImport("user32.dll")]
    public static extern Int32 SwapMouseButton(Int32 bSwap);

    static void Main(string[] args)
    {
        int rightButtonIsAlreadyPrimary = SwapMouseButton(1);
        if (rightButtonIsAlreadyPrimary != 0)
        {
            SwapMouseButton(0);  // Make the left mousebutton primary
        }
    }
}

15

在这种情况下,你不应该使用rundll32

Q164787: 信息:Windows Rundll 和 Rundll32 接口

[...] Rundll 和 Rundll32 程序不允许你调用任何 DLL 中导出的函数。例如,你不能使用这些实用程序来调用从系统 DLL 中导出的 Win32 API(应用程序编程接口)调用。这些程序只允许你调用由它们明确编写以供调用的 DLL 中的函数。


如果您已安装.NET Framework Runtime,则会带有多种语言的编译器(例如,在64位系统上,v3.5 C#编译器的路径为%SystemRoot%\Microsoft.NET\Framework64\v3.5\csc.exe)。 您可以使用C# 编写程序
using System.Runtime.InteropServices;
using System;

class SwapMouse {
    [DllImport("user32.dll")]
    public static extern Int32 SwapMouseButton(Int32 bSwap);
    static void Main(string[] args) {
        if (args.Length > 0 && String.Compare(args[0], "/u", true) == 0)
            SwapMouseButton(0);
        else
            SwapMouseButton(1);
    }
}

使用以下命令编译:

"%SystemRoot%\Microsoft.NET\Framework64\v3.5\csc" swap.cs

交换/取消交换按钮:

swap
swap /u

8

有一种黑客技巧,可以用来为左撇子用户配置鼠标布局。只需运行以下命令:

rundll32.exe user32.dll,SwapMouseButton

运行此命令以重新配置左撇子用户的鼠标布局。
我将为您解释这种行为的原因:
由rundll32.exe调用的函数具有以下函数原型:
void CALLBACK  EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow);

SwapMouseButton具有以下函数原型:

BOOL WINAPI SwapMouseButton(
  _In_ BOOL fSwap
)

SwapMouseButton使用与rundll32.exe调用的每个函数相同的调用约定(__stdcall)。

如果您使用带有附加命令行的rundll32.exe调用SwapMouseButton,则此命令行将作为lpszCmdLine传递给该函数,并将被忽略。

如果您使用rundll32调用这些函数,rundll32会自动将有效的窗口句柄(HWND)作为所调用函数的第一个参数传递。

hwnd - 窗口句柄,应用于DLL创建的任何窗口的所有者窗口

rundll32调用的SwapMouseButton函数需要将TRUE作为第一个参数才能为左撇子用户配置鼠标布局。由user32.dll传递给SwapMouseButton的有效窗口句柄不等于0,并在使用BOOL值时定义为TRUE。

您可以在此处找到关于rundll32.exe和由此可调用函数使用的原型的详细信息: INFO: Windows Rundll and Rundll32 Interface

您可以在此处找到有关SwapMouseButton函数的详细信息: SwapMouseButton function (Windows)


3

C中的短交换程序:

#include <windows.h>

int main()  
{
    if (SwapMouseButton(TRUE))
        SwapMouseButton(FALSE);

    return 0;  
}

安装Windows SDK并通过x86_x64交叉工具命令提示符编译,例如:cl swapmbtn.c "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.15063.0\um\x64\user32.lib"

main重命名为WinMain以防止控制台窗口闪烁。


0
这是一个简单的 C/C++ Windows 应用程序的代码:
// SwapMouseButtons.cpp - Inversion of mouse buttons

#include <Windows.h>
#include <iostream>
#include <WinUser.h>
#include <string.h>

void main(int nArgs, char* szArguments[])
{
    if (nArgs > 1 && (_strcmpi(szArguments[1], "FALSE") == 0 || _strcmpi(szArguments[1], "F") == 0
            || _strcmpi(szArguments[1], "RIGHT") == 0 || _strcmpi(szArguments[1], "R") == 0
            || _strcmpi(szArguments[1], "0") == 0)) { // Right handed
        SwapMouseButton(false);
        std::cout << "Mouse buttons set for Right handed (default) usage\n";
        return;
    }
    if (nArgs > 1 && (_strcmpi(szArguments[1], "TRUE") == 0 || _strcmpi(szArguments[1], "T") == 0
            || _strcmpi(szArguments[1], "LEFT") == 0 || _strcmpi(szArguments[1], "L") == 0 
            || _strcmpi(szArguments[1], "1") == 0)) { // Left handed
        SwapMouseButton(true);
        std::cout << "Mouse buttons set for Left handed (inverted) usage\n";
        return;
    }
    // No option selected => help
    std::cout << "Swap Mouse Buttons\nUsage: SwapMouseButtons x\nOptions for x:\n  - False, F, Right, R or 0 for left handed usage (default usage)\n  - True, T, Left, L or 1 for right handed usage (inverted usage)\n\n";
}

可以在这里找到编译后的版本(x86):http://micromeg.free.fr/english/progs.html


-1
我通过在开始菜单中搜索“鼠标”来解决了同样的问题。您会找到一个同名选项,请单击它。然后会出现一个对话框,在“按钮”选项卡中的“按钮配置”框中取消选中“切换主要和次要按钮”的选项。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接