AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

DllCall "SetConsoleCursorPosition" - Going to 0,0

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Slanter



Joined: 28 May 2008
Posts: 739
Location: Minnesota, USA

PostPosted: Fri Jan 02, 2009 1:50 am    Post subject: DllCall "SetConsoleCursorPosition" - Going to 0,0 Reply with quote

In the following script, I'm trying to use "SetConsoleCursorPosition" (Wrapped in MoveCursor()) to move the cursor around. The problem is, instead of setting the cursor to the correct coordinates, it always moves to 0,0 (Top left corner).
Code:
#Persistent
DllCall("AllocConsole")
hOutput := DllCall("GetStdHandle",UInt,-11)
DllCall("SetConsoleTitle",str,"Slanter's Console")
Write("SetConsoleCursorPosition hates me`n")
GetCursor(X,Y)
MsgBox Cursor is at %X%,%Y%
MoveCursor(2,2) ; Should move the cursor to 2,2
GetCursor(X,Y)
MsgBox Cursor is at %X%,%Y%
; This should be written starting at the second character of the second line. (Showing that GetCursor works correctly)
Write("Blah")

; ----------
; Functions
; ----------
Write(txt) {
   FileAppend, % txt, con
}
GetInput() {
   FileReadLine, txt, con, 1
   return txt
}
MoveCursor(X, Y) {
   Global hOutput
   VarSetCapacity(Coord, 4, 0)
   NumPut(Y, Coord, 0, "Short")
   NumPut(X, Coord, 2, "Short")
   If (!DllCall("SetConsoleCursorPosition", UInt, hOutput, UInt, Coord))
      MsgBox %ErrorLevel%`n%A_LastError%
}
GetCursor(ByRef X, ByRef Y) {
   Global hOutput
   VarSetCapacity(BufferInfo, 22, 0)
   DllCall("GetConsoleScreenBufferInfo", UInt, hOutput, UInt, &BufferInfo)
   X := NumGet(BufferInfo, 4, "Short")
   Y := NumGet(BufferInfo, 6, "Short")
}

I have tried all of the following:
Code:
DllCall("SetConsoleCursorPosition", UInt, hOutput, UInt, &Coord)
DllCall("SetConsoleCursorPosition", UInt, hOutput, Int, &Coord)
DllCall("SetConsoleCursorPosition", UInt, hOutput, Int, Coord)
DllCall("SetConsoleCursorPosition", UInt, hOutput, UInt64, Coord)
DllCall("SetConsoleCursorPosition", UInt, hOutput, Int64, Coord)
DllCall("SetConsoleCursorPosition", UInt, hOutput, UInt *, Coord)
DllCall("SetConsoleCursorPosition", UInt, hOutput, Int *, Coord)


Any idea what I'm doing wrong?
_________________
Unless otherwise stated, all code is untested

(\__/) This is Bunny.
(='.'=) Cut, copy, and paste bunny onto your sig.
(")_(") Help Bunny gain World Domination.
Back to top
View user's profile Send private message Visit poster's website
Lexikos



Joined: 17 Oct 2006
Posts: 4367
Location: Qld, Australia

PostPosted: Fri Jan 02, 2009 2:31 am    Post subject: Reply with quote

There are two problems.
  1. Edit: The following was caused by SciTE redirecting StdOut:
    GetStdHandle(-11) does not return a handle to a console output buffer. This is why SetConsoleCursorPosition sets A_LastError to ERROR_INVALID_HANDLE=6. Use the following instead:
    Code:
    hOutput := DllCall("CreateFile" ,"str","CONOUT$" ,"uint",0xC0000000 ,"uint",2 ,"uint",0 ,"uint",3 ,"uint",0 ,"uint",0)

  2. SetConsoleCursorPosition expects a COORD structure. What you have is almost correct, but for the "UInt" type DllCall expects a string of numeric characters representing an unsigned integer, not a binary number. Also, X and Y are around the wrong way. Use one of the following instead:
    Code:
       VarSetCapacity(Coord, 4, 0)
       NumPut(X, Coord, 0, "Short")
       NumPut(Y, Coord, 2, "Short")
       If (!DllCall("SetConsoleCursorPosition", UInt, hOutput, UInt, NumGet(Coord)))

    ; OR

       If (!DllCall("SetConsoleCursorPosition", UInt, hOutput, UInt, X | Y<<16))
Back to top
View user's profile Send private message Visit poster's website
Slanter



Joined: 28 May 2008
Posts: 739
Location: Minnesota, USA

PostPosted: Fri Jan 02, 2009 6:09 am    Post subject: Reply with quote

Great! Thanks! About the X and Y being switched, I think I did that to check if anything would happen (not really sure why I did that anymore tbh), then forgot to switch it back Embarassed.
_________________
Unless otherwise stated, all code is untested

(\__/) This is Bunny.
(='.'=) Cut, copy, and paste bunny onto your sig.
(")_(") Help Bunny gain World Domination.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group