Someone with Ruby/Win32API knowledge needed!

Started by ForeverZer0, December 04, 2010, 08:38:40 pm

Previous topic - Next topic

ForeverZer0

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.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Blizzard

http://msdn.microsoft.com/en-us/library/ms649039(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ff729168(v=VS.85).aspx
http://msdn.microsoft.com/en-us/library/dd183375(v=VS.85).aspx

You probably have separate the array into two blocks, one of them being the bitmapdata structure (which you can most probably just get rid of, but probably you will have to analyze it to get the format data of the bitmap) and one being the actual data.
Look up String#pack and String#unpack to learn how to convert the data.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

ForeverZer0

Beautiful. Thank you!

I don't know why I couldn't get them to pop up on a google search...
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Blizzard

I put in specifically GetClipboardData and then I clicked a few links on the page to gather those few links.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

ForeverZer0

I kept searching the same thing, but including the word "ruby" in there.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Blizzard

Yeah, you probably shouldn't have done that. xD If you want WinAPI functions, just type them into Google without anything. Among the first links are usually the MSDN documentation pages for the function you are looking for.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.