现在的位置: 首页 > 综合 > 正文

vc程序报错- 调用动态库并传文件指针操作

2013年05月30日 ⁄ 综合 ⁄ 共 8247字 ⁄ 字号 评论关闭

vc程序报错- 调用动态库并传文件指针操作(WinXP)

http://blog.chinaunix.net/u/31179/showart_1014063.html

**********************************************

** 文章名称:vc程序报错- 调用动态库并传文件指针操作(WinXP)
** 版权信息:可以任意转载,但要注明出处及作者信息
** 作   者: 隗公万
** 时   间: 2008.6.27

***********************************************/

一、自己有个程序是在外部调用动态库中的接口,接口参数中有个文件描述符,然后,动态库需要使用这个文件描述符。这样的程序在HP-UNIX、solaris、linux系统都是可以正常使用的,只是到windows操作系统上面运行的时候就报错。说文件指针不能共享。

二、直接在编译动态库和编译可执行程序中增加编译选项/MD (Multithreaded DLL),这样就可以互相共享了。

三、以下是一些网上找到的关于vc中编译参数设置的比较:
/////////////////////////////////////////////////////////////////////////////////
1.基于对话框(/单文档/多文档)的MFC程序
预编译头文件stdafx.h:
#define VC_EXTRALEAN     // Exclude rarely-used stuff from Windows headers

// afxwin.h中声明了MFC封装的一些很基本的类(CWnd、CView、CButton、CDC等)
#include <afxwin.h>         // MFC core and standard components
// afxext.h中声明了MFC的一些扩展类(CBitmapButton、CControlBar、CSplitterWnd等)
#include <afxext.h>         // MFC extensions
// afxdisp.h中声明了Ole的几个类(COleException、COleVariant等)
#include <afxdisp.h>        // MFC Automation classes
// afxdtctl.h中声明了几个控件类(CImageList、CMonthCalCtrl、CDateTimeCtrl等)
#include <afxdtctl.h>       // MFC support for Internet Explorer 4 Common Controls

#ifndef _AFX_NO_AFXCMN_SUPPORT
// afxcmn.h中声明了MFC常用的一些控件类(CListCtrl、CProgressCtrl、CToolTipCtrl等)
#include <afxcmn.h>            // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

(1.1)Use MFC in a Shared DLL
Debug版本:
预定义:WIN32,_DEBUG,_WINDOWS,_AFXDLL,_MBCS
编译参数:/nologo /MDd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_AFXDLL" /D "_MBCS" /FR"Debug/" /Fp"Debug/ExeDlg.pch" /Yu"stdafx.h" /Fo"Debug/" /Fd"Debug/" /FD /GZ   /c
连接参数:/nologo /subsystem:windows /incremental:yes /pdb:"Debug/ExeDlg.pdb" /debug /machine:I386 /out:"Debug/ExeDlg.exe" /pdbtype:sept

Release版本:
预定义:与Debug版本相比,将_DEBUG替换成了NDEBUG
编译参数:/nologo /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_AFXDLL" /D "_MBCS" /Fp"Release/ExeDlg.pch" /Yu"stdafx.h" /Fo"Release/" /Fd"Release/" /FD /c
连接参数:/nologo /subsystem:windows /incremental:no /pdb:"Release/ExeDlg.pdb" /machine:I386 /out:"Release/ExeDlg.exe"

(1.2)Use MFC in a Static Library
Debug版本:
预定义:与(1.1)相比,少了_AFXDLL
编译参数:将/MDd(使用Run-time library: Debug Multithreaded DLL)换成了/MTd(使用Run-time library: Debug Multithreaded)
连接参数:与(1.1)相同

Release版本:
编译参数/MD(使用Run-time library: Multithreaded DLL)换成了MT(使用Run-time library: Multithreaded)

***备注:以上编译/连接参数含义如下(更多的,请参考Msdn):
/nologo:抑制信息在编译或者连接时在Output Window输出;   /MD:运行时库使用MSVCRT.DLL;   /W3:编译时显示为Warning的级别为3;   /Gm:Enable Minimal Rebuild,一种减少重编译的选项;  /GX:Enable Exception Handling;     /ZI:设置Debug信息保存的数据库文件.PDB中;    /Od:Disable代码优化;     /FR:生成.SBR文件,包含有符号信息;       /Fp:命名生成的预编译头文件;     /Yu:指定预编译头文件。

/////////////////////////////////////////////////////////////////////////////////
2.MFC DLL项目
预编译头文件stdafx.h:
#define VC_EXTRALEAN        // Exclude rarely-used stuff from Windows headers

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions

