blez's : blog

code / design / music and more


Leave a comment

Get exe path from process

        private string GetProcessPath(Process process) {
            using (var searcher = new ManagementObjectSearcher("SELECT ProcessId, ExecutablePath FROM Win32_Process WHERE ProcessId = " + process.Id.ToString())) {
                using (var results = searcher.Get()) {
                    var mo = results.Cast<ManagementObject>().FirstOrDefault();
                    if (mo != null) return (string) mo["ExecutablePath"];
                }
            }

            return null;
        }
Advertisements


Leave a comment

How to use HTML5 audio in the WebBrowser control?

No matter how new version of Windows you use, the WebBrowser control always uses older version of IE. That’s pretty awful solution since we all know how good IE is. Using meta tags it is possible to say to IE to use a specific version. The following example illustrates how to make a simple html5 audio player playing mp3 directly from some url.

// the obvious..
// webBrowser1 is a WebBrowser control
// someUrl is an url to a mp3 file

var source = "<html><head><meta http-equiv='X-UA-Compatible' content='IE=9'></head><body><audio id='player' style='position: absolute; left: 0; top: 0; width: 100%' controls autoplay='autoplay'><source src='{0}' type='audio/mpeg'>You need a newer version of IE to play this track.</audio></body></html>";
webBrowser1.DocumentText = string.Format(source, someUrl);


Leave a comment

How to detect a Metro process?

I was searching for a way to detect if a process is IE 10 metro, not IE 10 desktop, then I saw that metro apps have no windows. I don’t know how Spy++ enums IE metro’s subwindows, but mine code doesn’t. That’s somehow a good thing, cause I can use that for detecting a metro app. I tried it with x86/x64 and console apps. Seems to work fine and detects only metro apps. Write if there’s a bug or a smarter solution.

        public static bool IsMetroApp(Process process) {            
            foreach (ProcessThread threadInfo in process.Threads) {
                var windows = GetWindowHandlesForThread(threadInfo.Id);
                if (windows != null && windows.Length > 0) return false;
            }

            return true;
        }

        private static IntPtr[] GetWindowHandlesForThread(int threadHandle) {
            results.Clear();
            EnumWindows(WindowEnum, threadHandle);
            return results.ToArray();
        }

        private delegate int EnumWindowsProc(IntPtr hwnd, int lParam);

        [DllImport("user32.Dll")]
        private static extern int EnumWindows(EnumWindowsProc x, int y);
        [DllImport("user32")]
        private static extern bool EnumChildWindows(IntPtr window, EnumWindowsProc callback, int lParam);
        [DllImport("user32.dll")]
        public static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);

        private static List<IntPtr> results = new List<IntPtr>();

        private static int WindowEnum(IntPtr hWnd, int lParam) {
            int processID = 0;
            int threadID = GetWindowThreadProcessId(hWnd, out processID);
            if (threadID == lParam) {
                results.Add(hWnd);
                EnumChildWindows(hWnd, WindowEnum, threadID);
            }

            return 1;
        }