Z
Z
Zakharov Alexander2017-10-18 14:35:21
C++ / C#
Zakharov Alexander, 2017-10-18 14:35:21

How to programmatically call the dialog for using the default program for a given protocol (http/https)?

Hello.
Need to programmatically call a dialog in C# to change the default browser? There is a suspicion that this is done by the SHChangeNotify function . It seems like it is called without errors: , but no dialog appears. I see that the https protocol does not appear anywhere. I don’t really understand where to substitute it, and is the function correct? Google Chrome, for example, can do this:
59e73a8359a1e272618012.png
59e73c2befb79254157412.png
59e73f860c595398269533.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zakharov Alexander, 2017-10-18
@AlexZaharow

Found a solution. Based on code: https://github.com/joewalker/devtools-window/blob/...

public static void NotifySystemOfNewRegistration() {

            tagOPENASINFO info = new tagOPENASINFO();
            info.cszFile = "https";
            info.cszClass = null;
            info.oaifInFlags = tagOPEN_AS_INFO_FLAGS.OAIF_FORCE_REGISTRATION |
                               tagOPEN_AS_INFO_FLAGS.OAIF_URL_PROTOCOL |
                               tagOPEN_AS_INFO_FLAGS.OAIF_REGISTER_EXT;
            SHOpenWithDialog(IntPtr.Zero, ref info);
        }

        // http://msdn.microsoft.com/en-us/library/windows/desktop/bb773363(v=vs.85).aspx 
        private struct tagOPENASINFO {
            [MarshalAs(UnmanagedType.LPWStr)]
            public string cszFile;

            [MarshalAs(UnmanagedType.LPWStr)]
            public string cszClass;

            [MarshalAs(UnmanagedType.I4)]
            public tagOPEN_AS_INFO_FLAGS oaifInFlags;
        }

        [Flags]
        private enum tagOPEN_AS_INFO_FLAGS {
            OAIF_ALLOW_REGISTRATION = 0x00000001,   // Show "Always" checkbox
            OAIF_REGISTER_EXT = 0x00000002,   // Perform registration when user hits OK
            OAIF_EXEC = 0x00000004,   // Exec file after registering
            OAIF_FORCE_REGISTRATION = 0x00000008,   // Force the checkbox to be registration
            OAIF_HIDE_REGISTRATION = 0x00000020,   // Vista+: Hide the "always use this file" checkbox
            OAIF_URL_PROTOCOL = 0x00000040,   // Vista+: cszFile is actually a URI scheme; show handlers for that scheme
            OAIF_FILE_IS_URI = 0x00000080    // Win8+: The location pointed to by the pcszFile parameter is given as a URI
        }

        // http://www.pinvoke.net/default.aspx/shell32.SHOpenWithDialog
        [DllImport("shell32.dll", EntryPoint = "SHOpenWithDialog", CharSet = CharSet.Unicode)]
        private static extern int SHOpenWithDialog(IntPtr hWndParent, ref tagOPENASINFO oOAI);

Result: A dialog box is displayed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question