c calls the ocx control example written by vc

  • 2020-06-15 10:07:03
  • OfStack

How do you invoke OCX controls written by VC in c#

Two key steps: Import the ocx control and create the instance.

1 Registration for ocx:


regsvr32.exe alasunsmscon.ocx

2 In the CMD window:


AxImp.exe alasunsmscon.ocx

Generates two files: alasun dll, Axalasun. dll

3 Reference Axalasun.dll in the project

4 calls


Axalasun.Axalasunsms sms = new Axalasun.Axalasunsms();
sms.CreateControl();

You can use it

There is also an example of c# console mode calling the ocx component developed with vc

1. Register component regsvr32 C:\WINDOWS\system32\ ES45en.ocx

2. Use Aximp. exe tool to convert ES51en. ocx and encapsulate APT. ocx into two class libraries (ES55en. dll, ES57en. dll)

3, code,


using System;
using System.Data;
using System.IO;
using System.Messaging;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Data.SqlClient;
using AXHH;//ocx controls 
using HH;//ocx controls 
   using System.Windows.Forms;  
namespace Ocx_test
{
    public class Program
    {
        // Set the static variable 
        static AXHH hh;
        [STAThread]
        static void Main(string[] args)
        {
            // Initialize the ocx controls 
            hh = new AXHH();
            // Explicitly calling the instance CreateControl() Method can only call other methods otherwise the system will report when called AxHost.InvalidActiveXStateException abnormal 
            hh.CreateControl();
           // Invokes control properties and methods 
            hh.ip="127.0.0.1";
           hh.test();
           }
}

4, note that if it is a multi-threaded call, then before the thread name.Start (), such as:


 The thread of .SetApartmentState(System.Threading.ApartmentState.STA);// Set mode, which must be set before startup. 
 //  Starting the thread will be called  ThreadStart  Delegate. 
 The thread of .Start();


Related articles: