C++封裝庫

來源:互聯網
上載者:User

標籤:

1.建立項目 -> Win32項目
    選擇DLL , 勾選 空項目 , 點擊完成。

2.本常式,使用一個CPP檔案 , 及一個標頭檔。 其中標頭檔包含函式宣告,CPP檔案實現函式宣告。

3.標頭檔: SSLLib.h

#pragma once // 避免重複編繹
#ifndef __SSLLIB_H  //與#pragma once作用一致,相容設定
#define __SSLLIB_H

#ifndef __DLL_EXPORTS
#define __DLL_EXPORTS _declspec(dllimport)
#endif

//聲明函數介面
extern "C" __DLL_EXPORTS int EncodeRSAKeyFile(const char * _strPemFileName, const char * _strData , unsigned char * buffer , int length ) ;
extern "C" __DLL_EXPORTS int DecodeRSAKeyFile(const char * _strPemFileName , const char * _strData , unsigned char * buffer , int length ) ;

#endif


/////SSLLIB.h 結束符

4.建立與標頭檔查關CPP檔案 SSLLib.CPP

#include "SSLLib.h" //包含標頭檔

//函數實現
int EncodeRSAKeyFile(const char * _strPemFileName , const char * _strData , unsigned char * buffer , int length )  {
//函數實現...
}

int DecodeRSAKeyFile(const char * _strPemFileName , const char * _strData , unsigned char * buffer , int length ){
//函數實現...
}

/////////SSLLib.CPP 結束符

5.在C++檔案中使用庫檔案
#include "SSLLib.h" //引用標頭檔

6.在不同編繹模式,引用靜態檔案
#ifdef _DEBUG
#pragma comment(lib , "..\\Debug\\SSLLib.lib");
#else
#pragma comment(lib , "..\\Release\\SSLLib.lib");
#endif

接下來可直接在專案檔中使用剛剛建立的庫。
/////////C++ 引用結束符


7. C#使用


在C#專案檔中建立一個DLL檔案夾,將DLL檔案及相關的靜態庫檔案拷入。

屬性設定:
複製到輸出目錄: 如果較新則複製
產生操作:內容

C# 調用 樣本:
[System.Runtime.InteropServices.DllImportAttribute("DLL\\SSLLib.dll", EntryPoint = "EncodeRSAKeyFile")]
        public static extern int EncodeRSAKeyFile([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string _strPemFileName, byte []  _strData, byte [] buffer, int length);

需要注意的是 C# Byte 類型預設範圍 0 - 255 , C++ Char 預設類型  -128 ~ 127 , 因此在C++ 介面函式宣告時,對應無符號類型 unsigned char








 

C++封裝庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.