将MinGW编译的openssl dll导出def和lib供MSVC使用

前面我们用mingw把openssl 编译成了动态库,得到下面2个dll文件:

libeay32.dll

ssleay32.dll

然后用下面的脚本生成Windows MSVC需要的模块定义文件(.def, .lib和.exp),

然后就可以在VC中使用了. 前提系统要安装VS.

系统要求:

Windows7+VS Studio (2008 and later)+MSYS

1) 根据32位dll生成模块定义文件的python代码:

#!/usr/bin/python
# filename: mklib32.py
#   -- Make 32bits windows module files from MinGW x86 .dll
# author: cheungmine@qq.com
# date: 2015-12-31
# note: run in MSYS
#######################################################################
import os, sys, platform

import optparse, ConfigParser

APPFILE = os.path.realpath(sys.argv[0])
APPNAME,_ = os.path.splitext(os.path.basename(APPFILE))
APPVER = "1.0"
APPHELP = "Make 32bits windows module files from MinGW .dll"

#######################################
# check if file exists
def file_exists(file):
    if file and os.path.isfile(file) and os.access(file, os.R_OK):
        return True
    else:
        return False

#######################################
# check system is msys or cmd
def check_system():
    # platform.uname():
    print " * platform:", platform.platform()
    print " * version:", platform.version()
    print " * architecture:", platform.architecture()
    print " * machine:", platform.machine()
    print " * network node:", platform.node()
    print " * processor:", platform.processor()
    if platform.architecture() != ('32bit', 'WindowsPE'):
        sys.exit("[ERROR] Platform not support.")

#######################################
# get MSVC path environment
def search_vspath():
    for msvc in [150, 140, 130, 120, 110, 100, 90, 80, 70, 60]:
        vsenv = "VS%dCOMNTOOLS" % msvc
        vspath = os.getenv(vsenv)

        if vspath:
            print " * %s='%s'" % (vsenv, vspath)
            return vspath
    sys.exit("[ERROR] VS_COMNTOOLS not found")

#######################################
# check dll file
def validate_args(dll_file, out_path):
    if out_path:
        if not os.path.exists(out_path):
            sys.exit("[ERROR] Specified out path not exists: %s" % out_path)
        if not os.path.isdir(out_path):
            sys.exit("[ERROR] Specified out path not dir: %s" % out_path)
    else:
        out_path = os.path.dirname(APPFILE)

    dllbases = []
    titles = []

    if file_exists(dll_file):
        dllpath = os.path.dirname(dll_file)
        dllbase = os.path.basename(dll_file)
        title, ext = os.path.splitext(dllbase)
        if ext.lower() != ".dll":
            sys.exit("[ERROR] Not a .dll file: %r" % dll_file)

        return (dllpath, [dllbase], [title], out_path)
    elif os.path.isdir(dll_file):
        for f in os.listdir(dll_file):
            pf = os.path.join(dll_file, f)
            if file_exists(pf):
                dllbase = os.path.basename(pf)
                title, ext = os.path.splitext(dllbase)
                if ext.lower() == ".dll":
                    dllbases.append(dllbase)
                    titles.append(title)
        if not len(dllbases):
            sys.exit("[ERROR] dll files not found in given path: %s" % dll_file)
        else:
            return (dll_file, dllbases, titles, out_path)
    else:
        sys.exit("[ERROR] Either file is missing or is not readable")

#######################################
def check_results(out_path, title):
    out_files = []

    def_file = os.path.join(out_path, title + ".def")
    if not file_exists(def_file):
        print "[ERROR] file not exists: %s" % def_file
    else:
        out_files.append(def_file)

    lib_file = os.path.join(out_path, title + ".lib")
    if not file_exists(lib_file):
        print "[ERROR] file not exists: %s" % lib_file
    else:
        out_files.append(lib_file)

    exp_file = os.path.join(out_path, title + ".exp")
    if not file_exists(exp_file):
        print "[ERROR] file not exists: %s" % exp_file
    else:
        out_files.append(exp_file)

    return out_files

