본문 바로가기
C#

[C#] dll 코드로 등록 방법 (관리자 권한으로 실행해야 함)

by Jcoder 2021. 8. 31.
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UpdateProgram
{
    public static class InstallHelper
    {
        public static bool RegsvrDll()
        {
            bool result = false;
            string screen_capture_recorder_x86 = System.IO.Path.Combine(Application.StartupPath, "x86", "screen-capture-recorder.dll");
            string virtual_audio_capturer_x86 = System.IO.Path.Combine(Application.StartupPath, "x86", "virtual-audio-capturer.dll");

            string screen_capture_recorder_x64 = System.IO.Path.Combine(Application.StartupPath, "x64", "screen-capture-recorder-x64.dll");
            string virtual_audio_capturer_x64 = System.IO.Path.Combine(Application.StartupPath, "x64", "virtual-audio-capturer-x64.dll");

            string screen_capture_recorder_Fileinfo = "/s " + "\"" + (Environment.Is64BitOperatingSystem ? screen_capture_recorder_x64 : screen_capture_recorder_x86) + "\""; //'/s' : indicates regsvr32.exe to run silently. (isRegist ? "/s" : "/u /s")
            string virtual_audio_capturer_Fileinfo = "/s " + "\"" + (Environment.Is64BitOperatingSystem ? virtual_audio_capturer_x64 : virtual_audio_capturer_x86) + "\""; //'/s' : indicates regsvr32.exe to run silently. (isRegist ? "/s" : "/u /s")

            if (!System.IO.File.Exists(Environment.Is64BitOperatingSystem ? screen_capture_recorder_x64 : screen_capture_recorder_x86))
                return result;

            if (!System.IO.File.Exists(Environment.Is64BitOperatingSystem ? virtual_audio_capturer_x64 : virtual_audio_capturer_x86))
                return result;

			// classroot\clsid\ 에서 등록이 되어 있는지 확인이 가능함.
            string subKey1 = @"CLSID\{4EA69364-2C8A-4AE6-A561-56E4B5044439}"; // screen-capture-recorder
            string subKey2 = @"CLSID\{8E146464-DB61-4309-AFA1-3578E927E935}"; // virtual-audio-capturer

            // 64비트 os 확인
            if (Environment.Is64BitOperatingSystem)
            {
                using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64).OpenSubKey(subKey1))
                {
                	// 등록이 안되어 있으면 등록
                    if (ndpKey?.GetValue("") == null)
                    {
                        using (Process reg = new Process())
                        {
                            reg.StartInfo.FileName = "regsvr32.exe";
                            reg.StartInfo.Arguments = screen_capture_recorder_Fileinfo;
                            reg.StartInfo.UseShellExecute = false;
                            reg.StartInfo.CreateNoWindow = true;

                            reg.Start();
                            reg.WaitForExit();
                            reg.Close();
                        }
                    }
                }
                using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64).OpenSubKey(subKey2))
                {
                    if (ndpKey?.GetValue("") == null)
                    {
                        using (Process reg = new Process())
                        {
                            reg.StartInfo.FileName = "regsvr32.exe";
                            reg.StartInfo.Arguments = virtual_audio_capturer_Fileinfo;
                            reg.StartInfo.UseShellExecute = false;
                            reg.StartInfo.CreateNoWindow = true;

                            reg.Start();
                            reg.WaitForExit();
                            reg.Close();
                        }
                    }
                }
            }
            else
            {
                using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32).OpenSubKey(subKey1))
                {
                    if (ndpKey?.GetValue("") == null)
                    {
                        using (Process reg = new Process())
                        {
                            reg.StartInfo.FileName = "regsvr32.exe";
                            reg.StartInfo.Arguments = screen_capture_recorder_Fileinfo;
                            reg.StartInfo.UseShellExecute = false;
                            reg.StartInfo.CreateNoWindow = true;

                            reg.Start();
                            reg.WaitForExit();
                            reg.Close();
                        }
                    }
                }

                using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32).OpenSubKey(subKey2))
                {
                    if (ndpKey?.GetValue("") == null)
                    {
                        using (Process reg = new Process())
                        {
                            reg.StartInfo.FileName = "regsvr32.exe";
                            reg.StartInfo.Arguments = virtual_audio_capturer_Fileinfo;
                            reg.StartInfo.UseShellExecute = false;
                            reg.StartInfo.CreateNoWindow = true;

                            reg.Start();
                            reg.WaitForExit();
                            reg.Close();
                        }
                    }
                }
            }

            result = true;
            return true;
        }

        public static bool UnRegsvrDll()
        {
            bool result = false;
            string screen_capture_recorder_x86 = System.IO.Path.Combine(Application.StartupPath, "x86", "screen-capture-recorder.dll");
            string virtual_audio_capturer_x86 = System.IO.Path.Combine(Application.StartupPath, "x86", "virtual-audio-capturer.dll");

            string screen_capture_recorder_x64 = System.IO.Path.Combine(Application.StartupPath, "x64", "screen-capture-recorder-x64.dll");
            string virtual_audio_capturer_x64 = System.IO.Path.Combine(Application.StartupPath, "x64", "virtual-audio-capturer-x64.dll");

            string screen_capture_recorder_Fileinfo = "/u /s " + "\"" + (Environment.Is64BitOperatingSystem ? screen_capture_recorder_x64 : screen_capture_recorder_x86) + "\""; //'/s' : indicates regsvr32.exe to run silently. (isRegist ? "/s" : "/u /s")
            string virtual_audio_capturer_Fileinfo = "/u /s " + "\"" + (Environment.Is64BitOperatingSystem ? virtual_audio_capturer_x64 : virtual_audio_capturer_x86) + "\""; //'/s' : indicates regsvr32.exe to run silently. (isRegist ? "/s" : "/u /s")

            if (!System.IO.File.Exists(Environment.Is64BitOperatingSystem ? screen_capture_recorder_x64 : screen_capture_recorder_x86))
                return result;

            if (!System.IO.File.Exists(Environment.Is64BitOperatingSystem ? virtual_audio_capturer_x64 : virtual_audio_capturer_x86))
                return result;

			// classroot\clsid\ 에서 등록이 되어 있는지 확인이 가능함.
            string subKey1 = @"CLSID\{4EA69364-2C8A-4AE6-A561-56E4B5044439}"; // screen-capture-recorder
            string subKey2 = @"CLSID\{8E146464-DB61-4309-AFA1-3578E927E935}"; // virtual-audio-capturer

            // 64비트 os 확인
            if (Environment.Is64BitOperatingSystem)
            {
                using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64).OpenSubKey(subKey1))
                {
                	// 등록이 되어 있으면 삭제
                    if (ndpKey?.GetValue("") != null)
                    {
                        using (Process reg = new Process())
                        {
                            reg.StartInfo.FileName = "regsvr32.exe";
                            reg.StartInfo.Arguments = screen_capture_recorder_Fileinfo;
                            reg.StartInfo.UseShellExecute = false;
                            reg.StartInfo.CreateNoWindow = true;

                            reg.Start();
                            reg.WaitForExit();
                            reg.Close();
                        }
                    }
                }
                using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64).OpenSubKey(subKey2))
                {
                    if (ndpKey?.GetValue("") != null)
                    {
                        using (Process reg = new Process())
                        {
                            reg.StartInfo.FileName = "regsvr32.exe";
                            reg.StartInfo.Arguments = virtual_audio_capturer_Fileinfo;
                            reg.StartInfo.UseShellExecute = false;
                            reg.StartInfo.CreateNoWindow = true;

                            reg.Start();
                            reg.WaitForExit();
                            reg.Close();
                        }
                    }
                }
            }
            else
            {
                using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32).OpenSubKey(subKey1))
                {
                    if (ndpKey?.GetValue("") != null)
                    {
                        using (Process reg = new Process())
                        {
                            reg.StartInfo.FileName = "regsvr32.exe";
                            reg.StartInfo.Arguments = screen_capture_recorder_Fileinfo;
                            reg.StartInfo.UseShellExecute = false;
                            reg.StartInfo.CreateNoWindow = true;

                            reg.Start();
                            reg.WaitForExit();
                            reg.Close();
                        }
                    }
                }

                using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32).OpenSubKey(subKey2))
                {
                    if (ndpKey?.GetValue("") != null)
                    {
                        using (Process reg = new Process())
                        {
                            reg.StartInfo.FileName = "regsvr32.exe";
                            reg.StartInfo.Arguments = virtual_audio_capturer_Fileinfo;
                            reg.StartInfo.UseShellExecute = false;
                            reg.StartInfo.CreateNoWindow = true;

                            reg.Start();
                            reg.WaitForExit();
                            reg.Close();
                        }
                    }
                }
            }
            result = true;
            return true;
        }
    }
}