Windows_Programing_English_To_Chinese_1_MSVC_C运行库

          我发现很少有书籍会从C语言运行库开始介绍Windows编程,而我通过Google发现了一个介绍

Windows下C语言编程的网站,从C语言运行库介绍开始,因此计划在接下来的一段时间内,将那个

网站的文章慢慢的弄成中文版的。

声明一点:  请不要将本系列的文章作为商业用途。

                      转载请注明出处。http://www.cnblogs.com/volcanol

                      原文版权归原网站所有,本人仅作中文版的试翻译。


                      鉴于本人C语言、Windows编程和英语水平不高,请各位大侠见谅,在转译过程中如有不当,

请不吝斧正。


MODULE A

IMPLEMENTATION SPECIFIC

MICROSOFT C Run-Time Win32 1

 

 

 

My Training Period: aa hours.Before you begin, read someinstruction here.

 

 

What are in this Module?

The Background Story: The Microsoft C Run-time Libraries

The Standard C++ Library

The Run-Time Functions

Directory Management

Directory Management Functions

Program Examples

本版块包含那些内容:

背景:Microsoft C运行时库

标准C++

运行时函数

目录管理

目录管理函数

程序实例

 

 

 

 

The skills that supposed to be acquired:

Able to recognize the standard C and implementation specific.

Able to grasp the fundamental of the Windows file system, file and directory.

Able to find and collect the needed information in order to use functions to accomplish your tasks.

Able to understand and use functions that available in the libraries in your own programs.

下面的内容假设您已经学习过C语言基础并:

1、能组织标准C语言和实现特定的应用

2、Windows文件系统、文件和目录的基础知识

3、能够收集必要的信息,并且运用这些信息来实现自己的任务

4、能够理解标准库文件中的函数,并且能在自己的程序中使用这些函数


In this Module you will be introduced to the absolute implementation specific of the C, the Microsoft C.  

However it is still non-GUI type of programs :o).

在这个版块将向你介绍特定的C实现: Microsoft C。然而介绍的依然是非图形话界面的程序设计


This brings you to the specific use of the compilers that compile the Windows applications and the programs

 developed are specific to be run on the Windows platform although in the discussion and program examples

 of this Module and later on, the standard and non standard (Microsoft extension) libraries will be used together.

本版块向你介绍能


Make sure you have already fluent in functions because we will learn how to use them.  

Remember all the thing about a function such as parameters, return values etc.  The final purpose o

f this Module and that follows actually to show you how to find and collect all the required information from

 tons of the documentations to suit your needs in developing programs.

确定你已经熟练掌握函数,因为在后面将会使用函数。记住函数的参数、返回值等等。

本模块的目的是向你介绍怎样从文档中查找和收集在你程序中要使用的必要的信息。


Keep in mind that, normally, in libraries we have the following things which defined in and accessed through the header files:

Macro definitions.

Function prototypes.

Structure definitions (struct).

Enumeration definitions (enum).

Data type definitions.

The typedef usage.

请牢记,通常,在库文件中通过在头文件中定义和访问下面的内容:

1、宏定义

2、函数原型声明

3、结构体定义

4、枚举类型定义

5、数据类型定义

6、Typedef的使用


For the data type and structure definitions, you will find that use of struct, typedef and enum is normal here and also for 

Win32 programming (will be discussed in another Module).

至于数据类型和结构体定义,你会发现结构体、typedef和枚举的使用在本系列文章和Win32编程

(在以后的模块会讨论)使用非常多。


To better understand you must be fluent on those things.  If you encounter any problem, you might have to go back to basic :o)

 by referring to tenouk.com. Other pre requirement topics include array and pointer.

为了更好的理解win32编程,你必须熟练掌握上述内容,如果碰到任何问题,

你必须回头复习基础知识(通过访问tenouk.com)。其他必须掌握的主题是:数组和指针。


The Background Story: The Microsoft C Run-time Libraries

背景故事:Microsoft C运行库


The following table lists a various .lib files that comprise the C run-time (CRT) 

libraries as well as their associated compiler options and preprocessor directives. 

 The libraries contain the C run-time library functions.

For these libraries, the header files used in the programs must have the .h extension.

下面的列表列出了部分组成C运行时的.lib库文件,同时还列出与这些库文件相关的编译器参数/选项和预处理指令。

 这些库文件包含了C语言运行库函数。要使用这些库文件必须包含与库文件相关的.h头文件。

 

 

 

Pure C run-time library

C语言运行库文件

Characteristics

特性

Compiler Option

编译器选项

Preprocessor directives

预处理指令

LIBC.LIB

Single-threaded, static link.

单线程、静态链接

/ML

-

LIBCMT.LIB

Multithreaded, static link.