###########################################################
# Usage for MSYS:
#   python mklib32.py -I "C:\DEVPACK\MinGW\msys\1.0\local\win32\bin" -O "./win32"
#
if __name__ == "__main__":
    print "*" * 54
    print "* %-50s *" % (APPNAME + " version: " + APPVER)
    print "* %-50s *" % APPHELP
    print "*" * 54

    if len(sys.argv) == 1:
        sys.exit("[ERROR] Input dll file not specified.")

    parser = optparse.OptionParser(usage='python %prog [options]', version="%prog " + APPVER)

    parser.add_option("-v", "--verbose",
        action="store_true", dest="verbose", default=True,
        help="be verbose (this is the default).")

    parser.add_option("-q", "--quiet",
        action="store_false", dest="verbose",
        help="quiet (no output).")

    group = optparse.OptionGroup(parser, APPNAME, APPHELP)

    parser.add_option_group(group)

    group.add_option("-I", "--dll-file",
        action="store", dest="dll_file", default=None,
        help="Specify input .dll file or path to export")

    group.add_option("-O", "--out-path",
        action="store", dest="out_path", default=None,
        help="Specify path for output files")

    (opts, args) = parser.parse_args()

    check_system()

    vspath = search_vspath()

    (dllpath, dllbases, titles, out_path) = validate_args(opts.dll_file, os.path.realpath(opts.out_path))

    print " * Input files:", dllpath
    for dll in dllbases:
        print " *             :", dll
    print " * Output path:", out_path

    out_dict = {}
    for i in range(0, len(dllbases)):
        print "-"*50
        dllbase = dllbases[i]
        title = titles[i]
        dll_file = os.path.join(dllpath, dllbase)

        print " * Make windows module definition: %s.def" % title
        msyscmd = 'pexports "%s" -o > "%s.def"' % (dll_file, os.path.join(out_path, title))
        ret = os.system(msyscmd)
        if ret != 0:
            sys.exit("[ERROR] MSYS command: %s" % msyscmd)

        print " * Make windows module import file: %s.lib" % title
        libcmd = 'cd "%s"&vsvars32.bat&cd "%s"&lib /def:%s.def /machine:i386 /out:%s.lib' % (vspath, out_path, title, title)
        ret = os.system(libcmd)
        if ret != 0:
            sys.exit("[ERROR] lib command: %s" % libcmd)

        out_dict[title] = check_results(out_path, title)

    print "=============== Output Files Report ==============="
    for title, files in out_dict.items():
        print "%s.dll =>" % title

        for f in files:
            print " * ", os.path.basename(f)

2) 根据64位dll生成模块定义文件的python代码:

#!/usr/bin/python
# filename: mklib64.py
#   -- Make 64bits windows module files from MinGW x64 .dll
# author: cheungmine@qq.com
# date: 2015-12-31
# note: run in MSYS
#######################################################################
import os, sys, platform

import optparse, ConfigParser

APPFILE = os.path.realpath(sys.argv[0])
APPNAME,_ = os.path.splitext(os.path.basename(APPFILE))
APPVER = "1.0"
APPHELP = "Make 64bits windows module files from MinGW .dll"

#######################################
# check if file exists
def file_exists(file):
    if file and os.path.isfile(file) and os.access(file, os.R_OK):
        return True
    else:
        return False

#######################################
# check system is msys or cmd
def check_system():
    # platform.uname():
    print " * platform:", platform.platform()
    print " * version:", platform.version()
    print " * architecture:", platform.architecture()
    print " * machine:", platform.machine()
    print " * network node:", platform.node()
    print " * processor:", platform.processor()
    if platform.architecture() != ('32bit', 'WindowsPE'):
        sys.exit("[ERROR] Platform not support.")

