'How do I allocate a buffer with Ruby's Fiddle for use with Win32 functions such as SendMessage?

The function signature for SendMessageW as per MSDN:

LRESULT SendMessageW(
  [in] HWND   hWnd,
  [in] UINT   Msg,
  [in] WPARAM wParam,
  [in] LPARAM lParam
);

I wish to get the text in a window title/caption using the WM_GETTEXT Msg. The problem I am having is that LPARAM is a long int type, and using standard C, the char buffer could be cast to LPARAM, but using Ruby and Fiddle the calling convention is not so clear. I have tried:

captionText = String.new
User32::SendMessageW(hWnd, User32::WM_GETTEXT, bufLen, captionText) 

which gives an implicit conversion from string to int error, and:

User32::SendMessageW(hWnd, User32::WM_GETTEXT, bufLen, captionText.to_i)

Which appears to do nothing.

I already have the window handle and length of the text from earlier calls, e.g:

hWnd = User32::FindWindow(<class name of window>, <window title>)
bufLen = User32::SendMessageW(hWnd, User32::WM_GETTEXTLENGTH, 0, 0) + 1

I have defined the WinAPI functions in a manner similar to the examples here, and they work:

https://coder-question.com/cq-blog/432387?force_isolation=true

I am aware of the GetWindowText and GetWindowTextLength functions, however GetWindowTextLength is not working for child windows, so I've decided to try SendMessage instead.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source