多线程、静态链接

/MT

_MT

MSVCRT.LIB

Multithreaded, dynamic link (import library for MSVCR70.DLL).  

Be aware that ifyou use the Standard C++ Library, your program

 will need MSVCP70.DLL to run.

多线程、动态链接(MSVCR70.dll 导出库文件),如果你需要使

用标准C++库文件,你需要MSVCP70.dll文件来运行你的程序

/MD

_MT, _DLL

LIBCD.LIB

Single-threaded, static link (debug).

单线程、静态链接(debug

/MLd

_DEBUG

LIBCMTD.LIB

Multithreaded, static link (debug).

多线程、静态链接(Debug

/MTd

_DEBUG, _MT

MSVCRTD.LIB

Multithreaded, dynamic link (import library for MSVCR70D.DLL) (debug)

多线程、动态链接(MSVCR70D.DLL导出库文件

/MDd

_DEBUG, _MT, _DLL

Table 1: C run-time libraries

1 C语言运行库

 

The Compiler Options, Preprocessor Directives and other configuration settings can be accessed from Project menu your_project_name Properties... sub menu.

编译选项、预处理指令和其他的编译器参数可以通过(MSVC 环境)的工程菜单——》 你的工程属性 子菜单来配置

如图Figure 0所示。

 

Figure 0

It also can be accessed by selecting your project directory (in the Solution Explorer pane)  Right click  Select the Properties menu as shown below.

同样你可以在项目浏览器中通过右击你的项目目录,然后选择项目属性来配置编译选项、预处理指令和其他的编译器参数,如下图1所示:

                                                            Figure 1

 

The following Figure is a project Property pages.

下图所示为项目属性标签页:

 

 

Figure 2.

If you link your program from the command line without a compiler option that specifies a C run-time library, the linker will use LIBC.LIB by default.

如果你通过命令行的形式来链接你的程序,而没有为C语言库指定特定的编译器选项,那么编译器就会使用默认的LIBC.LIB库文件。


To build a debug version of your application, the _DEBUG flag must be defined and the application must be linked with a debug version of one of these libraries.

为了建立Debug版本的应用程序实例, 那么必须指定_DEBUG预处理指令,并且应用程序必须链接一个debug版本的库文件。


However in this Module and that follows we will compile and link by using the Visual C++ .Net IDEs’ menus instead of command line :o).

在本模块以及接下来的部分,我们将使用Visual C++ .Net IDE环境来编译和链接应用程序。


The Standard C++ Library

标准C++


The run-time libraries also include .lib files that contain the iostream library and the Standard C++ Library. 

You should never have to explicitly link to one of these .lib files;

 the header files in each library link in the correct .lib file.

运行库(译注:MS 运行库)同时还包含iostream库和标准C++库,

你不需要显示的指定链接这些库文件,标准头文件已经指定如何链接正确的库文件。


The programs that use this old iostream library will need the .h extension for the header files.

使用老版本的iostream包含头文件是需要指定.h 扩展名。


Before Visual C++ 4.2, the C run-time libraries contained the iostream library functions. 

 In Visual C++ 4.2 and later, the old iostream library functions have been removed from

 LIBC.LIB, LIBCD.LIB, LIBCMT.LIB, LIBCMTD.LIB, MSVCRT.LIB, and MSVCRTD.LIB.

VC 4.2 前,C语言运行时库包含iostream库函数功能。在VC 4.2和以后的版本,

老版本的iostream库函数已经从LIBC.LIBLIBCD.LIB、 LIBCMD.LIB、 LIBCMTD.LIBMSVCRT.LIBMSVCRTD.LIB


This change was made because the Standard C++ Library has been added to Visual C++, and it contains a new set of iostream libraries.

有这种改变是因为标准C++库文件已经添加到VC++里面,并且由VC++实现,并且VC++里面同时还包含了一个全新的iostream库。


Thus, two sets of iostream functions are now included in Visual C++.  The old iostream functions now exist in their own libraries as shown in the following Table.

因此,新老版本的iostream函数现在同时包含在Visual C++。老版本的iostream函数现在都由本身的库文件来实现,如下表所示:


Old iostream library

Characteristics

Compiler Option

Preprocessor directives

LIBCI.LIB

Single-threaded, static link.

/ML

-

LIBCIMT.LIB

Multithreaded, static link.

/MT

_MT

MSVCIRT.LIB

Multithreaded, dynamic link (import library for MSVCI70.DLL).

/MD

_MT, _DLL

LIBCID.LIB

Single threaded, static link.

/MLd

_DEBUG

LIBCIMTD.LIB

Multithreaded, static link.

/MTd

_DEBUG, _MT

MSVCIRTD.LIB

Multithreaded, dynamic link (import library for MSVCI70D.DLL).

/MDd

_DEBUG, _MT, _DLL

 

Table 2: Old iostream libraries

The new iostream functions, as well as many other new functions, that exist in the Standard C++ Library are shown in the following Table.

The programs that use this new iostream library will need the header files without the .h extension such as <string>.

在标准C++库里面包含的新的iostream函数、以及很多新的函数功能,如下表所列。

当在程序中使用新的iostream库文件在包含头文件的时候不需要.h扩展名,例如包含<string>头文件。

Standard C++ Library

Characteristics

Compiler Option

Preprocessor directives

LIBCP.LIB

Single-threaded, static link

/ML

 -

LIBCPMT.LIB

Multithreaded, static link

/MT

_MT

MSVCPRT.LIB

Multithreaded, dynamic link (import library for MSVCP70.dll)

/MD

_MT, _DLL

LIBCPD.LIB

Single-threaded, static link

/MLd

_DEBUG

LIBCPMTD.LIB

Multithreaded, static link

/MTd

_DEBUG, _MT

MSVCPRTD.LIB

Multithreaded, dynamic link (import library for MSVCP70.DLL)

/MDd

_DEBUG, _MT, _DLL

 

Table 3: Standard C++ libraries

The Standard C++ Library and the old iostream library are incompatible, 

that is they cannot be mixed and only one of them can be linked with your project.

特别需要注意的是:标准C++库和老版本的iostream库文件是不兼容的,

因此在程序中不能混合使用,在同一程序里面只能链接一个库到你的应用软件项目。


The old iostream library created when the standard is not matured yet.

When you build a release version of your project, one of the basic C run-time libraries

 (LIBC.LIB, LIBCMT.LIB, and MSVCRT.LIB) is linked by default, depending on the compiler option you choose

 (single-threaded, multithreaded, or DLL).

老版本iostream库文件产生的时候,C++标准还不成熟。

当你需要生成发布版本的应用软件的时候,默认会链接基础C语言运行库文件中的一个

LIBC.LIB, LIBCMT.LIB, and MSVCRT.LIB),链接那一个库文件取决于再编译链接的时候编译器参数和选项。


Depending on the headers you use in your code, a library from the Standard C++ libraries or one

 from the old iostream libraries may also be linked, for example:

取决于源代码中包含的头文件,在链接编译生成的目标文件和库文件时编译器

会根据实际需要选择链接标准C++库文件或老版本的iostream库文件。例如:


If you include a Standard C++ Library header in your code, a Standard C++ Library will be linked in

 automatically by Visual C++ at compile time. For example:

如果在源代码中包含标准C++库文件的头文件,VC++编译器在链接时会自动链接标准C++库,例如:

#include <ios>

例如:

#include <ios>

If you include an old iostream library header, an old iostream library will be linked in automatically by Visual C++ at compile time. For example:

#include <ios.h>

1、如果在程序中包含老版本的iostream库的头文件,VC++编译器会自动链接老版本的iostream库文件。


Headers determine whether a Standard C++ library, an old iostream library, or neither will be linked. 

 Compiler options determine which of the libraries to be linked is the default (single-threaded, multithreaded, or DLL).

When a specific library compiler option is defined, that library is considered to be the default and its preprocessor directives are automatically defined.  

Also read Module23 for the big picture.

As a conclusion, for typical C and C++ programs, by providing the proper header files just let the compiler determine for you which libraries are appropriate to be linked in.

头文件决定是否要链接标准C++库、老版本的iosteam库或者不链接任何库。编译选项决定在链接的时候默认链接具体的库文件(单线程、多线程或者动态链接库DLL)

当指定一个特定的编译选项时,上述的库文件会被当做默认库同时它们的预处理指令会被自动生成。 参见接下来的文章Module 23来查阅示意图。

作为一个结论,对于典型的CC++程序,建议提供合适的预定义头文件让编译器决定选择合适的库文件包含在程序中。


The Run-Time Functions

运行时函数功能


The following sections will dive more detail some of the functions available in the C run-time library. 

These functions used to create routines that can be used to automate many tasks in Windows that not available in the standard C/C++.

接下来的几段将会讨论更多的关于C运行库文件函数的细节,这些函数在过去生成C运行库,

以前这些C运行库用来自动匹配windows应用程序,

但是这些函数在标准C/C++函数不在可用。


We will start with category, then the functions available in that category and finally followed by program examples that use some of the functions.

The functions discussed here mostly deal with directories and files.

For complete information please refer to Microsoft Visual C++ documentation (online MSDN: Microsoft Visual C++). 

 As a remainder, the following are the things that must be fully understood when you want to use a function, and here, _chsize() is used as an example.

下面将按类分析,并且将介绍各个类中所包含的函数,最后接下来的程序实例会使用所介绍的函数。

这里讨论的函数主要是处理目录和文件功能。

如果要获取完整的消息,请参考MSVC的帮助文档(在线帮助系统MSDN)。

当你使用函数的时候,你必须要完全理解所使用的函数,在这里我们将以_chsize()函数为例说明。


What is the use of the function?  This will match with "what are you going to do or create?" 

 Keep in mind that to accomplish some of the tasks you might need more (several) than one function.  For example:

_chsize() used to change the file size.

What is the function prototype, so we know how to write (call) a proper syntax of the function.  For example:

int _chsize(int handle, long size);

From the function prototype, how many parameters, the order and what are their types, so we can create the needed variables.  For example:

What header files need to be included?  For example the _chsize needs:

<io.h>

What is the return type and value?  For example:

_chsize() returns 0 if the file size is successfully changed.  A return value of 1 indicates an error: errno is set to EACCES

 if the specified file is locked against access, to EBADF if the specified file is read-only or the handle is invalid, or to ENOSPC if no space is left on the device.

 2、那么_chsize()函数实现什么功能呢? 这取决于:你想做什么或者想产生什么? 

    无论怎样,要有这样的意识,为了实现特定的任务,你也许需要在你的程序中使用一个或更多的函数。例如:

                  _chsize()函数用来改变文件的大小。

 3、函数原型是什么呢? 通过函数原型声明,可以知道怎样用正确的语法实现和调用函数,例如

                  Int  _chsize(int handle, kong size);

 4、从函数原型可以知道函数的参数个数、参数顺序和参数类型,通过这些信息来确定形参列表。例如

  

handle

Handle referring to open file.

size

New length of file in bytes.

                

Hence, you are ready to use the function in any of your programs.  If you still blur about functions, please read C & C++ Functions tutorial.  

Not all the needed information is provided here and for complete one, please refers to the Microsoft Visual C++/MSDN/SDK Platform documentations or HERE.

到此,你已经懂得怎样在应用程序中使用函数,如果你对函数的使用依然有疑问,那么请参考C & C++ Functions tutorial

这里并没有介绍所有的必要的信息, 如果你要获取完整的信息,那么请参考MSVC++/MSDN/平台 SDK文件,或者通过下面的链接获取信息:

 HERE.

For C++, together with the classes, it is used when you develop programs using Microsoft Foundation Class (MFC) and Automatic Template Library (ATL).

The notation convention used for identifiers in MSDN documentation is Hungarian Notation and is discussed C & C++ Notations.

对于C++中类,当你通过MFCATL模板类开发应用程序时,需要用到。


 --------------------The Story and Program Examples---------------------

程序实例

Directory Management

Functions available in this category used to access, modify, and obtain information about the directory structure and are listed in the following Table.

Notice that most of the function names are similar to the standard C wherever available, except prefixed with underscore ( _ ) :o).  

