Vc program error-call the dynamic library and pass the file pointer operation

Source: Internet
Author: User

VC program error-call dynamic library and pass file pointer operation (WINXP)

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

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

** Article name: VC program error-Call a dynamic library and upload a file pointer(WINXP) ** copyright information: this information can be reproduced at will, but the source and author information must be indicated. ** Author: ** time: 2008.6.27

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

1. You have a program that calls the interface in the dynamic library externally. The interface parameters have a file descriptor. Then, the dynamic library needs to use this file descriptor. Such a program can be used normally in HP-Unix, Solaris, and Linux systems, but an error is reported when it runs on a Windows operating system. File pointers cannot be shared.

2. Add the compilation option/MD (multithreaded DLL) directly to the dynamic library compilation and the executable program compilation, so that they can be shared with each other.

III. The following are some comparisons of the compilation parameter settings in VC found on the Internet:
//////////////////////////////////////// /////////////////////////////////////////
1. MFC program based on the dialog box (/single document/Multi-document)
Pre-compiled header file stdafx. h:
# Define vc_extralean // exclude rarely-used stuff from Windows Headers

// Afxwin. h declares some basic classes encapsulated by MFC (CWnd, CView, CButton, CDC, etc)
# Include <afxwin. h> // MFC core and standard components
// Afxext. h declares some extension classes of MFC (CBitmapButton, CControlBar, CSplitterWnd, etc)
# Include <afxext. h> // MFC extensions
// Afxdisp. h declares several Ole classes (COleException, COleVariant, etc)
# Include <afxdisp. h> // MFC Automation classes
// Afxdtctl. h declare several control classes (CImageList, CMonthCalCtrl, CDateTimeCtrl, etc)
# Include <afxdtctl. h> // MFC support for Internet Explorer 4 Common Controls

# Ifndef _ AFX_NO_AFXCMN_SUPPORT
// Afxcen. h declare some commonly used control classes of MFC (CListCtrl, CProgressCtrl, CToolTipCtrl, etc)
# Include <afxcmn. h> // MFC support for Windows Common controls
# Endif // _ afx_no_afxcmn_support

(1.1) use MFC in a shared DLL
Debug version:
Predefined values: Win32, _ debug, _ WINDOWS, _ afxdll, and _ MBCS
Compilation parameters: /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
Connection parameters:/nologo/subsystem: Windows/Incremental: yes/PDB: "Debug/exedlg. PDB "/debug/machine: i386/out:" Debug/exedlg.exe "/pdbtype: sept

Release Version:
Pre-defined: replace _ debug with ndebug compared to the debug version.
Compilation parameters: /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
Connection parameters:/nologo/subsystem: Windows/Incremental: No/PDB: "release/exedlg. PDB"/machine: i386/out: "release/exedlg.exe"

(1.2) use MFC in a static library
Debug version:
Pre-defined: _ afxdll is missing compared with (1.1 ).
Compile parameters: replace/MDD (run-time Library: Debug multithreaded DLL) with/MTD (run-time Library: Debug multithreaded)
Connection parameters: the same as (1.1)

Release Version:
Compile the parameter/MD (use run-time Library: multithreaded DLL) With Mt (use run-time Library: multithreaded)

* ** Note: The meanings of the above compilation/connection parameters are as follows (for more information, see msdn ):
/Nologo: suppress information output in the output window during compilation or connection;/MD: msvcrt is used in the Runtime Library. DLL;/W3: the level of warning displayed during compilation is 3;/GM: Enable minimal rebuild, an option to reduce recompilation;/GX: Enable exception handling;/Zi: set the database file for saving the debug information. in PDB;/OD: disable code optimization;/FR: generated. the SBR file, which contains signed information;/FP: names the pre-compiled header files generated;/YU: Specifies the pre-compiled header files.

//////////////////////////////////////// /////////////////////////////////////////
2. mfc dll Project
Pre-compiled header file 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
Added two OLE header files and two database header files.

(2.1) Use MFC in a Shared DLL
Debug version:
Pre-defined: WIN32, _ DEBUG, _ WINDOWS, _ WINDLL, _ AFXDLL, _ MBCS, _ USRDLL. Compared with the MFC Exe program, _ WINDLL and _ USRDLL are added.
Compilation parameters: similar to MFC Exe
Connection parameters:/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
Compared with MFC Exe, the/dll definition and/def: "./MFCDll. def" And/implib: "Debug/MFCDll. lib" are added ". Note: MFCDll is the name of the project to be tested and a non-standard DLL name.
From the project file, this project generates a. Def file more than the mfc exe file to define the export function.

The difference between the release version and the debug version is similar to the comparison in Project 1 (defined in _ afxdll ).

(2.2) use MFC in a static DLL
The difference with (2.1) mainly lies in the run-time library type used, and is compared with that in Project 1.

//////////////////////////////////////// /////////////////////////////////////////
3. MFC extension DLL Project
The pre-compiled header file stdafx. H has the same content as project 2.

(3.1) use MFC in a shared DLL
Debug version:
Predefined values: Win32, _ debug, _ WINDOWS, _ MBCS, _ afxext, _ Windll, and _ afxdll. Compared with project 2, _ usrdll is replaced with _ afxext.
Compilation parameters: there is no big difference from the above project
Connection parameters: similar to the mfc dll Project

The difference between the release version and the debug version is similar to the comparison in Project 1 (defined in _ afxdll ).

(3.2) use MFC in a static DLL
Similar to the comparison of the above projects.

