WinAPI Question...

Baffled…not sure where to go from here…

Using the following windows api, lpBufferIn is invalid…I’ve tried passing it as a memory block and it does not work. So my question is why and what should be changed to make it work?

Function WriteConsole(hConsoleOutputIn As Integer, lpBufferIn As Ptr, nNumberOfCharsToWriteIn As Integer, lpNumberOfCharsWrittenIn As Integer, lpReservedIn As Ptr) As Integer
  Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" (hConsoleOutput As Integer, lpBuffer As Ptr, nNumberOfCharsToWrite As Integer, lpNumberOfCharsWritten As Integer, lpReserved As Ptr) As Integer

  Return WriteConsole( hConsoleOutputIn, lpBufferIn, nNumberOfCharsToWriteIn, lpNumberOfCharsWrittenIn, lpReservedIn )
End Function

//cWritten can be null... Call WriteConsole hConsoleOut, sInput, Len(sInput), cWritten, 0&

Hi Matthew,

how it is declared sInput?
You can try using a MemoryBlock for lpBufferIn and MemoryBlock.Size for nNumberOfCharsToWriteIn.

[quote=64872:@Maurizio Rossi]Hi Matthew,

how it is declared sInput?
You can try using a MemoryBlock for lpBufferIn and MemoryBlock.Size for nNumberOfCharsToWriteIn.[/quote]

I managed to get it to work using a memoryblock. Initially it caused a stackoverflowexception until I set the memoryblock to an initial buffer.

Now I can’t seem to get a buffered output from ReadConsole

Function ConsoleReadLine() As String
  
  'Create a buffer
  Dim cBuffer As String
  Dim ZeroPos As Integer = 0
  Dim ConsoleBuffer as MemoryBlock = ""
  Dim BufferRead as Integer = 256
  
  'Read the input
  Call ReadConsole hConsoleIn, ConsoleBuffer, ConsoleBuffer.Size, BufferRead, Nil
  'Strip off trailing EndOfLine/Chr(0)'s
  ZeroPos = InStr(ConsoleBuffer, Chr(0))
  If ZeroPos > 0 Then cBuffer = Left(ConsoleBuffer, ZeroPos - 3)
  
  Return cBuffer
  
End Function

Any help is always appreciated…

Note: In C++ and vb6, the BufferRead can be any value and seems to work (although in those languages I set it to 0/Nil since it’s not important to the API call directly in this case)