Thursday, May 01, 2008

Ruby and Win32 API

If you are using Ruby as an automation process rather than a development tool, you sometimes might want to use some of Windows' GUI controls, such as a message box or folder browser box. In those cases, you might want to try the DL library.

Some of you have read this post about OLE Automation and DL. In a way it is quite simple but it can take a while to get right, especially if you're a beginner like me. However, after some time I have managed to get my folder browser to work and I haven't found Ruby code on the web that does this yet.
However, I feel like it is rather simple to do and to find out how to make your own version, all you need to do is to see how people who wrote VB code do it.
Why? Because it is that straight forward, in fact, Ruby reduces the amount of lines of code to do it.

I must warn and say that doing this kind of programming requires excessive use of the Win32API docs (one form or another) and some skilled programming, especially in the C-language area, because you are interfacing to the actual C-functions from Ruby.

Many VB code snippets that does this, have referenced these 2 c-functions (from the shell32 DLL):
  • SHGetPathFromIDListA

  • SHBrowseForFolderA

They also require a structure to send data to the BrowseForFolder function.
Here is a site to get you started.

Why don't I just paste the code? Well, actually the method I wrote to bring up the dialog box, requires a whole Module and I would like to write my own Ruby module before I publish anything.
Yeah, I might sound like a bugger but hey, I have posted lots of lines of code on this site before so... take it like a man! ;)
The hardest part is creating a Ruby C-like structure from the VB structure, so I think I'll help you out on that one:

ptr = DL.malloc(DL.sizeof('LLSSLLLL'))
ptr.struct!('LLSSLLLL', :br_hOwner, :br_pidRoot, :br_displayName, :br_title,
:br_flags, :br_fn, :br_lparam, :br_iImage)

Now don't say I didn't give you anything! :D

For those who still don't know what all those L's and S's are for, it is basically saying "This is a Long integer type" or "This is a string type". Its to determine the size of the structure, but you know that there are other data-types so you need to dig in on your own regarding that.

Now there is one thing I'm having trouble with and it is the Open/Save File Dialog box. Aparently if you take the exact same data-types as the prescribed docs say, Ruby fails to bring it up. After hours of struggling, I decided to skip the VB code and look on the web if someone already did this, and to my "surprise", nobody has posted any code on this. Bummer! So I decided to have a look on the MSDN site, and BAM! I found my answer. The reason why Ruby didn't bring up my dialog box was due to the fact that Ruby created a structure that had a different size than what the function was expecting! So now I am trying to find out where, how and why.

I might post my code and findings at a later stage, for this is a necessary thing for Windows Ruby Automation Programmers, like me! ;)

No comments: