程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 在C#代碼中調用C++和VB編寫的組件(4)

在C#代碼中調用C++和VB編寫的組件(4)

編輯:關於C語言

18.2.4 C#中的客戶程序

程序清單18-6:

//ClIEntCs.cs
using System;
using DICTVC;
using DICTCS;
using DICTVB;
class Test
{
 public static void Main()
 {
  Console.Write("Input the language name of library to load:");
  string language=Console.ReadLine();
//調用C#中的組件
  DICTCS.DictionaryComponent CsDict=new 
     DICTCS.DictionaryComponent();
  Console.WriteLine("Dictionary from C# DictionaryComponent");
  if(CsDict.LoadLibrary(language)==false){
    Console.WriteLine("Library not found in C# DictionaryComponent!");
    }
    else{
         Console.WriteLine("Library load successfully in C# DictionaryComponent");
    }
  //調用C++中的組件
    DICTVC.DictionaryComponent VcDict=new
    DICTVC.DictionaryComponent();
    Console.WriteLine("\nDictionary from C++
            DictionaryComponent");
    if(VcDict.LoadLibrary(language)==false){
    Console.WriteLIne("Library not found in C++ DictionaryComponent!");
    }
    else{
         Console.WriteLine("Library Load successfully in C++ DictionaryComponent");
    }
  //調用VB中的組件
    DICTVB.DictionaryComponent VbDict=new
    DICTVB.DictionaryComponent();
    Console.WriteLine("\n Dictionary from VB
             DictionaryComponent");
    if(VbDict.LoadLibrary(language)==false){
       Console.WriteLine("Library not found in VB DictionaryComponent!");
    }
    else{
    Console.WriteLine("Library load successfully in VB DictionaryComponent");
    }
    //顯示各個字典中的語言信息
    Console.WriteLine("The language of library in
       C# dictionary is:{0}",CsDict.CurrentLibrary);
    Console.WriteLine("The language of library in
       C++ dictionary is:{0}",VcDict.CurrentLibrary);
    Console.WriteLine("The language of library in
       VB dictionary is:{0}",VbDict.CurrentLibrary);
    //卸載字典語言庫
    CsDict.FreeLibrary();
    VcDict.FreeLibrary();
    VbDict.FreeLibrary();
  }
}

在上面的例子中,我們引用了名字空間的全名,因為定義的各個組件名字均為DictionaryComponent。如果不使用名字空間的全名,對組件的使用將產生混淆。如果你在聲明組件時采用下面的語法,我們就不需要引入全名:

using VC DictionaryComponent=CompVC.DictionaryComponent;
using Cs DictionaryComponent=CompCs.Dictionarycomponent;
using VB DictionaryComponent=CompVB.DictionaryComponent;

事實上,如果我們在C++中調用C#或者VB編寫的組件,除了一些范圍的指定外,客戶程序的代碼基本上沒有什麼兩樣。

編譯該C#客戶程序也十分簡單,現在讓我們把應用程序的可執行文件同樣輸出到當前目錄的“..\Bin”子目錄,這只需要使用/reference編譯選項就可以了:

csc /reference:..\Bin\DICTCS.dll;..\Bin\DICTVB.dll;
..\Bin\DICTVC.dll/out:..\Bin\ClientCS.exe ClIEntCS.cs

下面讓我們檢驗一下程序的運行效果,在屏幕上鍵入ClIEnt.exe命令,並按提示輸入語言的名稱:

Input the language name of library to load:

如果我們輸入“English”,程序運行的結果將是:

Dictionary from C# DictionaryComponent

library load successfully in C# DictionaryComponent

Dictionary from VC DictionaryComponent

Library load successfully in VC DictionaryComponent

Dictionary from VB DictionaryComponent

Library load successfully in VB DictionaryComponent

The language of library in C# dictionary is:English

The language of library in VB dictionary is:English

The language of library in VB dictionary is:English

再運行一次程序,這一次我們輸入“Japanese”,程序運行的結果將是:

Dictionary from C# DictionaryComponent

Library not found in C# DictionaryComponent!

Dictionary from VC DictionaryComponent

Library not found in VC DictionaryComponent!

Dictionary from VB DictionaryComponent

Library load successfully in VB Dictionarycomponent

The language of library in C# is:

The language of library in VC is:

The language of library in VB is:Japanese

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved