电竞比分网-中国电竞赛事及体育赛事平台

分享

C#控制臺(tái)模式下調(diào)用vc開發(fā)的ocx組件

 goodwangLib 2014-05-11

1、注冊(cè)組件 regsvr32 C:\WINDOWS\system32\HX.ocx

2、用Aximp.exe工具對(duì)HX.ocx做轉(zhuǎn)換,對(duì)APT.ocx再次封裝成兩個(gè)類庫(HX.dll,AxHX.dll)

3、代碼

    

復(fù)制代碼
 1 using System;
 2 using System.Data;
 3 using System.IO;
 4 using System.Messaging;
 5 using System.Net;
 6 using System.Text;
 7 using System.Text.RegularExpressions;
 8 using System.Threading;
 9 using System.Web;
10 using System.Data.SqlClient;
11 using AXHH;//ocx控件
12 using HH;//ocx控件
   using System.Windows.Forms; 
13 namespace Ocx_test
14 {
15     public class Program
16     {
17         //設(shè)置靜態(tài)變量
18         static AXHH hh;
19         [STAThread]
20         static void Main(string[] args)
21         {
22 
23             //初始化ocx控件
24             hh = new AXHH();
25             //顯式調(diào)用該實(shí)例的CreateControl()方法才能調(diào)用其他方法否則在調(diào)用時(shí)系統(tǒng)將報(bào)AxHost.InvalidActiveXStateException異常
26             hh.CreateControl();
27 
28            //調(diào)用控件屬性及方法
29             hh.ip="127.0.0.1";
30            hh.test();
31 
32            }
33 }
復(fù)制代碼

 

4、注意,如果是多線程調(diào)用,則在  線程名.Start() 前加上,例如:

 

            線程名.SetApartmentState(System.Threading.ApartmentState.STA);//設(shè)置模式,必須在啟動(dòng)之前設(shè)置。
                // 啟動(dòng)線程將調(diào)用 ThreadStart 委托。
                線程名.Start();

 

 5、參考了

     http://www.cnblogs.com/Jerryshome/archive/2011/04/21/2023183.html

     http://blog.sina.com.cn/s/blog_4a77f0630100imn9.html

     http://tech.it168.com/d/2008-05-12/200805120947628.shtml

 

     但是反射的技術(shù)試了,不行,

    

System.Reflection.Assembly ass;
            Type comObjectName;
            object obj;
            ass = System.Reflection.Assembly.LoadFile(@"G:\test\AxHH.dll");

            comObjectName = ass.GetType("AxHH.AxHH");//必須使用名稱空間+類名稱
            obj = ass.CreateInstance("AxHH.AxHH");//這句時(shí),就出現(xiàn) '((AxHH.AxHH)(obj)).extparam1' threw an exception of type 'System.Windows.Forms.AxHost.InvalidActiveXStateException' 

 

           誰有知道,請(qǐng)告知,謝謝

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多