+3  A: 

1 you can not control the elevation mode of the process hosting your DLL. You can grant permission to the target folder or registry for everyone during the install process if you can control install process.

2 you need to sign the program with a certificate published by a certificate authority that would be trusted by the client. Visit your local certificate store (control panel->internet options, content tab, publishers) to see common certificate authorities.

3 when you get UnauthorizedAccessExceotion, throw it to the hosting exe or return an error value indicating a security problem. The caller of your DLL then decide what to do, such as displaying a security error dialog to inform the user if the program is already elevated (permission not granted by domain controller?) , or restarting the process in elevated mode using the runas command if it is not elevated.

Sheng Jiang 蒋晟
+3  A: 

I had same problems. Googling around about 2 days I found the only solution that fit my needs - start the application with administrative rights. I start the application, check if it being run as admin. And if doesn't - restart it with administrative rights.

    static void Main(string[] args)
    {
        if (NeedElevation(args) && Elevate(args))
        { // If elevastion succeeded then quit.
            return;
        }
        // your code here
    }

    public static bool Elevate(string[] args)
    {
        try
        {
            ProcessStartInfo info = Process.GetCurrentProcess().StartInfo;
            info.Verb = "runas";
            info.Arguments = NoElevateArgument;
            foreach (string arg in args)
            {
                info.Arguments += ' ' + arg;
            }
            info.FileName = Assembly.GetEntryAssembly().Location;

            Process process = new System.Diagnostics.Process();
            process.StartInfo = info;

            process.Start();
        }
        catch (Exception)
        {
            MessageBox.Show("You don't have administrative privileges thus the Automatic Application Updates cannot be started. But the rest of application is available as usually.",
                "Not enough user rights", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return false;
        }

        return true;
    }
Vasiliy Borovyak