본문 바로가기

프로그램 경험/.Net

[C#] Regsvr32와 같은 효과 내기

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
        private extern static int LoadLibrary(string librayName);

        [DllImport("kernel32.dll", EntryPoint = "GetProcAddress", CharSet = CharSet.Ansi)]
        private extern static IntPtr GetProcAddress(int hwnd, string procedureName);

        [DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
        private extern static bool FreeLibrary(int hModule);

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        private delegate void _DLLPROC();


        private void button1_Click(object sender, EventArgs e)
        {
            if (File.Exists(Application.StartupPath + @"\xxx.dll"))
            {
                int hMod = LoadLibrary(Application.StartupPath + @"\xxx.dll");

                if (hMod != 0)
                {
                    _DLLPROC dllProc;
                    IntPtr pfnDllProc = GetProcAddress(hMod, "DllRegisterServer");

                    if (pfnDllProc != IntPtr.Zero)
                    {
                        dllProc = (_DLLPROC)Marshal.GetDelegateForFunctionPointer(pfnDllProc, typeof(_DLLPROC));
                        dllProc();
                    }
                }
            }       
        }
    }

}