Skip to content

Commit

Permalink
IL2CPP でも x64 なら動作するようになった。
Browse files Browse the repository at this point in the history
  • Loading branch information
kirurobo committed Jan 21, 2019
1 parent 990cf3e commit ac88d48
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
49 changes: 23 additions & 26 deletions Scripts/UniWinApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Text;
using AOT;
using System.Collections.Generic;
using System.Collections;

namespace Kirurobo
{
Expand Down Expand Up @@ -65,9 +64,11 @@ public WindowHandle(IntPtr hwnd)
Debug.Log("TITLE:" + Title);

// プロセスIDを取得
IntPtr pid;
WinApi.GetWindowThreadProcessId(hWnd, out pid);
ProcessId = pid.ToInt32();
//IntPtr pid;
ulong pid;
WinApi.GetWindowThreadProcessId(hWnd, out pid); // IL2CPP かつ x86 だとクラッシュ?
//ProcessId = pid.ToInt32();
ProcessId = (int)pid;
Debug.Log("PID: " + ProcessId);

#if ENABLE_IL2CPP
Expand Down Expand Up @@ -180,7 +181,7 @@ public override string ToString()
private static List<UniWinApi> instances = new List<UniWinApi>();

private static WinApi.EnumWindowsDelegate myEnumWindowsDelegate;
private static List<WindowHandle> windowList = new List<WindowHandle>();
private static List<IntPtr> hWndList = new List<IntPtr>();


/// <summary>
Expand Down Expand Up @@ -355,8 +356,6 @@ static public WindowHandle FindWindow()
int pid = process.Id;
Debug.Log("PID: " + pid);

IntPtr hwnd;

#if UNITY_EDITOR
// Gameビューを取得
// 参考: https://qiita.com/MARQUE/items/292c9080a686d95af1a5
Expand All @@ -365,7 +364,7 @@ static public WindowHandle FindWindow()
// Gameビューににフォーカスを与えてから、アクティブなウィンドウを取得
gameViewWin.Focus();
//System.Threading.Thread.Sleep(100);
hwnd = WinApi.GetActiveWindow();
IntPtr hwnd = WinApi.GetActiveWindow();

var rootHwnd = hwnd;
rootHwnd = WinApi.GetAncestor(hwnd, WinApi.GA_ROOT);
Expand Down Expand Up @@ -432,17 +431,9 @@ static public WindowHandle FindWindowByClass(string classname)
}

[MonoPInvokeCallback(typeof(WinApi.EnumWindowsDelegate))]
private static bool EnumWindowCallback(IntPtr hWnd, ArrayList lParam)
private static bool EnumCallback(IntPtr hWnd, IntPtr lParam)
{
StringBuilder sb = new StringBuilder(1024);
if (WinApi.IsWindow(hWnd) && WinApi.GetWindowText(hWnd, sb, sb.Capacity) != 0)
{
WindowHandle window = new WindowHandle(hWnd)
{
Title = sb.ToString()
};
windowList.Add(window);
}
hWndList.Add(hWnd);
return true;
}

Expand All @@ -451,18 +442,24 @@ private static bool EnumWindowCallback(IntPtr hWnd, ArrayList lParam)
/// </summary>
static public WindowHandle[] FindWindows()
{
if (myEnumWindowsDelegate == null)
{
myEnumWindowsDelegate = new WinApi.EnumWindowsDelegate(EnumCallback);
}

ArrayList hwndList = WinApi.GetWindows();

if (hwndList.Count < 1) return null;
hWndList.Clear();
//WinApi.EnumWindows(new WinApi.EnumWindowsDelegate(delegate (IntPtr hWnd, IntPtr lParam)
WinApi.EnumWindows(myEnumWindowsDelegate, IntPtr.Zero);

WindowHandle[] list = new WindowHandle[hwndList.Count];
int index = 0;
foreach (IntPtr hwnd in hwndList)
List<WindowHandle> windowList = new List<WindowHandle>();
foreach (IntPtr hwnd in hWndList)
{
list[index++] = new WindowHandle(hwnd);
if (WinApi.IsWindow(hwnd)) {
WindowHandle window = new WindowHandle(hwnd);
windowList.Add(window);
}
}
return list;
return windowList.ToArray();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Wrappers
Submodule Wrappers updated 1 files
+3 −16 WinApi.cs

0 comments on commit ac88d48

Please sign in to comment.