(Note: The following items are described in the debug version .)
//////////////////////////////////////// /////////////////////////////////////////
4. Win32 DLL Project
Pre-compiled header file stdafx. h:
# Define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers

# Include <windows. h>

The implementation of the project entry function DllMain appears.

(4.1) Not Using MFC
Predefined values: WIN32, _ DEBUG, _ WINDOWS, _ MBCS, _ USRDLL, and WIN32DLLDEMO_EXPORTS. Compared with project 2 (mfc dll), _ WINDLL, _ AFXDLL, only _ USRDLL is retained. In addition, the WIN32DLLDEMO_EXPORTS custom export macro.
Compilation parameters: there is no big difference.
Connection parameter: 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
Compared with the mfc dll project, there are many more library connections, with/subsystem missing: windows definition.

(4.2) Use MFC in a Shared DLL
Pre-defined: Compared with (4.1), the definition of _ WINDLL and _ AFXDLL is added.
Compilation parameters: there is no big difference.
Connection parameters:/nologo/dll/incremental: yes/pdb: "Debug/Win32DllDemo. pdb "/debug/machine: I386/out:" Debug/Win32DllDemo. dll "/implib:" Debug/Win32DllDemo. lib "/pdbtype: sept
As you can see, many of the databases connected in (4.1) disappear. At this time, Project 4 becomes similar to the settings in Project 2.
* ** Note the stdafx of Project 4 during programming. h only contains <windows. h>. If you want to use the MFC class, you need to add several header files (<afxwin. h>, <afxext. h>), and # include <windows. h> comment out the function with the DllMain entry!

(4.3) Use MFC in a Static DLL
The difference between the use of mfc dll in the way of Shared and Static is similar to that of the above project, and will not be compared.

//////////////////////////////////////// /////////////////////////////////////////
5. Win32 Static Library project
Pre-compiled header file stdafx. h (this file may not exist ):
If you do not use MFC, simply # define WIN32_LEAN_AND_MEAN. If you use MFC, the content is as follows:
# Define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers

# Include <afx. h>
# Include <afxwin. h>

(5.1) not using MFC
Pre-defined: Win32, _ debug, _ MBCS, _ Lib, new symbol _ lib
Compilation parameters: /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
Note that the run-time library parameter is/MLD.
Library parameters: This project has no link settings page, instead of the library page. The parameters are as follows:/nologo/out: "Debug/w32staprehead. lib"

(5.2) use MFC in a shared DLL
Predefined values: Win32, _ debug, _ WINDOWS, _ MBCS, and _ afxdll, which are the same as those set in Project 1 (MFC exe.
Compile parameters: note that the run-time library parameter is/MDD.
Library parameters: there is no big difference.

(5.3) use MFC in a static DLL
Compile parameters: note that the run-time library parameter is/MTD.

//////////////////////////////////////// /////////////////////////////////////////
6. Win32 Application Project
Pre-compiled header file 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>

The entry function WinMain of the Win32 program appears.

In this Project, the MFC method is set in Project-> settings-> General. It is generally set to Not Using MFC. Otherwise, the program structure is greatly changed. From Not Using MFC to Using MFC, the impact on connected libraries is similar to project 4 (Win32 DLL ).

(6.1) Not Using MFC
Pre-defined: WIN32, _ DEBUG, _ WINDOWS, _ MBCS
Compilation parameters: /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
Note that the Run-time library parameter is/MLd.
Connection parameter: 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
Compile parameters: note that the Run-time library parameter is/MDd.

(6.3) Use MFC in a Static DLL
Compile parameters: note that the Run-time library parameter is/MTd.

Summary:
1. The impact of MFC usage on the default Run-time library (take the Debug version as an example ):
Not Using MFC --->/MLd: Debug Single-Threaded (static connection LIBCD. LIB Library)
Use MFC in a Shared DLL --->/MDd: Debug Multithreaded DLL (dynamically connect to the MSVCRTD. DLL library)
Use MFC in a Static DLL --->/MTd: Debug Multithreaded (Static connection LIBCMTD. LIB Library)

2. If you do not use MFC, a series of Windows API library files are usually connected in the Link column. If you use MFC, these connection libraries will disappear ".

3. the Debug version usually has the pre-Definition of _ DEBUG, while the Release version defines NDEBUG.

4. Compared with Static MFC, Shared MFC generally has one more definition of _ AFXDLL. The default Run-time Library is different. The former is/MDd, and the latter is/MTd.

5. The common DLL project of MFC is generally predefined by _ WINDLL and _ USRDLL compared to the EXE project of MFC. One more connection parameter/dll is defined. Compared with the MFC common DLL project, the MFC extension DLL project has changed _ USRDLL to _ AFXEXT.

6. Compared with mfc dll, Win32 DLL without MFC is less predefined than _ WINDLL and _ AFXDLL, but only _ USRDLL is retained.

7. static libraries without MFC are predefined AS _ LIB.

8. # include <afxwin. h> and # include <windows. h> cannot be included repeatedly. The former is used for the MFC program, and the latter is used for the program.

9. In order to remove the definition rarely used in Windows header files, generally in stdafx. h, the Win32 program defines # define WIN32_LEAN_AND_MEAN, while the MFC program defines # define VC_EXTRALEAN.

10. As an application in this article, change the project parameter settings to implement project conversion between different types of projects, as shown below:
MFC Exe <======> MFC DLL
|
|
|
Win32 Exe <======> Win32 DLL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.