Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Windows API can't open simple window #1140

@DeVic256

Description

@DeVic256

I need to use Win32 API and most functions work. MessageBox opens a dialog, and functions return "expected values". However, with the code together to open a simple window doesn't work. It gets stuck without doing nothing and I suspect it might be a pointer thing but I already typedefed "types that work" on my attached functions.

require './win32_ffi'

class Window
  include Win32

  def initialize
    @wclass_name = FFI::MemoryPointer.from_string('WindowClass')
    @wname = FFI::MemoryPointer.from_string('WindowTitle')

    @black = GetStockObject(4)
    @white = GetStockObject(0)

    @hInstance = GetModuleHandleA(nil)

    @wc = WNDCLASS.new
    @wc[:style] = 0
    @wc[:hIcon] = 0
    @wc[:cbClsExtra] = 0
    @wc[:cbWndExtra] = 0
    @wc[:lpszClassName] = @wclass_name.to_ptr
    @wc[:lpszMenuName] = @wname.to_ptr
    @wc[:hbrBackground] = @white
    @wc[:hCursor] = LoadCursorA(nil, 32512)
    @wc[:hInstance] = @hInstance
    @wc[:lpfnWndProc] = method(:WindowProc)
    
    @hwMain = CreateWindowExA(0, @wclass_name, @wname, 0x10CF0000, 100, 100, 500, 500, 0, 0, 0, nil)

    raise StandardError.new 'Unable to create a Window' if !RegisterClassA(@wc.to_ptr)
    raise StandardError.new 'Unable to create a Window' if @hwMain.nil?

    get_message
  end

  def WindowProc(hwnd, msg, wp, lp)
    case msg
    when 0x0001
      p 'Window created'
    when 0x0002
      PostQuitMessage(0)
    else
      return DefWindowProcA(hwnd, msg, wp, lp)
    end

    return 0
  end

  def get_message
    msg = MSG.new
    msg[:hwnd] = 0
    msg[:message] = 0
    msg[:wparam] = 0
    msg[:lparam] = 0
    msg[:time] = 0
    msg[:pt] = 0

    while GetMessageA(msg.to_ptr, 0, 0, 0) != 0
      TranslateMessageA(msg.to_ptr)
      DispatchMessageA(msg.to_ptr)
    end
  end
end

w = Window.new(title: 'WindowCustomTitle')

Nothing seems to work to make that window open

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions