Windows下的控制台輸出

來源:互聯網
上載者:User

控制台輸出就象dos下的輸出,可不是圖形介面。象ping/ipconfig/ftp等命令都是這類程式。

回憶過去,在dos下進行檔案操作時,常用到“檔案把柄”的概念,使用檔案把柄操作時,非常方便,操作時,只要知道把柄號就可以,而不用操心檔案的位置。dos下,裝置也都有自己的專用把柄,這些把柄是:
0000H 標準輸入裝置 (stdin)
0001H 標準輸出裝置 (stdout)
0002H 標準錯誤裝置 (stderr)
0003H 標準輔助裝置 (stdaux)
0004H 標準列印裝置 (stdprn)

stdin和stdout可以被再定向,dos下常用的“輸入改向”和“輸出改向”就是這個意思。

下面的dos功能調用中將向螢幕輸出資訊:
mov ah,40h ;寫到檔案或裝置
mov bx,1 ;標準輸出裝置
lea dx,OutData ;DS:DX->要輸出的資料
mov cx,Count ;要輸出字元的個數
int 21h ;執行dos功能調用

利用同樣的道理,在windows下,也可向螢幕輸出資訊。這要用到兩個Api函數,一個是GetStdHandle,另一個是WriteFile,在Win32 developer's References中它們是這樣定義的:
------------------------------------------------------------
HANDLE GetStdHandle(
DWORD nStdHandle // input, output, or error device
); 

The GetStdHandle function returns a handle for the standard input, standard output, or standard error device.

nStdHandle->Specifies the device for which to return the handle. This parameter can have one of the following values:
Value | Meaning
--------------------+---------------------------
STD_INPUT_HANDLE | Standard input handle
STD_OUTPUT_HANDLE | Standard output handle
STD_ERROR_HANDLE | Standard error handle

If the function succeeds, the return value is a handle of the specified device.
------------------------------------------------------------
BOOL WriteFile(
HANDLE hFile, // handle to file to write to
LPCVOID lpBuffer, // pointer to data to write to file
DWORD nNumberOfBytesToWrite, // number of bytes to write
LPDWORD lpNumberOfBytesWritten, // pointer to number of bytes written
LPOVERLAPPED lpOverlapped // pointer to structure needed for overlapped I/O
); 

=============================================================

;下面我們看一個程式,作用是顯示一個字串資訊!

.386
.model flat,stdcall
option casemap:none

include windows.inc

include kernel32.inc
include user32.inc

includelib kernel32.lib
includelib user32.lib

.data
mess db 'How are you !',0 ;要顯示的資訊

.data?
StdOut dd ? ;存放標準輸出的把柄
CharOut dd ? ;記錄實際輸出的字元數

.code
start: 
invoke GetStdHandle,STD_OUTPUT_HANDLE ;擷取標準輸出的把柄
mov StdOut,eax ;儲存把柄號

lea eax,mess
invoke lstrlen,eax ;求字串的長度

lea ecx,CharOut
invoke WriteFile,StdOut,addr mess,eax,ecx,NULL ;寫檔案

invoke ExitProcess,NULL ;程式結束
end start
--------------------------------------------------------------
編譯連結,下面給出詳盡的資訊,供分析參考:
D:\MASM7>dir /ad

Volume in drive D has no label
Volume Serial Number is 18F0-186B
Directory of D:\MASM7

. 〈DIR〉 02-12-03 17:36 .
.. 〈DIR〉 02-12-03 17:36 ..
LIB 〈DIR〉 02-12-03 17:38 LIB
INCLUDE 〈DIR〉 02-12-03 17:38 INCLUDE
0 file(s) 0 bytes
4 dir(s) 2,411,925,504 bytes free

D:\MASM7>ml /coff /I include 4.asm /link /subsystem:console /libpath:lib
Microsoft (R) Macro Assembler Version 6.14.8444 └─ 控制台程式
Copyright (C) Microsoft Corp 1981-1997. All rights reserved.

Assembling: 4.asm
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/subsystem:console /libpath:lib
"4.obj"
"/OUT:4.exe"

D:\MASM7>4
How are you !
D:\MASM7>
--------------------------------------------------------------
另外,在masm32.inc中有函數StdOut的聲明,可用來直接輸出資訊,把上面的例子修改後就是下面的樣子,可見來得更簡煉,供大家參考:

.386
.model flat,stdcall
option casemap:none ;case sensitive

include windows.inc

include kernel32.inc
include masm32.inc

includelib kernel32.lib
includelib masm32.lib

.data
mess db 'How are you !',0 

.code
start: 
invoke StdOut,addr mess
invoke ExitProcess,NULL
end start

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.