As you may have noticed, I have been exploring many of the features of the Win32API module, but I am utterly stumped on something.
What I am trying to do is to find a way to retrieve data off the clipboard and set as a bitmap object in Ruby, but without the use of an external DLL. Basically it can do the same thing as the Screenshot.dll (I already have methods to have a bitmap object save to a PNG file if needed). This would be no problem in later versions of Ruby with the WINOLE module to pass the clipboard data to MSPaint or whatever, have it save, then load it normally, but I can't seem to figure out how exactly to read the data I get properly. Here's what I got for creating the image:
Keybd_Event = Win32API.new('user32', 'keybd_event', 'LLLL', '')
def capture
Keybd_Event.call(0x12, 0, 0, 0) # ALT Down
Keybd_Event.call(0x2C, 0, 0, 0) # PRINT SCREEN Down
Keybd_Event.call(0x2C, 0, 2, 0) # PRINT SCREEN Up
Keybd_Event.call(0x12, 0, 2, 0) # ALT Up
end
This is the same as ALT + Print Screen being pressed, which sets a bitmap image to the clipboard.
Now, for reading it, this is what I got so far:
OpenClipboard = Win32API.new('user32', 'OpenClipboard', 'L', 'L')
GetClipboardData = Win32API.new('user32', 'GetClipboardData', 'L', 'L')
CloseClipboard = Win32API.new('user32', 'CloseClipboard', '', 'L')
# Pretty sure I will need these, but not sure yet how to use properly.
GlobalAlloc = Win32API.new('kernel32', 'GlobalAlloc', 'LL', 'L')
GlobalLock = Win32API.new('kernel32', 'GlobalLock', 'L', 'L')
GlobalSize = Win32API.new('kernel32', 'GlobalSize', 'L', 'L')
GlobalUnlock = Win32API.new('kernel32', 'GlobalUnlock', 'L', '')
Memcpy = Win32API.new('ntdll', 'memcpy', 'PPL', 'L')
#Memcpy = Win32API.new('crtdll', 'memcpy', 'PPL', 'L')
def read
OpenClipboard.call(0)
image = GetClipboardData.call(8)
CloseClipboard.call
# What should go here?
end
There is two major things I am unsure of.
- Why does 8 return data in the GetClipboardData call and everything else returns 0 when 1 or 2 are the formats that should work with a bitmap.
- Either way, when called with 8, it returns a large string of binary data, but I am unsure as to how to pack it, or whatever needs done with it in order to convert it to a bitmap.
Any help that could be offered in this would be much appreciated.