zoukankan      html  css  js  c++  java
  • C++封装库

    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








  • 相关阅读:
    一个老鸟发的公司内部整理的 Android 学习路线图
    Android studio button 按钮 四种绑定事件的方法
    Android 运行时权限处理(from jianshu)
    Android 运行时权限处理
    Android studio 快捷键
    Android Toast:是一个类,主要管理消息的提示
    Android开发使用软件
    重装系统,打开VS进行程序调试运行的时候 Unable to find manifest signing certificate in the certificate store
    dev gridview指定单元格cell获取坐标
    IOS 杂笔-3 (property参数)
  • 原文地址:https://www.cnblogs.com/a_bu/p/4398222.html
Copyright © 2011-2022 走看看