C# 操作Mumu模拟器

Mumu模拟器是笔者用过算比较好用的模拟器,本文来分享笔者如何集成Mumu官方API,来快捷操作Mumu模拟器,本文只介绍核心代码,高可用性包装已经精简,可以自行穿插添加:

首先要操作Mumu模拟器就要获得模拟器的句柄:

/// <summary>
/// Mumu模拟器路径
/// </summary>
public static string ManagerPath = string.Empty;

/// <summary>
/// Mumu模拟器句柄
/// </summary>
public static IntPtr MumuMainIntptr { get; set; } = IntPtr.Zero;

/// <summary>
/// 根据Mumu模拟器关联的Apk图标获得安装地址
/// </summary>
public static  void GetMumuInstallLocation()
{
    var registryPath ="SOFTWARE\\Classes\\MuMuPlayer-12.0.apk\\DefaultIcon"; 
    var result= Registry.LocalMachine.OpenSubKey(registryPath);

    //Mumu的DefaultIcon下就是路径,这里的Key传空
    var val = result.GetValue("");
    var iconPath= val.ToString();
    reg.Close();

    var fileInfo = new FileInfo(iconPath);
    var mumuPath = fileInfo.Directory.FullName;

    ManagerPath = Path.Combine(mumuPath, "MuMuManager.exe");
}

[DllImport("user32.dll")]
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, IntPtr lParam);

[DllImport("user32.dll")]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

[DllImport("user32", SetLastError = true)]
public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);

/// <summary>
/// 获得Mumu模拟器的句柄
/// </summary>
public static void GetMumuIntptr()
{
    var isGetMumuIntptr = EnumWindows(EnumWindowsCallback, IntPtr.Zero);
}

/// <summary>
/// Mumu模拟器有两个同类名的句柄,一个是Mumu更新,这里过滤掉他
/// </summary>
private static bool EnumWindowsCallback(IntPtr hwnd, IntPtr IParam)
{
    StringBuilder className = new(256);
    _ = GetClassName(hwnd, className, 256);
    if (className.ToString() == "Qt5156QWindowIcon")
    {
        StringBuilder windowText = new(256);
        _ = GetWindowText(hwnd, windowText, 256);
        if (windowText.ToString() == "MuMu模拟器12")
        {
             MumuMainIntptr = hwnd;
             return false;
        }
        return true;
    }
}

获得Mumu模拟器的句柄之后就可以操作他了,详细命令可以自行查看Mumu官方开发者文档:

https://mumu.163.com/help/20240726/35047_1170006.html

本文只简单集成了一部分:

/// <summary>
/// 打开Mumu模拟器
/// </summary>
public static void OpenMumu()
{
    Process process = new();
    // 指定路径和参数
    process.StartInfo.FileName = ManagerPath;
    //本文集成的命令都是替换下面的Arguments,就不再重复,部分命令列在文外
    process.StartInfo.Arguments = @" api -v 0 launch_player ";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    process.StartInfo.CreateNoWindow = true;
    process.Start();
    string output = process.StandardOutput.ReadToEnd();
    process.WaitForExit();
}

部分Arguments命令:

//关闭Mumu模拟器
@" api -v 0 shutdown_player "
 
//隐藏Mumu模拟器
@" api -v 0 hide_player_window "
 
//查询Mumu模拟器状态
@" api -v 0 player_state "
 
//安装应用进Mumu模拟器
@" api -v 0 install_apk " + 你的APK包路径
 
//Mumu模拟器启动应用
@" api -v 0 launch_app " + 你的APK包包名
 
//Mumu模拟器关闭应用
@" api -v 0 close_app " + 你的APK包包名
 
//Mumu模拟器卸载应用
@" api -v 0 uninstall_app " + 你的APK包包名

这里的APK包名不是安装包名字,而是Mumu唯一识别应用的包名,详情可以看我另外一篇文章:

https://www.cnblogs.com/Poetin/p/22363656

posted @ 2026-08-10 11:01  Poetindusk  阅读(1)  评论(0)    收藏  举报