#ifndef _AFX_NO_OLE_SUPPORT
#include <afxole.h>         // MFC OLE classes
#include <afxodlgs.h>       // MFC OLE dialog classes
#include <afxdisp.h>        // MFC Automation classes
#endif // _AFX_NO_OLE_SUPPORT

#ifndef _AFX_NO_DB_SUPPORT
#include <afxdb.h>            // MFC ODBC database classes
#endif // _AFX_NO_DB_SUPPORT

#ifndef _AFX_NO_DAO_SUPPORT
#include <afxdao.h>            // MFC DAO database classes
#endif // _AFX_NO_DAO_SUPPORT

#include <afxdtctl.h>        // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>            // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
增加了两个OLE的头文件和两个数据库的头文件

(2.1) Use MFC in a Shared DLL
Debug版本:
预定义:WIN32,_DEBUG,_WINDOWS,_WINDLL,_AFXDLL,_MBCS,_USRDLL,与MFC Exe程序相比,增加了_WINDLL和_USRDLL
编译参数:与MFC Exe没有太大区别
连接参数:/nologo /subsystem:windows /dll /incremental:yes /pdb:"Debug/MFCDll.pdb" /debug /machine:I386 /def:"./MFCDll.def" /out:"Debug/MFCDll.dll" /implib:"Debug/MFCDll.lib" /pdbtype:sept
与MFC Exe相比,增加了/dll定义,以及/def:"./MFCDll.def"和/implib:"Debug/MFCDll.lib"。注意:其中MFCDll是测试的项目名字,非标准DLL名字。
从项目的文件上看,这个项目比MFC Exe多产生一个.def的文件用于定义导出函数。

Release版本与Debug版本的区别类似项目1中的比较(上了_AFXDLL定义)。

(2.2) Use MFC in a Static DLL
与(2.1)的区别,主要在使用的Run-time library类型上,与项目1中的比较。

/////////////////////////////////////////////////////////////////////////////////
3.MFC Extension DLL项目
预编译头文件stdafx.h内容与项目2相同。

(3.1) Use MFC in a Shared DLL
Debug版本:
预定义:WIN32,_DEBUG,_WINDOWS,_MBCS,_AFXEXT,_WINDLL,_AFXDLL,与项目2相比,将_USRDLL换成了_AFXEXT。
编译参数:与上述项目没有太大区别
连接参数:与MFC DLL项目相似

Release版本与Debug版本的区别类似项目1中的比较(上了_AFXDLL定义)。

(3.2) Use MFC in a Static DLL
类似以上项目的比较。

(注:以下项目均以Debug版本论述。)
/////////////////////////////////////////////////////////////////////////////////
4.Win32 DLL项目
预编译头文件stdafx.h:
#define WIN32_LEAN_AND_MEAN        // Exclude rarely-used stuff from Windows headers

#include <windows.h>

出现项目入口函数DllMain的实现。

(4.1) Not Using MFC
预定义:WIN32,_DEBUG,_WINDOWS,_MBCS,_USRDLL,WIN32DLLDEMO_EXPORTS,与项目2(MFC DLL)相比,少了_WINDLL,_AFXDLL,而仅保留了_USRDLL。另外,WIN32DLLDEMO_EXPORTS自定义的导出宏。
编译参数:没有太大区别。
连接参数:kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /incremental:yes /pdb:"Debug/Win32DllDemo.pdb" /debug /machine:I386 /out:"Debug/Win32DllDemo.dll" /implib:"Debug/Win32DllDemo.lib" /pdbtype:sept
与MFC DLL项目相比,多了很多库的连接,少了/subsystem:windows的定义。

(4.2) Use MFC in a Shared DLL
预定义:与(4.1)相比,增加了_WINDLL,_AFXDLL的定义
编译参数:没有太大区别。
连接参数:/nologo /dll /incremental:yes /pdb:"Debug/Win32DllDemo.pdb" /debug /machine:I386 /out:"Debug/Win32DllDemo.dll" /implib:"Debug/Win32DllDemo.lib" /pdbtype:sept
可以看出,(4.1) 里连接的很多库消失了,这时,项目4变成了类似于项目2中的设置。
*** 但是,编程时需要注意,项目4的stdafx.h仅包含了<windows.h>,此时如果要用MFC的类,需要自己加入MFC的几个头文件(<afxwin.h>、<afxext.h>等),并且将#include <windows.h>和DllMain入口函数注释掉!

(4.3) Use MFC in a Static DLL
使用MFC DLL的方式Shared和Static之间的区别与上述项目类似,不再做比较。