The functions available in this category are listed in the following Table.

目录管理

这类函数用来访问、修改和获取目录结构体的信息, 如下表所列。

注意:下面所列的函数与标准C函数库里的函数类似,除了函数前面的下划线之外。

目录管理函数如下表所列:

Directory Management Functions

目录管理函数

§ The following Table lists functions used for directory control and management.

§ 下表所列函数用来进行目录管理和控制

Handle

句柄

Handle referring to open file.

打开文件的句柄

Size

大小

New length of file in bytes.

指定文件的新的大小,以字节为单位

Function

Use

_chdir(), _wchdir()

Change current working directory.

改变当前工作目录

_chdrive()

Change current drive.

改变当前驱动器

_getcwd(), _wgetcwd()

Get current working directory for default drive.

为默认驱动器获取当前工作目录

_getdcwd(), _wgetdcwd()

Get current working directory for specified drive.

为指定的驱动器获取当前工作目录

_getdiskfree()

Populates a_diskfree_t structure with information about a disk drive.

通过一个 _diskfree_t 结构体返回驱动信息

_getdrive()

Get current (default) drive.

获取默认驱动器

_getdrives()

Returns a bitmask representing the currently available disk drives.

返回代表当前可用的驱动器的位旗标信息

_mkdir(), _wmkdir()

