Jump to content

Help with _WinApi_CreateFile() Function and RAW mode


Recommended Posts

The latest Betas no longer support opening a file in RAW mode.

One of the neatest uses for this ability is to capture user input from the console in a CUI compiled script, by opening the console in raw mode, as per here:

http://www.autoitscript.com/forum/index.php?showtopic=79275&st=0&p=571658&#entry571658

Jon's reason for removal is here:

http://www.autoitscript.com/forum/index.php?showtopic=95366&view=findpost&p=761026

and I wouldn't want him adding back in code he wasn't happy with (or writing new code from scratch to support just one specialized feature).

The same functionality should be possible with _WinApi_CreateFile() function.

I've read the MSDN documentation on the function @ http://msdn.microsoft.com/en-us/library/aa363858%28VS.85%29.aspx

but I can't seem to get the parameters right... maybe I need _WinApi_ReadFile() too?

Looking for some assistance from the DllCall() gurus. And yes, I've read monoceres's tutorial on Dll calls, but that hasn't helped me in this instance.

Thanks

Link to comment
Share on other sites

I've done it ;) I've been pointing to Valiks example for ages but you never saw it.

#AutoIt3Wrapper_Change2CUI=y
#include <WinAPI.au3>

ConsoleWrite ("text: ")

Local $tBuffer = DllStructCreate("char")

Local $nRead, $sRet

Local $hFile = _WinAPI_CreateFile("CON", 2, 2)
While 1
    _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 1, $nRead)
    If DllStructGetData ($tBuffer, 1) = @CR Then ExitLoop
    $sRet &= DllStructGetData ($tBuffer, 1)
WEnd
_WinAPI_CloseHandle($hFile)

MsgBox(4096, "", $sRet)

Dibs making udf :evil:

Mat

Edit: Programmer friendly:

#AutoIt3Wrapper_Change2CUI=y
#include <WinAPI.au3>

ConsoleWrite("text: ")
$sText = _ConsoleReadLine()
MsgBox(0, "Result", $sText)

Func _ConsoleReadLine()
    If Not @Compiled Then Return SetError(1, 0, 0) ; Not compiled

    Local $tBuffer = DllStructCreate("char"), $nRead, $sRet = ""
    Local $hFile = _WinAPI_CreateFile("CON", 2, 2)

    While 1
        _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 1, $nRead)
        If DllStructGetData($tBuffer, 1) = @CR Then ExitLoop
        If $nRead > 0 Then $sRet &= DllStructGetData($tBuffer, 1)
    WEnd

    _WinAPI_CloseHandle($hFile)
    Return $sRet
EndFunc   ;==>_ConsoleReadLine
Edited by Mat
Link to comment
Share on other sites

I've done it ;) I've been pointing to Valiks example for ages but you never saw it.

Thank's Mat; that's what I wanted.

I did search forums (with built-in and Google) for _WinApi_CreateFile("con" but didn't find any direct results)

Do you have a link to Valik's example (I like to keep track of authors and seeds of origin where possible).

Also, slight mod of your function to make it "all-in-one", kinda like InputBox(): :evil:

;credits to Valik & Mat
#AutoIt3Wrapper_Change2CUI=y
#include <WinAPI.au3>

$sResponse = _ConsoleInput("Please input some text: ")
ConsoleWrite("You Entered: " & $sResponse & @CRLF)

Func _ConsoleInput($sPrompt)
    If Not @Compiled Then Return SetError(1, 0, 0) ; Not compiled

    ConsoleWrite($sPrompt)

    Local $tBuffer = DllStructCreate("char"), $nRead, $sRet = ""
    Local $hFile = _WinAPI_CreateFile("CON", 2, 2)

    While 1
        _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 1, $nRead)
        If DllStructGetData($tBuffer, 1) = @CR Then ExitLoop
        If $nRead > 0 Then $sRet &= DllStructGetData($tBuffer, 1)
    WEnd

    _WinAPI_CloseHandle($hFile)
    Return $sRet
EndFunc
Link to comment
Share on other sites

I've done it ;) I've been pointing to Valiks example for ages but you never saw it.

http://www.autoitscript.com/forum/index.php?showtopic=107752&view=findpost&p=760049

Thats the link to Valiks original post.

So Valik's original post was on the 5th of January, today's the 8th, and you've been pointing to his example for ages? :evil:

