将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代码:

  1. #!/usr/bin/python
  2. # filename: mklib32.py
  3. # -- Make 32bits windows module files from MinGW x86 .dll
  4. # author: cheungmine@qq.com
  5. # date: 2015-12-31
  6. # note: run in MSYS
  7. #######################################################################
  8. import os, sys, platform
  9.  
  10. import optparse, ConfigParser
  11.  
  12. APPFILE = os.path.realpath(sys.argv[0])
  13. APPNAME,_ = os.path.splitext(os.path.basename(APPFILE))
  14. APPVER = "1.0"
  15. APPHELP = "Make 32bits windows module files from MinGW .dll"
  16.  
  17. #######################################
  18. # check if file exists
  19. def file_exists(file):
  20. if file and os.path.isfile(file) and os.access(file, os.R_OK):
  21. return True
  22. else:
  23. return False
  24.  
  25. #######################################
  26. # check system is msys or cmd
  27. def check_system():
  28. # platform.uname():
  29. print " * platform:", platform.platform()
  30. print " * version:", platform.version()
  31. print " * architecture:", platform.architecture()
  32. print " * machine:", platform.machine()
  33. print " * network node:", platform.node()
  34. print " * processor:", platform.processor()
  35. if platform.architecture() != ('32bit', 'WindowsPE'):
  36. sys.exit("[ERROR] Platform not support.")
  37.  
  38. #######################################
  39. # get MSVC path environment
  40. def search_vspath():
  41. for msvc in [150, 140, 130, 120, 110, 100, 90, 80, 70, 60]:
  42. vsenv = "VS%dCOMNTOOLS" % msvc
  43. vspath = os.getenv(vsenv)
  44.  
  45. if vspath:
  46. print " * %s='%s'" % (vsenv, vspath)
  47. return vspath
  48. sys.exit("[ERROR] VS_COMNTOOLS not found")
  49.  
  50. #######################################
  51. # check dll file
  52. def validate_args(dll_file, out_path):
  53. if out_path:
  54. if not os.path.exists(out_path):
  55. sys.exit("[ERROR] Specified out path not exists: %s" % out_path)
  56. if not os.path.isdir(out_path):
  57. sys.exit("[ERROR] Specified out path not dir: %s" % out_path)
  58. else:
  59. out_path = os.path.dirname(APPFILE)
  60.  
  61. dllbases = []
  62. titles = []
  63.  
  64. if file_exists(dll_file):
  65. dllpath = os.path.dirname(dll_file)
  66. dllbase = os.path.basename(dll_file)
  67. title, ext = os.path.splitext(dllbase)
  68. if ext.lower() != ".dll":
  69. sys.exit("[ERROR] Not a .dll file: %r" % dll_file)
  70.  
  71. return (dllpath, [dllbase], [title], out_path)
  72. elif os.path.isdir(dll_file):
  73. for f in os.listdir(dll_file):
  74. pf = os.path.join(dll_file, f)
  75. if file_exists(pf):
  76. dllbase = os.path.basename(pf)
  77. title, ext = os.path.splitext(dllbase)
  78. if ext.lower() == ".dll":
  79. dllbases.append(dllbase)
  80. titles.append(title)
  81. if not len(dllbases):
  82. sys.exit("[ERROR] dll files not found in given path: %s" % dll_file)
  83. else:
  84. return (dll_file, dllbases, titles, out_path)
  85. else:
  86. sys.exit("[ERROR] Either file is missing or is not readable")
  87.  
  88. #######################################
  89. def check_results(out_path, title):
  90. out_files = []
  91.  
  92. def_file = os.path.join(out_path, title + ".def")
  93. if not file_exists(def_file):
  94. print "[ERROR] file not exists: %s" % def_file
  95. else:
  96. out_files.append(def_file)
  97.  
  98. lib_file = os.path.join(out_path, title + ".lib")
  99. if not file_exists(lib_file):
  100. print "[ERROR] file not exists: %s" % lib_file
  101. else:
  102. out_files.append(lib_file)
  103.  
  104. exp_file = os.path.join(out_path, title + ".exp")
  105. if not file_exists(exp_file):
  106. print "[ERROR] file not exists: %s" % exp_file
  107. else:
  108. out_files.append(exp_file)
  109.  
  110. return out_files
  111.  
  112. ###########################################################
  113. # Usage for MSYS:
  114. # python mklib32.py -I "C:\DEVPACK\MinGW\msys\1.0\local\win32\bin" -O "./win32"
  115. #
  116. if __name__ == "__main__":
  117. print "*" * 54
  118. print "* %-50s *" % (APPNAME + " version: " + APPVER)
  119. print "* %-50s *" % APPHELP
  120. print "*" * 54
  121.  
  122. if len(sys.argv) == 1:
  123. sys.exit("[ERROR] Input dll file not specified.")
  124.  
  125. parser = optparse.OptionParser(usage='python %prog [options]', version="%prog " + APPVER)
  126.  
  127. parser.add_option("-v", "--verbose",
  128. action="store_true", dest="verbose", default=True,
  129. help="be verbose (this is the default).")
  130.  
  131. parser.add_option("-q", "--quiet",
  132. action="store_false", dest="verbose",
  133. help="quiet (no output).")
  134.  
  135. group = optparse.OptionGroup(parser, APPNAME, APPHELP)
  136.  
  137. parser.add_option_group(group)
  138.  
  139. group.add_option("-I", "--dll-file",
  140. action="store", dest="dll_file", default=None,
  141. help="Specify input .dll file or path to export")
  142.  
  143. group.add_option("-O", "--out-path",
  144. action="store", dest="out_path", default=None,
  145. help="Specify path for output files")
  146.  
  147. (opts, args) = parser.parse_args()
  148.  
  149. check_system()
  150.  
  151. vspath = search_vspath()
  152.  
  153. (dllpath, dllbases, titles, out_path) = validate_args(opts.dll_file, os.path.realpath(opts.out_path))
  154.  
  155. print " * Input files:", dllpath
  156. for dll in dllbases:
  157. print " * :", dll
  158. print " * Output path:", out_path
  159.  
  160. out_dict = {}
  161. for i in range(0, len(dllbases)):
  162. print "-"*50
  163. dllbase = dllbases[i]
  164. title = titles[i]
  165. dll_file = os.path.join(dllpath, dllbase)
  166.  
  167. print " * Make windows module definition: %s.def" % title
  168. msyscmd = 'pexports "%s" -o > "%s.def"' % (dll_file, os.path.join(out_path, title))
  169. ret = os.system(msyscmd)
  170. if ret != 0:
  171. sys.exit("[ERROR] MSYS command: %s" % msyscmd)
  172.  
  173. print " * Make windows module import file: %s.lib" % title
  174. libcmd = 'cd "%s"&vsvars32.bat&cd "%s"&lib /def:%s.def /machine:i386 /out:%s.lib' % (vspath, out_path, title, title)
  175. ret = os.system(libcmd)
  176. if ret != 0:
  177. sys.exit("[ERROR] lib command: %s" % libcmd)
  178.  
  179. out_dict[title] = check_results(out_path, title)
  180.  
  181. print "=============== Output Files Report ==============="
  182. for title, files in out_dict.items():
  183. print "%s.dll =>" % title
  184.  
  185. for f in files:
  186. print " * ", os.path.basename(f)

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

  1. #!/usr/bin/python
  2. # filename: mklib64.py
  3. # -- Make 64bits windows module files from MinGW x64 .dll
  4. # author: cheungmine@qq.com
  5. # date: 2015-12-31
  6. # note: run in MSYS
  7. #######################################################################
  8. import os, sys, platform
  9.  
  10. import optparse, ConfigParser
  11.  
  12. APPFILE = os.path.realpath(sys.argv[0])
  13. APPNAME,_ = os.path.splitext(os.path.basename(APPFILE))
  14. APPVER = "1.0"
  15. APPHELP = "Make 64bits windows module files from MinGW .dll"
  16.  
  17. #######################################
  18. # check if file exists
  19. def file_exists(file):
  20. if file and os.path.isfile(file) and os.access(file, os.R_OK):
  21. return True
  22. else:
  23. return False
  24.  
  25. #######################################
  26. # check system is msys or cmd
  27. def check_system():
  28. # platform.uname():
  29. print " * platform:", platform.platform()
  30. print " * version:", platform.version()
  31. print " * architecture:", platform.architecture()
  32. print " * machine:", platform.machine()
  33. print " * network node:", platform.node()
  34. print " * processor:", platform.processor()
  35. if platform.architecture() != ('32bit', 'WindowsPE'):
  36. sys.exit("[ERROR] Platform not support.")
  37.  
  38. #######################################
  39. # get MSVC path environment
  40. # C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC
  41. def search_vspath():
  42. for msvc in [150, 140, 130, 120, 110, 100, 90, 80, 70, 60]:
  43. vsenv = "VS%dCOMNTOOLS" % msvc
  44. vspath = os.getenv(vsenv)
  45.  
  46. if vspath:
  47. vcbat = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(vspath))), "VC\\vcvarsall.bat")
  48. if file_exists(vcbat):
  49. vspath = os.path.dirname(vcbat)
  50. print " * %s='%s'" % (vsenv, vspath)
  51. return vspath
  52. sys.exit("[ERROR] vcvarsall.bat not found")
  53.  
  54. #######################################
  55. # check dll file
  56. def validate_args(dll_file, out_path):
  57. if out_path:
  58. if not os.path.exists(out_path):
  59. sys.exit("[ERROR] Specified out path not exists: %s" % out_path)
  60. if not os.path.isdir(out_path):
  61. sys.exit("[ERROR] Specified out path not dir: %s" % out_path)
  62. else:
  63. out_path = os.path.dirname(APPFILE)
  64.  
  65. dllbases = []
  66. titles = []
  67.  
  68. if file_exists(dll_file):
  69. dllpath = os.path.dirname(dll_file)
  70. dllbase = os.path.basename(dll_file)
  71. title, ext = os.path.splitext(dllbase)
  72. if ext.lower() != ".dll":
  73. sys.exit("[ERROR] Not a .dll file: %r" % dll_file)
  74.  
  75. return (dllpath, [dllbase], [title], out_path)
  76. elif os.path.isdir(dll_file):
  77. for f in os.listdir(dll_file):
  78. pf = os.path.join(dll_file, f)
  79. if file_exists(pf):
  80. dllbase = os.path.basename(pf)
  81. title, ext = os.path.splitext(dllbase)
  82. if ext.lower() == ".dll":
  83. dllbases.append(dllbase)
  84. titles.append(title)
  85. if not len(dllbases):
  86. sys.exit("[ERROR] dll files not found in given path: %s" % dll_file)
  87. else:
  88. return (dll_file, dllbases, titles, out_path)
  89. else:
  90. sys.exit("[ERROR] Either file is missing or is not readable")
  91.  
  92. #######################################
  93. def check_results(out_path, title):
  94. out_files = []
  95.  
  96. def_file = os.path.join(out_path, title + ".def")
  97. if not file_exists(def_file):
  98. print "[ERROR] file not exists: %s" % def_file
  99. else:
  100. out_files.append(def_file)
  101.  
  102. lib_file = os.path.join(out_path, title + ".lib")
  103. if not file_exists(lib_file):
  104. print "[ERROR] file not exists: %s" % lib_file
  105. else:
  106. out_files.append(lib_file)
  107.  
  108. exp_file = os.path.join(out_path, title + ".exp")
  109. if not file_exists(exp_file):
  110. print "[ERROR] file not exists: %s" % exp_file
  111. else:
  112. out_files.append(exp_file)
  113.  
  114. return out_files
  115.  
  116. ###########################################################
  117. # Usage for MSYS:
  118. # python mklib64.py -I "C:\DEVPACK\MinGW\msys\1.0\local\win64\bin" -O "./win64"
  119. #
  120. if __name__ == "__main__":
  121. print "*" * 54
  122. print "* %-50s *" % (APPNAME + " version: " + APPVER)
  123. print "* %-50s *" % APPHELP
  124. print "*" * 54
  125.  
  126. if len(sys.argv) == 1:
  127. sys.exit("[ERROR] Input dll file not specified.")
  128.  
  129. parser = optparse.OptionParser(usage='python %prog [options]', version="%prog " + APPVER)
  130.  
  131. parser.add_option("-v", "--verbose",
  132. action="store_true", dest="verbose", default=True,
  133. help="be verbose (this is the default).")
  134.  
  135. parser.add_option("-q", "--quiet",
  136. action="store_false", dest="verbose",
  137. help="quiet (no output).")
  138.  
  139. group = optparse.OptionGroup(parser, APPNAME, APPHELP)
  140.  
  141. parser.add_option_group(group)
  142.  
  143. group.add_option("-I", "--dll-file",
  144. action="store", dest="dll_file", default=None,
  145. help="Specify input .dll file or path to export")
  146.  
  147. group.add_option("-O", "--out-path",
  148. action="store", dest="out_path", default=None,
  149. help="Specify path for output files")
  150.  
  151. (opts, args) = parser.parse_args()
  152.  
  153. check_system()
  154.  
  155. vspath = search_vspath()
  156.  
  157. (dllpath, dllbases, titles, out_path) = validate_args(opts.dll_file, os.path.realpath(opts.out_path))
  158.  
  159. print " * Input files:", dllpath
  160. for dll in dllbases:
  161. print " * :", dll
  162. print " * Output path:", out_path
  163.  
  164. out_dict = {}
  165. for i in range(0, len(dllbases)):
  166. print "-"*50
  167. dllbase = dllbases[i]
  168. title = titles[i]
  169. dll_file = os.path.join(dllpath, dllbase)
  170.  
  171. print " * Make windows module definition: %s.def" % title
  172. msyscmd = 'pexports "%s" -o > "%s.def"' % (dll_file, os.path.join(out_path, title))
  173. ret = os.system(msyscmd)
  174. if ret != 0:
  175. sys.exit("[ERROR] MSYS command: %s" % msyscmd)
  176.  
  177. print " * Make windows module import file: %s.lib" % title
  178. libcmd = 'cd "%s"&vcvarsall.bat x86_amd64&cd "%s"&lib /def:%s.def /machine:amd64 /out:%s.lib' % (vspath, out_path, title, title)
  179. ret = os.system(libcmd)
  180. if ret != 0:
  181. sys.exit("[ERROR] lib command: %s" % libcmd)
  182.  
  183. out_dict[title] = check_results(out_path, title)
  184.  
  185. print "=============== Output Files Report ==============="
  186. for title, files in out_dict.items():
  187. print "%s.dll =>" % title
  188.  
  189. for f in files:
  190. print " * ", os.path.basename(f)

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

  1. $ python mklib64.py -I "C:\DEVPACK\MinGW\msys\1.0\local\win64\bin" -O "./win64"
  1. ******************************************************
  2. * mklib64 version: 1.0 *
  3. * Make 64bits windows module files from MinGW .dll *
  4. ******************************************************
  5. * platform: Windows-7-6.1.7601-SP1
  6. * version: 6.1.7601
  7. * architecture: ('32bit', 'WindowsPE')
  8. * machine: AMD64
  9. * network node: ThinkPad-W520
  10. * processor: Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
  11. * VS120COMNTOOLS='C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC'
  12. * Input files: C:\DEVPACK\MinGW\msys\1.0\local\win64\bin
  13. * : libeay32.dll
  14. * : ssleay32.dll
  15. * Output path: c:\DEVPACK\Workspace\temp\win64
  16. --------------------------------------------------
  17. * Make windows module definition: libeay32.def
  18. * Make windows module import file: libeay32.lib
  19. Microsoft (R) Library Manager Version 12.00.21005.1
  20. Copyright (C) Microsoft Corporation. All rights reserved.
  21.  
  22. 正在创建库 libeay32.lib 和对象 libeay32.exp
  23. --------------------------------------------------
  24. * Make windows module definition: ssleay32.def
  25. * Make windows module import file: ssleay32.lib
  26. Microsoft (R) Library Manager Version 12.00.21005.1
  27. Copyright (C) Microsoft Corporation. All rights reserved.
  28.  
  29. 正在创建库 ssleay32.lib 和对象 ssleay32.exp
  30. =============== Output Files Report ===============
  31. ssleay32.dll =>
  32. * ssleay32.def
  33. * ssleay32.lib
  34. * ssleay32.exp
  35. libeay32.dll =>
  36. * libeay32.def
  37. * libeay32.lib
  38. * 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. jquery easyui combobox 高度自适应

    data-options="required:true,editable:false,panelHeight:'auto'"  加上panelHeight:'auto'即可 列合并 ...

  2. Servlet生命周期与工作原理(转载)

    Servlet生命周期分为三个阶段: 1,初始化阶段  调用init()方法 2,响应客户请求阶段 调用service()方法 3,终止阶段 调用destroy()方法 Servlet初始化阶段: 在 ...

  3. 修改apache默认主页,重定向404页面

    yum 下载apache后默认主页 默认配置文件: vim /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/welcome.conf 跳转页面到 /var/w ...

  4. Tarjan笔记1

    Tarjan 2822 爱在心中 ** 时间限制: 1 s ** 空间限制: 128000 KB ** 题目等级 : 钻石 Diamond 题解 题目描述 Description"每个人都拥 ...

  5. zabbix API基本使用方法介绍

    前言: 以下内容根据zabbix 3.2官方文档总结:https://www.zabbix.com/documentation/3.2/manual/api 此文档只是简单的介绍API的基本使用,关于 ...

  6. JavaScript 错误处理 Throw、Try 和 Catch

    try 语句测试代码块的错误. catch 语句处理错误. throw 语句创建自定义错误. JavaScript 错误 当 JavaScript 引擎执行 JavaScript 代码时,会发生各种错 ...

  7. mongo 读分析

    分布式读 读冲突 分布式中数据库有多份数据,各份数据可能存在不一致性. mongo 只会写到primary节点上,理论上来说不会有文档冲突,也就是说数据库中的数据都以primary节点为标准. 但是有 ...

  8. 在Spring Boot框架下使用WebSocket实现消息推送

    Spring Boot的学习持续进行中.前面两篇博客我们介绍了如何使用Spring Boot容器搭建Web项目(使用Spring Boot开发Web项目)以及怎样为我们的Project添加HTTPS的 ...

  9. SpringBatch简介

    spring Batch是一个轻量级的.完善的批处理框架,旨在帮助企业建立健壮.高效的批处理应用.SpringBatch是Spring的一个子项目,使用Java语言并基于Spring框架为基础开发,使 ...

  10. 深入Java虚拟机(3)——安全

    因为网络允许多台计算机共享数据和分布式处理,所以它提供了一条入侵计算机系统的潜在途径,使得其他人可以窃取信息,改变或破坏信息,盗取计算机资源等等.为了解决由网络引起的安全问题,Java体系结构采用了一 ...