#######################################
# get MSVC path environment
# C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC
def search_vspath():
    for msvc in [150, 140, 130, 120, 110, 100, 90, 80, 70, 60]:
        vsenv = "VS%dCOMNTOOLS" % msvc
        vspath = os.getenv(vsenv)

        if vspath:
            vcbat = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(vspath))), "VC\\vcvarsall.bat")
            if file_exists(vcbat):
                vspath = os.path.dirname(vcbat)
                print " * %s='%s'" % (vsenv, vspath)
                return vspath
    sys.exit("[ERROR] vcvarsall.bat not found")

#######################################
# check dll file
def validate_args(dll_file, out_path):
    if out_path:
        if not os.path.exists(out_path):
            sys.exit("[ERROR] Specified out path not exists: %s" % out_path)
        if not os.path.isdir(out_path):
            sys.exit("[ERROR] Specified out path not dir: %s" % out_path)
    else:
        out_path = os.path.dirname(APPFILE)

    dllbases = []
    titles = []

    if file_exists(dll_file):
        dllpath = os.path.dirname(dll_file)
        dllbase = os.path.basename(dll_file)
        title, ext = os.path.splitext(dllbase)
        if ext.lower() != ".dll":
            sys.exit("[ERROR] Not a .dll file: %r" % dll_file)

        return (dllpath, [dllbase], [title], out_path)
    elif os.path.isdir(dll_file):
        for f in os.listdir(dll_file):
            pf = os.path.join(dll_file, f)
            if file_exists(pf):
                dllbase = os.path.basename(pf)
                title, ext = os.path.splitext(dllbase)
                if ext.lower() == ".dll":
                    dllbases.append(dllbase)
                    titles.append(title)
        if not len(dllbases):
            sys.exit("[ERROR] dll files not found in given path: %s" % dll_file)
        else:
            return (dll_file, dllbases, titles, out_path)
    else:
        sys.exit("[ERROR] Either file is missing or is not readable")

#######################################
def check_results(out_path, title):
    out_files = []

    def_file = os.path.join(out_path, title + ".def")
    if not file_exists(def_file):
        print "[ERROR] file not exists: %s" % def_file
    else:
        out_files.append(def_file)

    lib_file = os.path.join(out_path, title + ".lib")
    if not file_exists(lib_file):
        print "[ERROR] file not exists: %s" % lib_file
    else:
        out_files.append(lib_file)

    exp_file = os.path.join(out_path, title + ".exp")
    if not file_exists(exp_file):
        print "[ERROR] file not exists: %s" % exp_file
    else:
        out_files.append(exp_file)

    return out_files

###########################################################
# Usage for MSYS:
#   python mklib64.py -I "C:\DEVPACK\MinGW\msys\1.0\local\win64\bin" -O "./win64"
#
if __name__ == "__main__":
    print "*" * 54
    print "* %-50s *" % (APPNAME + " version: " + APPVER)
    print "* %-50s *" % APPHELP
    print "*" * 54

    if len(sys.argv) == 1:
        sys.exit("[ERROR] Input dll file not specified.")

    parser = optparse.OptionParser(usage='python %prog [options]', version="%prog " + APPVER)

    parser.add_option("-v", "--verbose",
        action="store_true", dest="verbose", default=True,
        help="be verbose (this is the default).")

    parser.add_option("-q", "--quiet",
        action="store_false", dest="verbose",
        help="quiet (no output).")

    group = optparse.OptionGroup(parser, APPNAME, APPHELP)

    parser.add_option_group(group)

    group.add_option("-I", "--dll-file",
        action="store", dest="dll_file", default=None,
        help="Specify input .dll file or path to export")

    group.add_option("-O", "--out-path",
        action="store", dest="out_path", default=None,
        help="Specify path for output files")

    (opts, args) = parser.parse_args()

    check_system()

    vspath = search_vspath()

    (dllpath, dllbases, titles, out_path) = validate_args(opts.dll_file, os.path.realpath(opts.out_path))

    print " * Input files:", dllpath
    for dll in dllbases:
        print " *             :", dll
    print " * Output path:", out_path

    out_dict = {}
    for i in range(0, len(dllbases)):
        print "-"*50
        dllbase = dllbases[i]
        title = titles[i]
        dll_file = os.path.join(dllpath, dllbase)

        print " * Make windows module definition: %s.def" % title
        msyscmd = 'pexports "%s" -o > "%s.def"' % (dll_file, os.path.join(out_path, title))
        ret = os.system(msyscmd)
        if ret != 0:
            sys.exit("[ERROR] MSYS command: %s" % msyscmd)

        print " * Make windows module import file: %s.lib" % title
        libcmd = 'cd "%s"&vcvarsall.bat x86_amd64&cd "%s"&lib /def:%s.def /machine:amd64 /out:%s.lib' % (vspath, out_path, title, title)
        ret = os.system(libcmd)
        if ret != 0:
            sys.exit("[ERROR] lib command: %s" % libcmd)

        out_dict[title] = check_results(out_path, title)

    print "=============== Output Files Report ==============="
    for title, files in out_dict.items():
        print "%s.dll =>" % title

        for f in files:
            print " * ", os.path.basename(f)