Make new directory.

新建目录

_rmdir(), _wrmdir()

Remove directory.

删除一个目录

_searchenv(), _wsearchenv()

Search for given file on specified paths.

在指定的路径下搜索指定的文件

Table 4:  Directory control functions

目录控制函数

§ The following is an example of the needed information in order to use the  _getdrives() function.

§ 下面是一个关于如何通过_getdriives() 函数获取信息

Information

信息

Description

描述

The function

函数名

_getdrives().

The use

功能

Returns a bitmask representing the currently available disk drives.

返回当前可用驱动器的位旗标信息

The prototype

函数原型

int _getdrives(void);

Example

代码示例

ULONG DriveMask = _getdrives();

 

while(DriveMask)

{// list all the drives...

 //列出所有驱动器

  if(DriveMask & 1)

    printf(mydrives);

    ++mydrives[1];

   // shift the bit masks binary to the right and repeat

    DriveMask >>= 1;

}

The parameters

函数参数

void

The return value

函数返回值

If the function succeeds, the return value is a bitmask representing the currently available disk drives.

 Bit position 0 (the leastsignificant bit) is drive A, bit position 1 is drive B, bit position 2 is drive C, 

and so on. If the function fails, the return value is zero. 

To get extended error information, callGetLastError().

如果函数成功执行,那么返回一个代表当前可用驱动器的位旗标。D0位表示A盘, D1表示B盘, D2表示C盘,以此类推。 如果函数执行失败,那么返回一个非零值,可以通过GetLastError函数获取出错信息