/////////////////////////////////////////////////////////////////////////////////
5.Win32 Static Library项目
预编译头文件stdafx.h(可能没有这个文件):
如果不使用MFC的话,仅仅#define WIN32_LEAN_AND_MEAN,而如果使用MFC,内容如下:
#define VC_EXTRALEAN        // Exclude rarely-used stuff from Windows headers

#include <afx.h>
#include <afxwin.h>

(5.1) Not Using MFC    
预定义:WIN32,_DEBUG,_MBCS,_LIB,新出现了符号_LIB
编译参数:/nologo /MLd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /Fp"Debug/W32StaPrehead.pch" /Yu"stdafx.h" /Fo"Debug/" /Fd"Debug/" /FD /GZ  /c
注意到使用的Run-time library参数为/MLd。
库参数:这个项目没有Link设置页,代替的是Library页,参数如下:/nologo /out:"Debug/W32StaPrehead.lib"

(5.2) Use MFC in a Shared DLL
预定义:WIN32,_DEBUG,_WINDOWS,_MBCS,_AFXDLL,与项目1(MFC Exe)设置相同。
编译参数:注意使用的Run-time library参数为/MDd。
库参数:没有太大区别。

(5.3) Use MFC in a Static DLL
编译参数:注意使用的Run-time library参数为/MTd。

/////////////////////////////////////////////////////////////////////////////////
6.Win32 Application项目
预编译头文件stdafx.h
#define WIN32_LEAN_AND_MEAN    // Exclude rarely-used stuff from Windows headers

// Windows Header Files:
#include <windows.h>

// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>

出现了Win32程序的入口函数WinMain。

这个项目,Project->settings->General中设置使用MFC的方式,一般设为Not Using MFC,否则程序结构的改动比较大。从Not Using MFC到使用MFC的改变,对连接的库的影响类似于项目4(Win32 DLL)。

(6.1) Not Using MFC
预定义:WIN32,_DEBUG,_WINDOWS,_MBCS
编译参数:/nologo /MLd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /Fp"Debug/W32StaPrehead.pch" /Yu"stdafx.h" /Fo"Debug/" /Fd"Debug/" /FD /GZ  /c
注意到使用的Run-time library参数为/MLd。
连接参数: kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /incremental:yes /pdb:"Debug/W32AppDemo.pdb" /debug /machine:I386 /out:"Debug/W32AppDemo.exe" /pdbtype:sept

(6.2) Use MFC in a Shared DLL
编译参数:注意使用的Run-time library参数为/MDd。

(6.3) Use MFC in a Static DLL
编译参数:注意使用的Run-time library参数为/MTd。

小结:
1.MFC的使用方式对默认情况选择的Run-time library的影响(以Debug版本为例):
Not Using MFC ---〉/MLd: Debug Single-Threaded(静态连接LIBCD.LIB库)
Use MFC in a Shared DLL ---〉/MDd: Debug Multithreaded DLL(动态连接MSVCRTD.DLL库)
Use MFC in a Static DLL ---〉/MTd: Debug Multithreaded(静态连接LIBCMTD.LIB库)

2.如果不使用MFC,在Link一栏一般会连接一系列Windows API的库文件;如果使用MFC,这些连接库就会“消失”。

3.Debug版本一般会有_DEBUG的预定义,而Release版本则定义NDEBUG。

4.使用Shared MFC和Static MFC相比,前者一般多一个_AFXDLL的定义。默认使用的Run-time Library也不一样,前者为/MDd,后者为/MTd。

5.MFC的普通DLL项目比MFC的EXE项目,一般多_WINDLL和_USRDLL预定义;连接参数多一个/dll定义。而MFC扩展DLL项目与MFC普通DLL项目相比,预定义将_USRDLL换成了_AFXEXT。

6.不使用MFC的Win32 DLL与MFC DLL相比,预定义少了_WINDLL和_AFXDLL,而仅保留了_USRDLL。

7.不使用MFC的静态库有_LIB的预定义。

8.#include <afxwin.h> 和#include <windows.h>不能重复包含,前者用于MFC程序,后者用于程序。

9.为了去掉Windows头文件中很少用到的定义,一般在stdafx.h中,Win32程序会定义#define WIN32_LEAN_AND_MEAN,而MFC程序会定义#define VC_EXTRALEAN。

10.作为本文的应用,改变项目参数设置,实现不同类型项目之间的项目转换,如下:
MFC Exe   <======> MFC DLL
  ||                  ||
  ||                  ||
  ||                  ||
Win32 Exe <======> Win32 DLL

抱歉!评论已关闭.