使用起来非常简单, 打开MSYS命令行:

 $ python mklib64.py -I "C:\DEVPACK\MinGW\msys\1.0\local\win64\bin" -O "./win64"
******************************************************
* mklib64 version: 1.0                               *
* Make 64bits windows module files from MinGW .dll   *
******************************************************
 * platform: Windows-7-6.1.7601-SP1
 * version: 6.1.7601
 * architecture: ('32bit', 'WindowsPE')
 * machine: AMD64
 * network node: ThinkPad-W520
 * processor: Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
 * VS120COMNTOOLS='C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC'
 * Input files: C:\DEVPACK\MinGW\msys\1.0\local\win64\bin
 *             : libeay32.dll
 *             : ssleay32.dll
 * Output path: c:\DEVPACK\Workspace\temp\win64
--------------------------------------------------
 * Make windows module definition: libeay32.def
 * Make windows module import file: libeay32.lib
Microsoft (R) Library Manager Version 12.00.21005.1
Copyright (C) Microsoft Corporation.  All rights reserved.

   正在创建库 libeay32.lib 和对象 libeay32.exp
--------------------------------------------------
 * Make windows module definition: ssleay32.def
 * Make windows module import file: ssleay32.lib
Microsoft (R) Library Manager Version 12.00.21005.1
Copyright (C) Microsoft Corporation.  All rights reserved.

   正在创建库 ssleay32.lib 和对象 ssleay32.exp
=============== Output Files Report ===============
ssleay32.dll =>
 *  ssleay32.def
 *  ssleay32.lib
 *  ssleay32.exp
libeay32.dll =>
 *  libeay32.def
 *  libeay32.lib
 *  libeay32.exp

64bits的文件名仍然是???32.

利用openssl管理证书及SSL编程第3部分:将MinGW编译的openssl dll导出def和lib供MSVC使用的更多相关文章

  1. 利用openssl管理证书及SSL编程第2部分:在Windows上编译 openssl

    利用openssl管理证书及SSL编程第2部分:在Windows上编译 openssl 首先mingw的环境搭建,务必遵循下文: http://blog.csdn.net/ubuntu64fan/ar ...

  2. 利用openssl管理证书及SSL编程第1部分: openssl证书管理

    利用openssl管理证书及SSL编程第1部分 参考:1) 利用openssl创建一个简单的CAhttp://www.cppblog.com/flyonok/archive/2010/10/30/13 ...

  3. 基于X.509证书和SSL协议的身份认证过程实现(OpenSSL可以自己产生证书,有TCP通过SSL进行实际安全通讯的实际编程代码)good

    上周帮一个童鞋做一个数字认证的实验,要求是编程实现一个基于X.509证书认证的过程,唉!可怜我那点薄弱的计算机网络安全的知识啊!只得恶补一下了. 首先来看看什么是X.509.所谓X.509其实是一种非 ...

  4. openssl建立证书,非常详细配置ssl+apache

    原文链接:http://blog.51yip.com/apachenginx/958.html openssl建立证书,非常详细配置ssl+apache 张映 发表于 2010-08-07 分类目录: ...

  5. 利用keytool、openssl生成证书文件

    转载请标明出处:http://blog.csdn.net/shensky711/article/details/52225073 本文出自: [HansChen的博客] 用openssl指令逐步生成各 ...

  6. Security基础(三):OpenSSL及证书服务、邮件TLS/SSL加密通信

    一.OpenSSL及证书服务 目标: 本案例要求熟悉OpenSSL工具的基本使用,完成以下任务操作: 使用OpenSSL加密/解密文件 搭建企业自有的CA服务器,为颁发数字证书提供基础环境 方案: 使 ...

  7. 如何利用OpenSSL生成证书

    此文已由作者赵斌授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 一.前言 最近为了测试内容分发网络(Content Delivery Network,简称 CDN)添加的新功 ...

  8. 使用 openssl 生成证书

    一.openssl 简介 目前最流行的 SSL 密码库工具官网:https://www.openssl.org/source/ 构成部分 密码算法库 密钥和证书封装管理功能 SSL通信API接口 用途 ...

  9. SSL编程(1) 概述

    文章来自本园马若望 SSL是TCP/IP环境上的标准的安全加密传输协议.SSL的全称是安全的 Socket层,它具有与Socket类似的客户端/服务器体制.常见的https即http+ssl,从安全的 ...