The header file

要包含的头文件

<direct.h>

Table 5:  _getdrives() information

_getdrives() 函数使用信息

§ The following program example uses the _getdrives() function to list the available logical drives in the current machine.

§ 下面的程序代码使用_getdrives() 函数来列举出本机当前可以使用的驱动器。

#include<windows.h>

#include<direct.h>

#include<stdio.h>

#include<tchar.h>

 

// buffer, be careful with terminated NULL. Must match with ++mydrives[1]...that is one space

//for example if no one space: "A:"--> ++mydrives[0];

// you may try char mydrives[ ] = {" A: "}; or char mydrives[ ] = " A: ";

TCHAR mydrives[ ] = " A: ";

 

intmain()

{

  // get the drives bit masks...1 is available, 0 is not available A = least significant bit...

   ULONG DriveMask = _getdrives();

  // if something wrong

   if(DriveMask == 0)

      printf("_getdrives() failed with failure code: %d\n", GetLastError());

  else

   {

     printf("This machine has the following logical drives:\n");

     while (DriveMask)

     {   // list all the drives...

         if(DriveMask & 1)

            printf(mydrives);

        // go to the next drive strings with one space

        ++mydrives[1];

        // shift the bit masks binary to the right and repeat

         DriveMask >>= 1;

      }

  printf("\n");

}

   return 0;

}

 

The output:

 

This machine has the following logical drives:

 A:  C:  D:  E:  F:  G:  H:  I:  J:  K:  L:

Press any key to continue

§ The following is an example of the needed

     information in order to use the  _getdiskfree() function.

Information

信息

Description

介绍

The function

函数名

_getdiskfree().

The use

函数功能

Populates a_diskfree_t structure with information about a disk drive.

通过返回一个_diskfree_t 结构体返回驱动器的信息

The prototype

函数原型

unsigned _getdiskfree(unsigned drive,struct _diskfree_t * driveinfo);

Example

实例

struct_diskfree_t df = {0};

unsigneduDrive;

_getdiskfree(uDrive, &df);

The parameters

参数

drive - The disk drive for which you want information.

Drive-你要获取信息的驱动器

driveinfo- A_diskfree_t structure that will be populated with information about the drive. 

                   _diskfree_t is defined in direct.h.

Driveinfo : _diskfree_t 结构体变量,通过这个变量的指针返回驱动器信息,

 _diskfree_tdirect.h头文件中定义

The return value

返回值

If the function succeeds, the return value is zero. If the function fails,

 the return value is the error code. To get extended error information, call GetLastError().

如果函数成功执行,返回值=0

如果函数执行失败,返回出错代码,要获取更多错误信息,调用GetLaseError函数

The header file

头文件

<direct.h>

Table 6:  _getdiskfree() information

 6   _getdiskfree()函数

§ The following program example uses the _getdiskfree() function to list logical drives information in the current machine.

§ 下面的程序实例通过_getdiskfree()函数获取本机当前可以逻辑驱动器的信息。

#include<windows.h>

#include<direct.h>

#include<stdio.h>

#include<tchar.h>

 

TCHAR g_szText[ ]  = _T("Drive Total_clus Available_clus Sec/Cluster Bytes/Sec\n");

TCHAR g_szText1[ ] = _T("----- ---------- -------------- ----------- ---------\n");

TCHAR g_szInfo[ ]  = _T("->                                                   \n");

 

// ...for data display format...

// right justified, thousand comma separated and other format for displayed data

voidutoiRightJustified(TCHAR* szLeft, TCHAR* szRight, unsigned uValue)

