PDA

View Full Version : مشکل با ReadConsole



ICEMAN
جمعه 24 اسفند 1386, 17:57 عصر
سلام ...
من این کد و توی یه کتاب دیدم ولی دقیقا نگرفتم چی شد!!!


#include "Envirmnt.h" /* #define or #undef UNICODE here. */
#include <windows.h>
#include <stdarg.h>
#include <tchar.h>
#include <iostream>
using std:: cout;
using std:: endl;

BOOL PrintStrings (HANDLE hOut, ...)

/* Write the messages to the output handle. */
{
DWORD MsgLen, Count;
LPCTSTR pMsg;
va_list pMsgList; /* Current message string. */
va_start (pMsgList, hOut); /* Start processing messages. */
while ((pMsg = va_arg (pMsgList, LPCTSTR)) != NULL) {
MsgLen = _tcslen (pMsg);
/* WriteConsole succeeds only for console handles. */
if (!WriteConsole (hOut, pMsg, MsgLen, &Count, NULL)
/* Call WriteFile only if WriteConsole fails. */
&& !WriteFile (hOut, pMsg, MsgLen * sizeof (TCHAR),
&Count, NULL))
return FALSE;
}
va_end (pMsgList);
return TRUE;
}

BOOL PrintMsg( HANDLE hOut, LPCTSTR pMsg )
{//Single message version of printStrings
return PrintStrings( hOut, pMsg, NULL );
}

BOOL ConsolePrompt( LPCTSTR pPromptMsg, LPTSTR pRespone, DWORD MaxTchar, BOOL Echo )
{//Prompt user at console and get a respone
HANDLE hStdIn, hStdOut;
DWORD TcharIn, EchoFlag;
BOOL Success;

hStdIn= CreateFile( _T( "CONIN$" ), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
hStdOut= CreateFile( _T( "CONOUT$" ), GENERIC_WRITE, 0,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
EchoFlag= Echo ? ENABLE_ECHO_INPUT : 0;
Success=
SetConsoleMode( hStdIn, ENABLE_LINE_INPUT |
EchoFlag |
ENABLE_PROCESSED_OUTPUT )
&& SetConsoleMode( hStdOut, ENABLE_WRAP_AT_EOL_OUTPUT |
ENABLE_PROCESSED_OUTPUT )
&& PrintStrings( hStdOut, pPromptMsg, NULL )
&& ReadConsole ( hStdIn, pRespone, MaxTchar, &TcharIn, NULL );

if ( Success )
pRespone[ TcharIn - 2 ]= '\0';

CloseHandle( hStdIn );
CloseHandle( hStdOut );
return Success;
}
/////////////////////////////////////

int _tmain()
{
LPTSTR entry;
HANDLE hStdOut= CreateFile( _T( "CONOUT$" ), GENERIC_WRITE, 0,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );

ConsolePrompt( "Enter some: ", entry, 100, FALSE );
if ( entry == "majid" )
PrintStrings( hStdOut, "correct", NULL );
else
PrintStrings( hStdOut, "- incorrect", NULL );
return 0;
}

خواستم با ReadConsole از ورودی که با CreateFile باز شده بخونم و با یه اسم مقایسه کنم.
این قسمت مقایسه توی تابع _tmain و یه نگاه بکنید ؟

ممنون