Closing the Console

● ARCHIVED · READ-ONLY
Started by Maximus32 6 posts View original ↗
  1. Alright, so I'm trying to experiment with consoles in RPG Maker using Windows functions, of which my knowledge is limited...

    My code for creating a new console:

    Win32API.new('kernel32.dll', 'AllocConsole', '', '').callWin32API.new('kernel32.dll', 'SetConsoleTitle', 'P', '').call(title)$stdin.reopen('CONIN$')$stdout.reopen('CONOUT$')And after I create a console, I dispose of it by running the code below

    My code for closing it:

    Win32API.new('kernel32.dll', 'FreeConsole', '', '').callhandle = Win32API.new('kernel32.dll', 'GetConsoleWindow', '', '').callWin32API.new('user32.dll', 'DestroyWindow', 'P','').call(handle)The code compiles without error, but the console window does not close. I guess the problem might have something to do with the console not being a "Window" although I would think it is. Also, I'm not sure what the 3rd and 4th parameters for the Win32API.new method are supposed to be...

    Any help would be much appreciated.
  2. Hrm... when it attempts to compile $stdout.close('conout'), it tells me .close shouldn't take any arguments. The weird thing is that the same thing happens when I call the close method from within your game (DEK::Console, line 148). Maybe it's something with my computer? The Ruby API for the IO module doesn't say that .close takes parameters either. And yet, running $stdout.close() has no effect on the console window.

    By the way, it looks like a very nice game engine: lots of cool and advanced features. It must be fun to make!
  3. Nothing is wrong with your computer. Dekita just didn't read the documentation unlike you.

    To close a window, simply post the WM_CLOSE message instead of destroying it directly (read the remarks section of DestroyWindow). That's the same as clicking on X.

    The following code should work:

    hwnd = Win32API.new('kernel32', 'GetConsoleWindow', 'V', 'L').callWin32API.new('kernel32', 'FreeConsole', 'V', 'L').callmsg = 0x0010 # WM_CLOSEWin32API.new('user32', 'PostMessageW', 'LLLL', 'L').call(hwnd, msg, 0, 0)The 3rd argument are the types of the function's parameters and the 4th is the type of the function's return value. It's quite complicated and ugly actually, and Win32API is also deprecated. RGSS3 also (partially) provides dl, which is mightier and but not as easy to use and it also was removed from Ruby recently (replaced by Fiddle), but that's not important right now. Maybe for RGSS4 one day.A simple overview:

    Parameters (3rd argument):

    N, n, I, i, L, l: a number (Float, Fixnum, Bignum, etc.) converted to unsigned long will be passed

    V or v: nothing will be passed (= ignored)

    P or p: either 0 (for nil), the same as N, etc. (for a Fixnum), and else a pointer to the underlying char * of a Ruby string will be passed

    Return value (4th argument):

    N, n, I, i, L, l: a Fix- or Bignum converted from int will be returned

    V or v: a Fixnum with the value 0 will be returned

    P or p: a null-terminated (narrow character) string will be returned as Ruby string. So using any Win32 functions that returns a pointer to a wide character string, such as GetCommandLineW, can't be used with this ugly interface.
  4. lol, never noticed that as I had no reason to close the window. Just assumed that as the $stdout has a .close method that would close the console...

    How silly of me...
  5. Wohoo, it works! Thanks cremnophobia. Thanks also for the overview on those function parameters: it's interesting to see how much programming has changed from having to specify input and output types in older languages, to passing whatever parameters you like and having multiple return values in Ruby.