{

   TCHAR* szCur = szRight;

   int nComma = 0;

   if(uValue)

   {

      while(uValue && (szCur >= szLeft))

         {

         if(nComma == 3)

               {

            *szCur = ',';

            nComma = 0;

         }

        else

               {

            *szCur = (uValue % 10) | 0x30;

            uValue /= 10;

            ++nComma;

         }

         --szCur;

      }

   }

  else

   {

      *szCur = '0';

      --szCur;

   }

 if(uValue)

   {

      szCur = szLeft;

      while(szCur <= szRight)

         {// if not enough field to display the data...

         *szCur = '*';

         ++szCur;

      }

   }

}

 

intmain()

{

   TCHAR szMsg[4200];

   struct _diskfree_t df = {0};

  // search drives and assigns the bit masks to uDriveMask variable...

   ULONG uDriveMask = _getdrives();

   unsigned uErr, uLen, uDrive;

  printf("clus - cluster, sec - sector\n");

   // may need a casting from TCHAR to char * here

   // printf((char *)g_szText);

   printf(g_szText);

   printf(g_szText1);

   for(uDrive = 1; uDrive <= 26; ++uDrive)

   {

     // if the drive is available...

      if(uDriveMask & 1)

         {// call _getdiskfree()...

         uErr = _getdiskfree(uDrive, &df);

        // provide some storage

         memcpy(szMsg, g_szInfo, sizeof(g_szInfo));

         szMsg[3] = uDrive + 'A' - 1;

        

        // if _getdiskfree() is no error, display the data

         if(uErr == 0)

               {

            utoiRightJustified(szMsg+4, szMsg+15, df.total_clusters);

            utoiRightJustified(szMsg+18, szMsg+29, df.avail_clusters);

            utoiRightJustified(szMsg+27, szMsg+37, df.sectors_per_cluster);

            utoiRightJustified(szMsg+40, szMsg+50, df.bytes_per_sector);

         }

        else

           {// print system message and left other fields empty

            uLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, uErr, 0, szMsg+8, 4100, NULL);

            szMsg[uLen+6] = ' ';

            szMsg[uLen+7] = ' ';

            szMsg[uLen+8] = ' ';

         }

         printf(szMsg);

      }

     // shift right the found drive bit masks and repeat the process

      uDriveMask >>= 1;

   }

   return 0;

}

 

The output sample:

 

§ Note that J: is CD-RW, L: is thumb drive and A: is a floppy.  For floppy and CD-ROM, you have to insert the media.

§ The following is another sample output run using VC++ EE 2005.

§ 注意: JCDRW驱动器, LU盘, A是软驱。对于软驱和CD-ROM, 必须插入软驱和光盘。

§ 下面是用VC++ EE2005版本输出的信息

§ The following is an example of the needed information in order to use the  _getdrive() function.

§ 下面的实例介绍关于如何使用 _getdrive() 函数的信息

clus - cluster, sec - sector

Drive Total_clus Available_clus Sec/Cluster Bytes/Sec

----- ---------- -------------- ----------- ---------

-> A       2,847           834       1          512

-> C   2,560,351        62,315       8          512

-> D   2,560,351     1,615,299       8          512

-> E   2,560,351     2,310,975       8          512

-> F   2,560,351     2,054,239       8          512

-> G   2,353,514     2,039,396       8          512

-> H   2,560,351     2,394,177       8          512

-> I   2,381,628     1,618,063       8          512

-> J     321,935             0       1        2,048

-> L      63,419        20,706      16          512

Press any key to continue

 

Information

信息

Description

描述

The function

函数

_getdrive().

The use

功能

Gets the current disk drive.

获取当前磁盘驱动器

The prototype

函数原型

int _getdrive(void);

Example

实例

Int  curdrive;

curdrive = _getdrive();

The parameters

参数

void

The return value

返回值

Returns the current (default) drive (1=A, 2=B, and so on).  There is no error return.

(默认)返回当前的驱动器(A=1B=2....),函数没有错误值返回,是不多的总是可以执行成功的函数之一

The header file

头文件

<direct.h>

Table 7:  _getdrive() information

 7  _getdrive()函数

§ The following Table lists the needed information in order to use the _chdir(), _wchdir() functions.

§ 下表列表介绍怎样使用 _chdir(), _wchdir() 函数

Information

Description

The function

函数

_chdir(),_wchdir().

The use

功能

Change the current working directory.

切换当前工作目录

The prototype

函数原型

int _chdir(const char *dirname);

/* for wide character */

int _wchdir(const wchar_t *dirname);

Example

实例

charpath1[50] = "C:\\WINNT\\System32\\config";

_chdir(path1);

The parameters

参数

dirname - Path of new working directory.

Dirname- 工作目录的完整路径

The return value

返回值

These functions return a value of 0 if successful.  A return value of 1 indicates that the specified path

 could not be found, in which case errno is set to  ENOENT.

