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;

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

Get the currently focused control

It’s easy to get the currently foreground window (using the GetForegroundWindow API), but what about the currently focused child window?
Here’s one way.

        private static extern IntPtr GetForegroundWindow();

        static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

        static extern uint GetCurrentThreadId();

        static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

        static extern IntPtr GetFocus();

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        public static IntPtr GetCurrentFocusWindow() {
            var remoteThreadId = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);
            var currentThreadId = GetCurrentThreadId();
            // AttachThreadInput is needed so we can get the handle of a focused window in another app
            AttachThreadInput(remoteThreadId, currentThreadId, true);
            // Get the handle of a focused window
            IntPtr focussed = GetFocus();

            // Now detach since we got the focused handle
            AttachThreadInput(remoteThreadId, currentThreadId, false);

            return focussed;

Leave a comment

Detect if a mouse is attached

I had to detect if a mouse is attached. So I googled a lot, but there was no solution, so I made one. I’m not sure how good it is, but it worked in my case. Comment if you find it wrong.

bool HasMouseAttached() {
    ManagementObjectCollection collection;
    using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PointingDevice"))
        collection = searcher.Get();

    bool hasMouse = false;

    foreach (var device in collection) {                
        var name = (string) device.GetPropertyValue("Caption");
        name = name.ToLowerInvariant().Trim();

        if (name == "hid-compliant mouse") continue;
        if (name.Contains("mouse") || name.Contains("input device")) {
            hasMouse = true;

    return hasMouse;

Leave a comment

How to get the system colors using JQuery?

Using CSS2 we can get the current system colors (the OS colors). Here‘s an article for that. CSS knows about the colors, but how can we use them from JavaScript? It’s tricky, because an element first needs to be created to know the actual color. Here’s a workaround that returns the color in hex format:

function GetSystemColor(name) {
    // create an invisible element, so we can get its color
    var el = $("<div style='display: none; color: " + name + "'></div>");
    $("body").append(el); // it must be appended to make css know the actual color
    var color = el.css('color');
    // convert rgb to hex
    color = color.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    function hex(x) {
        return ("0" + parseInt(x).toString(16)).slice(-2).toUpperCase();
    return "#" + hex(color[1]) + hex(color[2]) + hex(color[3]);