Link to comment
Share on other sites

So Valik's original post was on the 5th of January, today's the 8th, and you've been pointing to his example for ages? ;)

He pointed to it hours ago in the Latest Beta thread. That qualifies as "ages" in my book.
Link to comment
Share on other sites

  • 10 months later...

Is there anyway to use this method and gather a single keystroke without the user having to hit the enter key.

I'm wanting to emulate the functionality of the choice.com program which does not work on a 64bit system.

With choice.com you can write to console a message and wait for a single key to be pressed and exit with that key as the error code. On 32 bit systems I'm able to use choice.com in a batch file to get a single keystroke from the user.

Has anyone seen an autoit3 script of that nature on this forum?

Link to comment
Share on other sites

  • 3 years later...

Sorry for reviving so old thread, but does someone know why this piece of code doesn't work properly on Windows 8 / 8.1? You have to press ENTER twice and only second string is returned. The same compiled program run on Windows 7 works great.

Link to comment
Share on other sites

You have to turn off the ENABLE_LINE_INPUT in order for the console read functions to return immediately.

Example: 

#AutoIt3Wrapper_Change2CUI=n

#include <WinAPI.au3>
#include "Console.au3"

_Console_Alloc()

_WinAPI_SetLastError(0)
_Console_ReadOutputCharacter(-1, 1, 1, 1)
If _WinAPI_GetLastError() = 6 Then
    Run("""" & @AutoItExe & """ /AutoIt3ExecuteScript """ & @ScriptFullPath & """ " & $CmdLineRaw, @WorkingDir, @SW_SHOW, 0)
    Exit
EndIf

_Console_Write("Test: ")
$sText = _Console_ReadConsole(-1, False)
MsgBox(0, "Result", $sText)

Killgore, as mentioned in the pm, the above code will work for you too, just change _Console_ReadConsole(-1, False) to _Console_ReadConsole() to read the whole line.

Edit: It seems like this thread is getting hijacked a little. It's about time I just made a thread for console.au3 on it's own. Maybe renaming it to WinAPIConsole.au3 to avoid confusion with this thread. there are still a lot of things I don't understand and need fixing but most of the answers are there now.

Edited by Mat
Link to comment
Share on other sites

  • 8 months later...

Hi-

In response to Kilgores question about having to enter twice with Windows 8, where do you turn off "ENABLE_LINE_INPUT"? I'm using the Valiks code example to read the input from the console and doing simple validation on the return.

#AutoIt3Wrapper_Change2CUI=y
#include <WinAPI.au3>
#include <StringConstants.au3>
#include <console.au3>

_GetServiceInfoCMD()

Func _ConsoleInput($sPrompt)
    If Not @Compiled Then Return SetError(1, 0, 0) ; Not compiled

    ConsoleWrite($sPrompt)

    Local $tBuffer = DllStructCreate("char"), $nRead, $sRet = ""
    Local $hFile = _WinAPI_CreateFile("CON", 2, 2)

    While 1
        _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 1, $nRead)
        If DllStructGetData($tBuffer, 1) = @CR Then ExitLoop
        If $nRead > 0 Then $sRet &= DllStructGetData($tBuffer, 1)
    WEnd
    _WinAPI_CloseHandle($hFile)
    Return $sRet
EndFunc


Func _GetServiceInfoCMD()
    Local $sCustomerID = _ConsoleInput("Please enter your Customer ID: ")
    Local $iDcount = StringLen($sCustomerID)
    Local $iIsNumber = StringIsDigit($sCustomerID)
    While $iIsNumber <> 1
        ConsoleWrite( $sCustomerID & " is not a valid entry, numbers are only allowed." &  @CRLF)
        $sCustomerID = _ConsoleInput("Please enter again: ")
        $iIsNumber = StringIsDigit($sCustomerID)
        ConsoleWrite("IsNumber: " &  $iIsNumber & @CRLF)
    WEnd
    While $iDcount <> 7
        ConsoleWrite("Incorrect amount of numbers, only 7 are allowed." & @CRLF)
        $sCustomerID = _ConsoleInput("Please enter again: ")
        $iDcount = StringLen($sCustomerID)
        ConsoleWrite("Count: " &  $iDcount & @CRLF)
    WEnd
EndFunc
Link to comment
Share on other sites

  • 4 years later...

InputBox()

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...