函数成功执行返回0值。

返回值= -1 时表示找不到指定路径,

当函数执行失败,用GetLastError函数获取的返回值为 ENOENT

The header file

头文件

<direct.h> or <wchar.h> for_wchdir().

Table 8:  _chdir(),_wchdir() functions information

8  _chdir(),_wchdir()函数

§ The _chdir() function changes the current working directory to the directory specified by dirname.  The dirname parameter must refer to an existing directory.

§ _chdir()函数将当前工作路径改变通过dirname指定的路径。Dirname必须是系统存在的目录,(译注:该函数实现CD的功能)

§ This function can change the current working directory on any drive.  If a new drive letter is specified in dirname, the default drive letter will be changed as well.

§ 本函数可以将当前工作目录切换至其他任意的驱动器,如果通过dirname指定新的驱动器盘符,那么默认的(译注:当前的)驱动器盘符也会改变。

§ For example, if A is the default drive letter and \BIN is the current working directory, the following call changes the current working directory for drive

 C and establishes C as the new default drive:

§ 例如: 如果A是默认驱动器盘符(译注:当前驱动器盘符),同时\bin 是当前工作目录, 下面的函数调用将会时当前工作目录改变至C盘下temp目录。

_chdir("c:\\temp");

§ When you use the optional backslash character (\) in paths, you must place two backslashes (\\) in a C string literal to represent a single backslash (\).

§ 因为在C语言中反斜杠 符号有特定的意义,因此当指定路径时如果存在 符号时,那么必须使用 \\ 这样的形式来表示 符号。

§ _wchdir() is a wide-character version of _chdir().  The dirname argument to _wchdir() is a wide-character string.  _wchdir() and _chdir() behave identically otherwise.

§ _wchdir()函数是宽字符版本的_chdir() 函数, 对_wchdir()函数dirname是一个宽字符集的字符串。

§ Let use some of the functions in our programs.  You can follow the same steps for other functions :o).  The following is a program example that uses some of the functions.

§ 让我们在程序中使用这些函数,  你可以通过同样的步骤来测试其他的函数, 下面是一个如何使用这些函数的程序实例:

/*******cruntime.cpp*******/

/***Visual C++ .Net/7.0****/

#include<stdio.h>

#include<conio.h>

#include<direct.h>

#include<stdlib.h>

#include<ctype.h>

 

intmain(void)

{

intchr, drive, curdrive;

staticchar path[_MAX_PATH];

charbuffer[_MAX_PATH];

charnewdir[50] = "\\testdir";

// char path1[50] = "C:\\Windows\\System32\\config";

charpath1[50] = "C:\\WINNT\\System32\\config";

 

/* save current drive. */

curdrive = _getdrive();

printf("Available drives in this machine are: \n");

 

/* if we can switch to the drive, it exists. */

for(drive = 1; drive <= 26; drive++)

if(!_chdrive(drive))

  printf("%c: ", drive + 'A' - 1);

 

printf("\n\nType drive letter to check: ");

chr = _getch();

if(chr == 27)

  printf("Illegal drive input\n");

if(isalpha(chr))

  _putch(chr);

if(_getdcwd(toupper(chr) - 'A' + 1, path, _MAX_PATH) != NULL)

  printf("\nCurrent directory on that drive is:\n%s\n", path);

 

/* restore original drive. */

_chdrive(curdrive);

 

/* get the current working directory */

if(_getcwd(buffer, _MAX_PATH) == NULL)

  perror("_getcwd error");

else

  printf("\nCurrent working directory is: %s\n", buffer);

 

/* create a directory and then delete */

if(_mkdir(newdir) == 0)

{

  printf("\nDirectory %s was successfully created\n", newdir);

  system("dir \\testdir");

 

  if(_rmdir("\\testdir") == 0)

    printf("\nDirectory %s was successfully removed\n", newdir);

  else

    printf("\nProblem removing directory %s\n", newdir);

}

else

   printf("\nProblem creating directory %s\n", newdir);

 

/* uses _chdir() function to verify that a given directory exists */

printf("\n");

printf("Change directory........\n");

if(_chdir(path1))

  printf("Unable to locate the directory: %s\n", path1);

else

  system("dir *.log /a");

printf("\n");

return0;

}

 

The output:

      输出:

Available drives in this machine are:

C: D: F:

本机当前可用的驱动器有:

C:  D:  F: 

 

Type drive letter to check: c

Current directory on that drive is:

c:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE

驱动器盘符是:C

C驱动器盘下当前目录是:c:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE

 

Current working directory is: D:\mcruntime

Directory \testdir was successfully created

 Volume in drive D has no label.

 Volume Serial Number is 90C8-54BB

