Re: Open Default Mail Client

From: Rob Kennedy (.)
Date: 01/15/04


Date: Thu, 15 Jan 2004 16:16:08 -0600

Bryan Valencia wrote:
> procedure TMainForm.OpenDefaultEmailClient;
> var
> reg:TRegistry;
> DefaultMailClient, exeCommand:string;
> begin
> reg:=TRegistry.Create;
> try
> reg.RootKey:=HKEY_LOCAL_MACHINE;
> if reg.OpenKey('\SOFTWARE\Clients\Mail',false) then

Are you sure that will give you the current user's default e-mail
program? You can't get user-specific information from HKLM. Fetch the
current user's e-mail program from the same branch of HKCU, and then go
to HKLM to get the command line. Something like this:

RootKey := HKey_Current_User;
OpenKey('\Software\Clients\Mail', False);
DefaultMailClient := ReadString('');
RootKey := HKey_Local_Machine;
OpenKey(Format('\Software\Clients\Mail\%s\shell\open\command',
[DefaultMailClient]), False);
Command := ReadString('');

Also, use the OpenKeyReadOnly method instead of OpenKey. OpenKey will
fail if the user doesn't have write permissions in HKLM.

> begin
> DefaultMailClient := reg.ReadString('');
> reg.OpenKey(DefaultMailClient+'\shell\open\command',false);
> exeCommand := reg.ReadString('');
> exeCommand := Copy(exeCommand, Pos('"', exeCommand) + 1,
> Length(exeCommand));
> exeCommand := Copy(exeCommand, 1, Pos('"', exeCommand) - 1);

On my computer, you would be left with an empty string now. There are no
quotation marks in my command string, so Pos('"') returns 0, which means
you try to copy -1 characters.

Furthermore, by removing everything beyond the quotation mark (assuming
there is a quotation mark), you won't always get the e-mail program
anymore. Netscape and Mozilla both package the browser and the mailer in
the same program. They are differentiated by a command-line parameter.
With no parameters, the browser is started. Include the "-mail" argument
to start the mail program.

The "shell\open\command" value contains the *full* command line for
starting the mail program. You need to trust that value to be correct,
so just pass the whole string directly to ShellExecute or CreateProcess.
If the executable file name wasn't quoted but should have been, then the
API functions will probably figure it out for themselves. If they don't,
then it's not your problem; the mail program wasn't installed properly.

-- 
Rob


Relevant Pages

  • TIP #185: Null Handling
    ... nulls, and command modifications for manipulating them. ... Tcl deals with strings, the universal medium for representing data. ... is know and it is an empty string, but if a respondent forgets to give ...
    (comp.lang.tcl)
  • Re: Determine how Word was launched
    ... inspect the command line used to launch Word. ... Private Declare Function GetCommandLineA Lib "kernel32" As Long ... Public Function GetCommandLineAs String ... Dim lngCmdLinePtr As Long ...
    (microsoft.public.office.developer.vba)
  • Re: Class.getMethod in classs static initializer block
    ... I just wanted to add that I had some similar code for a "command line server" project. ... static private final boolean DEBUG = true; ... {public ReturnCode runCommand(String s) ... public ReturnCode parseLine(String line) ...
    (comp.lang.java.programmer)
  • Re: New "base document" available
    ... How to go about retrieving command line parameters? ... the command line arguments (and environment variables) ... Keyword and positional parameters can be mixed in the same command line. ... clp-name points to a string containing "P1" ...
    (comp.lang.cobol)
  • Re: How about UserRPL command to store "bare" text to SD?
    ... just as with Kermit transfers. ... a string directly to the SD card, verbatim, ... whose function is simply "store this literal string to that file" ... a more basic and universal "store this literal string as a file" command. ...
    (comp.sys.hp48)