随机推荐

  1. Java多线程并发工具类

    Semaphore-信号灯机制 当我们创建一个可扩展大小的线程池,并且需要在线程池内同时让有限数目的线程并发运行时,就需要用到Semaphore(信号灯机制),Semaphore 通常用于限制可以访问 ...

  2. BZOJ#1717:[Usaco2006 Dec]Milk Patterns 产奶的模式(后缀数组+单调队列)

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的 ...

  3. UCSC下载ENCODE数据

    ENCODE数据库用于存放基因组原件,所有的测序数据(原始数据以及每一步处理后的数据以及最终的结果)都是开放下载的.假如说去官网下载的话会比较麻烦,这里可以通过UCSC的数据库下载(真的是神器啊)!下 ...

  4. Dockerfile怎么创建镜像

    编写完成 Dockerfile 之后,可以通过 docker build 命令来创建镜像. 基本的格式为 docker build [选项] 路径,该命令将读取指定路径下(包括子目录)的 Docker ...

  5. Android中软键盘弹出时底部菜单上移问题

    当在Android的layout设计里面如果输入框过多,则在输入弹出软键盘的时候,下面的输入框会有一部分被软件盘挡住,从而不能获取焦点输入. 解决办法: 方法一:在你的activity中的oncrea ...

  6. HA机制下的Hadoop配置

    [版权申明:本文系作者原创,转载请注明出处] 文章出处:http://www.cnblogs.com/sdksdk0/p/5585355.html 作者: 朱培    ID:sdksdk0 ----- ...

  7. Swift:Foundation框架中的NS前缀的由来

    可能大家对于著名的NS前缀的由来有一些疑问. 绝大多数这些NS前缀的类是NeXTSTEP操作系统中Foundation框架里的一部分,而该操作系统是OS X的基础. NeXTSTEP的程序员对它们的类 ...

  8. Python 3 智能发音

    真是十分神奇.. import win32com.client import time s = win32com.client.Dispatch("SAPI.SpVoice") s ...

  9. 用Python递归解决阿拉伯数字转为中文财务数字格式的问题(2)--打开思路的一种方法

    几天前自己写了个将阿拉伯数字转为中文财务数字的程序.用的递归,不幸的是它是树形递归. 虽然实际过程中不太可能出现金额数字大到让Python递归栈溢出,但是始终是一块心病,这玩意终究在理论上是受限制的. ...

  10. Dynamics CRM 删除字段时检测到有组件类型为查看的依赖组件而无法删除问题

    今天在删除一个字段的时候报如下截图错误,点开详细信息会看到是一个快速查找视图,但却在视图列中没有找到我要删的那个字段,然后回过头来又看到组件类型是查看,这是啥类型?有点摸不着头脑了. 最后想到是不是查 ...