How to CreateObject in C#?

前端 未结 1 1052
不知归路
不知归路 2021-01-07 08:23

I want to translate the following VB6 code into C#

If optHost(0).Value Then
   Set m_oScpiAccess = New IcSCPIActiveX.IcSCPIAccess
Else
   sHost = txtHost.Tex         


        
1条回答
  •  醉梦人生
    2021-01-07 09:04

    It is not a constructor call. The sHost variable contains the name of a machine, the one that provides the out-of-process COM server. The equivalent functionality is provided by Type.GetTypeFromProgId(), using the overload that allows specifying the server name:

      var t = Type.GetTypeFromProgID("Exfo.IcSCPIActiveX.IcSCPIAccess", sHost, true);
      obj = (IcSCPIAccess)Activator.CreateInstance(t);
    

    I named it "obj", do avoid giving variables the same name as the interface type. A gazillion things can still go wrong, having the COM server properly registered both on the client and server machine and setting the DCOM security correctly is essential to make this code work. Don't try this until you are sure that the original code works properly.

    0 讨论(0)
提交回复
热议问题