コード例 #1
0
            public WindowHandle(IntPtr hwnd)
            {
                hWnd = hwnd;
                if (hWnd == IntPtr.Zero)
                {
                    return;
                }
                //Debug.Log("HWND:" + hWnd);

                // StringBufferの大きさ
                const int len = 1024;

                // クラス名を取得
                StringBuilder sbClass = new StringBuilder(len);

                if (WinApi.GetClassName(hWnd, sbClass, len) > 0)
                {
                    ClassName = sbClass.ToString();
                }
                //Debug.Log("CLASS:" + ClassName);

                // ウィンドウタイトルを取得
                StringBuilder sbTitle = new StringBuilder(len);

                if (WinApi.GetWindowText(hWnd, sbTitle, len) > 0)
                {
                    Title = sbTitle.ToString();
                }
                //Debug.Log("TITLE:" + Title);

                // プロセスIDを取得
                ulong pid = 0;

                WinApi.GetWindowThreadProcessId(hWnd, out pid);     // IL2CPP かつ x86 だとクラッシュ?
                ProcessId = (int)pid;
                //Debug.Log("THREAD PID: " + ProcessId);

#if ENABLE_IL2CPP
                // プロセス名を取得
                // Unity 2018.2.8f1 にて IL2CPP の場合、プロセス名取得時にクラッシュする。
                //   .NET 4 の場合は即クラッシュした。
                //   .NET 3.5 のときは即ではないがやはりクラッシュした。
                ProcessName = "";
#else
                // プロセス名を取得
                // GetProcessById(PID) で指定PIDが存在しない例外になる場合があるため try {} を使用
                try
                {
                    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(ProcessId);
                    ProcessName = p.ProcessName;
                }
                catch
                {
                    ProcessName = "";
                    Debug.Log("Getting process name by PID " + ProcessId + " failed");
                }
#endif
                //Debug.Log("NAME: " + ProcessName);
            }