Dynamic Link Library for Calling C # Based on COM component technology c ++

Source: Internet
Author: User

This document is based on the download dynamic link library interface description file provided by Alstom on-board cc. It initially completes the interface definition and some interface tests.

1. Compile the C # (vs2008) Dynamic Link Library Project

 

2. WriteDownloadInterface

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. Set the assemblyinfo. CS Interface

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. register the download Dynamic Link Library

# Regasm download. dll/TLB: Download. TLB

5. Compile the testdownload (VC ++ 6.0) project

Use VC ++ 6.0 to generate a console test application, copy download. TLB to the same directory of the project, and copy download. DLL to the DEBUG directory of the project.

// 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. program execution result 1:

B. program execution result 2:

Pomap-> showhellodialog ();

 

End

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.