Monday, July 2, 2007

Querying the Windows Registry for the Default Mail Client

Following up on my post about sending email with MS Outlook, a reader asks, "I wonder how one can mine out the chosen default mail client... Is that somewhere in the Registry?"

It does indeed appear to be in the Windows Registry, and can therefore be extracted using the win32/registry library. I've just started hacking around with this library, but this bit of code seems to do the trick...


require 'win32/registry'

def get_mail_client
Win32::Registry::HKEY_LOCAL_MACHINE.open('Software\Clients\Mail') do |reg|
reg_typ, reg_val = reg.read('')
return reg_val
end
end

mail_client = get_mail_client

...though I am sure it can be improved upon.

On my machine, this returns 'Microsoft Outlook'.

Well, there you have it. As always, post comments or send email with enhancements, questions, or suggestions for future topics.

Thanks for stopping by!


Digg my article

8 comments:

Anonymous said...

Is there an explanation somewhere on how to read the documentation for Win32::Registry? How would I know that the read method returns two values from looking at the reference
at http://stdlib.rubyonrails.org/libdoc/Win32API/rdoc/index.html?

David Mullet said...

A very good question! It would be nice to have this documented better, but there are a few ways to determine that the read method returns a 2-element array.

One way is to take advantage of the fact that this is open source. If you click on the [Source] link on the rdoc page you referenced, you can view the source code for the read method and see that it returns (implicitly) a two-element array.

Another way is to simply try it out. This...

Win32::Registry::HKEY_LOCAL_MACHINE.open('Software\Clients\Mail') do |reg|
puts reg.read('').class
puts reg.read('').size
end

...returns this:

Array
2

I hope that helps!

David

dheer said...

If i want to close the new instance of outlook window by using ruby language code, what is the way of doing this. Can someone suggest the work around or code regarding the same.

Cheers!
Dheeraj

David Mullet said...

@Dheeraj:

To quit Outlook, call the Outlook Application object's Quit method:

outlook.Quit

David

Majix560 said...
This comment has been removed by the author.
Majix560 said...

Could you please provide easy to use functions to read and write to/from Windows Registry. And is it possible to execute DLL functions using Ruby? If so, could you please provide a function to do so?

And also could you provide a link to where I can get all of the ruby libraries required to run these functions? I ask this because I am wanting to use them in my RPGMakerVX games, and I don't know what libraries are included in its built Ruby runtime environment.

Rob said...

If you already know the type of the registry item you're going to retrieve, something like this reads a little better:

Win32::Registry::HKEY_LOCAL_MACHINE.open('Software\Clients\Mail') { |reg| reg.read_s('') }

read_s instance method reads a string value, read_bin a binary, etc.

Anonymous said...

How do we close HKEY if we need another instance of hkey open.

Ex: Win32::Registry::HKEY_LOCAL_MACHINE.open(REG_PATH_1,Win32::Registry::Constants::KEY_ALL_ACCESS) do |reg|
if (reg.read_i(name)!="null")
return reg.read_i(name)
end
end
Win32::Registry::HKEY_LOCAL_MACHINE.close()
Win32::Registry::HKEY_LOCAL_MACHINE.open(REG_PATH_2,Win32::Registry::Constants::KEY_ALL_ACCESS) do |reg|
return reg.read_i(name)
end