基於COM組件技術C++調用C#的動態鏈接庫

       本文檔根據Alstom車載CC提供的下載動態鏈接庫接口描述文件編寫,初步完成接口定義的內容,完成部分接口測試。

1、編寫C#(VS2008)動態鏈接庫工程

 

2、編寫Download接口

Download.cs

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

using System.Windows.Forms;

 

namespace download

{

    [StructLayout(LayoutKind.Sequential)]

    public struct FileStruct

    {

        [MarshalAs(UnmanagedType.BStr)]

        public string CreateTime;

        [MarshalAs(UnmanagedType.BStr)]

        public string Name;

        public int Length;

        public int bandwidth;

    }

    

    public interface IOmapService

    {     

        FileStruct[] GetFileList(string directoryPath);

        FileStruct[] GetDownloadInformation();

        FileStruct[] GetCurrentInformation();     

        Boolean Create(string login, string password, string network);

        Boolean Download(string directoryPath, DateTime BeginningDate, DateTime EndingDate);

        void close();        

        void PauseResumeDownload();

        void ShowHelloDialog();

    }

 

    public class COmapService : IOmapService

    {        

        FileStruct[] fs = new FileStruct[3];

        public COmapService()

        {

            fs[0].CreateTime = "2012-07-17 13:00";

            fs[0].Name = "File1.omap";

            fs[0].Length = 1000;

            fs[0].bandwidth = 10;

 

            fs[1].CreateTime = "2012-07-17 13:30";

            fs[1].Name = "File2.omap";

            fs[1].Length = 1020;

            fs[1].bandwidth = 20;

 

            fs[2].CreateTime = "2012-07-17 14:00";

            fs[2].Name = "File3.omap";

            fs[2].Length = 1030;

            fs[2].bandwidth = 30;   

        }

 

        public Boolean Create(string login, string password, string network)

        {

            return true;

        }

 

        public Boolean Download(string directoryPath, DateTime BeginningDate, DateTime EndingDate)

        {

            return true;

        }

 

        public void close()

        {

 

        }

 

        public void PauseResumeDownload()

        {

 

        }

 

        public FileStruct[] GetFileList(string directoryPath)

        {

            return fs;

        }

 

        public FileStruct[] GetDownloadInformation()

        {

            return fs;

        }

 

        public FileStruct[] GetCurrentInformation()

        {

            return fs;

        } 

        

        public void ShowHelloDialog()

        {

            MessageBox.Show("Hello world!");

        }

    }        

}

3、設置AssemblyInfo.cs接口

using System.Reflection;

using System.Runtime.CompilerServices;

using System.Runtime.InteropServices;

 

// General Information about an assembly is controlled through the following 

// set of attributes. Change these attribute values to modify the information

// associated with an assembly.

[assembly: AssemblyTitle("download")]

[assembly: AssemblyDescription("OMAP download service.")]

[assembly: AssemblyConfiguration("")]

[assembly: AssemblyCompany("liuxuezong")]

[assembly: AssemblyProduct("download")]

[assembly: AssemblyCopyright("Copyright © liuxuezong 2012")]

[assembly: AssemblyTrademark("")]

[assembly: AssemblyCulture("")]

 

// Setting ComVisible to false makes the types in this assembly not visible 

// to COM components.  If you need to access a type in this assembly from 

// COM, set the ComVisible attribute to true on that type.

[assembly: ComVisible(true)]

 

// The following GUID is for the ID of the typelib if this project is exposed to COM

[assembly: Guid("4d3638eb-19a0-4bb7-a7d8-df70484edb8f")]

 

// Version information for an assembly consists of the following four values:

//

//      Major Version

//      Minor Version 

//      Build Number

//      Revision

//

// You can specify all the values or you can default the Revision and Build Numbers 

// by using the '*' as shown below:

[assembly: AssemblyVersion("1.0.0.0")]

[assembly: AssemblyFileVersion("1.0.0.0")]

4、註冊download動態鏈接庫

#regasm download.dll /tlb:download.tlb

5、編寫testDownload(VC++6.0)工程

首先使用VC++6.0生成一個控制檯測試應用程序,然後把download.tlb拷貝到該工程相同目錄下,把download.dll拷貝到該工程Debug目錄下。

// TestDownload.cpp : Defines the entry point for the console application.

//

 

#include "stdafx.h"

#include <string>

#include <iostream>

using namespace std;

 

#import "download.tlb"

using namespace Download;

 

int main(int argc, char* argv[])

{

     HRESULT hr = CoInitialize(NULL);

 

     IOmapService *pOmap = NULL;

     hr = CoCreateInstance(__uuidof(COmapService), NULL, 

         CLSCTX_INPROC_SERVER, __uuidof(IOmapService), (void**)&pOmap);

 

     if (SUCCEEDED(hr))

     {

         pOmap->ShowHelloDialog();

         

         Download::FileStruct HUGEP *pFileBSTR;

         SAFEARRAY *psa = pOmap->GetFileList("D:/Omap");

         hr = SafeArrayAccessData(psa, (void HUGEP* FAR*)&pFileBSTR);

         UINT nSize = psa->rgsabound[0].cElements;

         

         printf("File List:\r\n");

 

         for (int i = 0; i < nSize; i++)

         {

              printf("CreateTime: %S\r\n", pFileBSTR[i].CreateTime);

              printf("Name: %S\n", pFileBSTR[i].Name);

              printf("Length: %d\n", pFileBSTR[i].Length);

              printf("bandwidth: %d\n", pFileBSTR[i].bandwidth);

         }        

     }

     

     CoUninitialize ();

     

     return 0;

}


A、程序執行結果1:

 B、程序執行結果2:

pOmap->ShowHelloDialog();

 

 

發佈了97 篇原創文章 · 獲贊 32 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章