標籤:
檢查是否已經註冊的代碼為:
#region Is dll register public bool IsDllRegistered() { bool reg=true; RegistryKey rk=Registry.ClassesRoot.OpenSubKey("CLSID\\{AC53EFE4-94A7-47E6-BBFC-E9B9CF322299}"); if(rk==null) { MessageBox.Show("未註冊"); reg=false; } else { MessageBox.Show("登入"); } return reg; } #endregion
其中GUID串{AC53EFE4-94A7-47E6-BBFC-E9B9CF322299}為註冊dll的Class ID。註冊表搜尋了半天沒有找到,可通過一個名叫RegDllView的小工具進行尋找。
#region Open dwg file [DllImport(@"../ja-JP/com/DwgViewX.dll")] private static extern int DllRegisterServer(); private void OpenDwgFile(string savePath) { if(!IsDllRegistered()) { int i = DllRegisterServer(); if (i >= 0) { /* * Inoke special function to open dwg file * */ } else { /* * Register failed and open dwg with CAD * */ System.Diagnostics.Process.Start(savePath); } } } #endregion
其中@"../ja-JP/com/DwgViewX.dll"是你的dll儲存的相對路徑。注意需要引用名空間:
using System.Runtime.InteropServices;using Microsoft.Win32;
COM相關操作(C#)