Function GetDefaultEmailClient()

B

Bill

In case anyone was following the "email client name re-visited"
thread, below is the essentially finished product as derived from
the API RegEnumValue described at www.allapi.net
=============================================
Option Compare Database
Option Explicit

Const ERROR_NO_MORE_ITEMS = 259&
Const HKEY_LOCAL_MACHINE = &H80000002
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long)
As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA"
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias
"RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal
lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long,
lpType As Long, lpData As Any, lpcbData As Long) As Long
Public Function GetDefaultEmailClient() As String
'===============================================================================================================
' The GetDefaultEmailClient function does just that. The Windows Registry
Key where the default e-mail client
' name is stored is opened. And, each value is read until the entry is found
wherein the value name is a zero-
' length string, which is the default value. The function then returns the
name of the "default e-mail client".
'===============================================================================================================
Dim hKey As Long 'The currently opened Windows Registry Key
Dim Cnt As Long 'The current value index, incremented for
each RegEnumValue
Dim sName As String 'RegEnumValue returns the value name here.
Dim Ret As Long 'RegEnumValue returns the length of the
value name here.
Dim sData As String 'RegEnumValue returns the value string
here.
Dim RetData As Long 'RegEnumValue returns the length of the
value, which includes the null trailer char.

Dim rc As Long 'Incidental to the use of MsgBox if
there's an error.

Const BUFFER_SIZE As Long = 255

'Open the registry key containing the name of the default E-Mail client.

If RegOpenKey(HKEY_LOCAL_MACHINE, "Software\Clients\Mail", hKey) = 0
Then
'initialize the buffers
sName = Space(BUFFER_SIZE)
sData = Space(BUFFER_SIZE)
Ret = BUFFER_SIZE
RetData = BUFFER_SIZE

GetDefaultEmailClient = "" 'Set to zero-length string if
default value not found.

Cnt = 0 'Initialize index to first value in
current key
While RegEnumValue(hKey, Cnt, sName, Ret, 0, ByVal 0&, ByVal sData,
RetData) <> ERROR_NO_MORE_ITEMS _
And GetDefaultEmailClient = ""
If RetData > 0 Then
If Len(Left$(sName, Ret)) = 0 Then GetDefaultEmailClient =
Left$(sData, RetData - 1) 'GOT IT!!!!
End If

'Current value name something other than the default, prepare
buffers for next value
Cnt = Cnt + 1
sName = Space(BUFFER_SIZE)
sData = Space(BUFFER_SIZE)
Ret = BUFFER_SIZE
RetData = BUFFER_SIZE
Wend

'Close the registry key
RegCloseKey hKey
Else
rc = MsgBox("Error in while calling RegOpenKey", vbCritical,
"FUNCTION: GetDefaultEmailClient")
End If

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top