基於COM組件技術C++調用C#的動態連結程式庫

來源:互聯網
上載者:User

       本文檔根據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();

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.