2012-06-08 3 views
0

C#에서 COM으로 표시되는 ActiveX를 만들려고합니다. Windows Form입니다. DLL ActiveX로 성공적으로 구축 한 다음 VBScript 코드를 작성하여 호출했습니다. 출처는 출현했지만 그 직후 사라졌습니다.C#에서 GUI를 만들고 VBScript를 호출하십시오.

C# 코드

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Text; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
using Microsoft.Win32; 
using System.Reflection; 

namespace ActiveXTestLibrary 
{ 
    [ProgId("ActiveXTestLibrary.UserControl")] 
    [ClassInterface(ClassInterfaceType.AutoDispatch)] 
    [ComVisible(true)] 
    public partial class UserControl1 : Form 
    { 
     public UserControl1() 
     { 
      InitializeComponent(); 
     } 


     [ComVisible(true)] 
     public void Hello() 
     { 
      this.Show(); 
     } 

     private void radButton1_Click(object sender, EventArgs e) 
     { 
      this.lblResult.Text = "I am a .NET user control happily living \ninside an ActiveX container. Cheers."; 
     } 

     [ComRegisterFunction()] 
     public static void RegisterClass(string key) 
     { 
      StringBuilder sb = new StringBuilder(key); 
      sb.Replace(@"HKEY_CLASSES_ROOT\", ""); 

      RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true); 

      RegistryKey ctrl = k.CreateSubKey("Control"); 
      ctrl.Close(); 

      RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true); 
      inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase); 
      inprocServer32.Close(); 

      k.Close(); 
     } 

     [ComUnregisterFunction()] 
     public static void UnregisterClass(string key) 
     { 
      StringBuilder sb = new StringBuilder(key); 
      sb.Replace(@"HKEY_CLASSES_ROOT\", ""); 

      RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true); 

      if (k == null) 
      { 
       return; 
      } 
      k.DeleteSubKey("Control", false); 

      RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true); 

      inprocServer32.DeleteSubKey("CodeBase", false); 

      inprocServer32.Close(); 
     } 
    } 
} 

및 VBScript를 :

Sub main 
    set objTest = CreateObject("ActiveXTestLibrary.UserControl") 
    objTest.Hello 
end sub 

call main 

답변

0

당신이 호출하여 메시지 루프를 시작하는 데 필요한 양식을 표시하려면 다음 @@ 내 코드 이유를 모르겠어요 Application.Run :

[ComVisible(true)] 
public void Hello() 
{ 
    Applicaiton.Run(this); 
} 

Show() 함수는 양식과 즉각적인 나간다. Application.Run()은 양식이 닫힐 때까지 종료되지 않습니다.

+0

환상적인 남자. 그것은 작동합니다. 고마워. – fatpipp

관련 문제