当前工作目录是: D\mcruntime

成功创建目录:\testdir

驱动器D盘没有卷标

驱动器D的盘符序列号是:90C8-54BB

Directory of D:\testdir

D:\testdir目录信息:     (译注:dir的功能)

 

05/17/2005  08:54p      <DIR>          .

05/17/2005  08:54p      <DIR>          ..

               0 File(s)              0 bytes

               2 Dir(s)  13,975,789,568 bytes free

 

Directory \testdir was successfully removed

 \testdir目录成功删除

Change directory........

改变当前工作目录

 Volume in drive C has no label.

 Volume Serial Number is B03D-C18F

驱动器盘符没有卷标信息

驱动器盘符序列号:B03D-C18F

 Directory of C:\WINNT\System32\config

C:\WINNT\System32\config 目录信息:    (译注:dir显示信息 )

 

05/17/2005  08:07p               1,024 default.LOG

05/17/2005  08:08p               1,024 SAM.LOG

05/17/2005  08:17p               1,024 SECURITY.LOG

05/17/2005  08:54p              12,288 software.LOG

06/05/2004  07:06a               1,024 system.LOG

06/05/2004  07:06a                   0 TempKey.LOG

06/05/2004  07:06a               1,024 userdiff.LOG

               7 File(s)         17,408 bytes

               0 Dir(s)   7,208,861,696 bytes free

 

Press any key to continue

按任意键继续...........

The following is a sample output run using VC++ EE 2005 on another machine.

下面是用VC++ EE 2005 在另外机器上编译执行输出的结果:

The following example tested on Win XP Pro SP2 using Visual C++ Express Edition 2005 with Windows SDK installed.

下面的程序在安装了VC++ EE 2005 Express版本的WinXP Pro SP2 机器上测试:

/*******cruntime.cpp*******/

/***Visual C++ EE 2005 on Win XP Pro SP 2 ****/

#include <stdio.h>

#include <conio.h>

#include <direct.h>

#include <stdlib.h>

#include <ctype.h>

 

int main(void)

{

    int chr, drive, curdrive;

    static char path[_MAX_PATH];

    char buffer[_MAX_PATH];

    char newdir[50] = "\\testdir";

    // charpath1[50] = "C:\\WINNT\\System32\\config";

    char path1[50] = "C:\\Windows\\System32\\config";

    /* save current drive. */

    curdrive = _getdrive();

    printf("Available drives in this machine are: \n");

    /* if we can switch to the drive, it exists. */

    for(drive = 1; drive <= 26; drive++)

        if(!_chdrive(drive))

    printf("%c: ", drive + 'A' - 1);

    printf("\n\nType drive letter to check: ");

    chr = _getch();

    if(chr == 27)

        printf("Illegal drive input\n");

    if(isalpha(chr))

        _putch(chr);

    if(_getdcwd(toupper(chr) - 'A' + 1, path, _MAX_PATH) != NULL)

        printf("\nCurrent directory on that drive is:\n%s\n", path);

    /* restore original drive. */

    _chdrive(curdrive);

    /* get the current working directory */

    if(_getcwd(buffer, _MAX_PATH) == NULL)

        perror("_getcwd error");

    else

        printf("\nCurrent working directory is: %s\n", buffer);

    /* create a directory and then delete */

    if(_mkdir(newdir) == 0)

    {

        printf("\nDirectory %s was successfully created\n", newdir);

        system("dir \\testdir");

        if(_rmdir("\\testdir") == 0)

            printf("\nDirectory %s was successfully removed\n", newdir);

        else

            printf("\nProblem removing directory %s\n", newdir);

    }

    else

        printf("\nProblem creating directory %s\n", newdir);

    /* uses _chdir() function to verify that a given directory exists */

    printf("\n");

    printf("Change directory........\n");

    if(_chdir(path1))

        printf("Unable to locate the directory: %s\n", path1);

    else

        system("dir *.log /a");

    printf("\n");

    return 0;

}

 

A sample output:

 

Running sample programs using Visual C++ Express Edition 2005, 2008 and Visual C++ 2005/Visual Studio 2008

利用 Visual C++ Express Edition 2005, 2008 and Visual C++ 2005/Visual Studio 2008来测试实例程序

For the above mentioned IDEs, you need to set the Character Set to Not Set else you need to change all the types

 in the programs to Unicode. The following Figures show the project setting for the character set used in the project.

对于上面列举的IDE环境,你需要将字符集设定为Not Set,同样你还需要将程序变换成Unicode字符集类型。 

下面的图向你展示如何进行设置项目属性。

 

 

Visual C++ Express Edition 2005

 


 

Visual C++ 2005 of the Visual Studio 2005



 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值