Microsoft KB Archive/268884

= How To Set or Retrieve the Authentication Level of a DCOM Client =

Article ID: 268884

Article Last Modified on 8/30/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q268884



SUMMARY
If you need to set a custom value for the Authentication Level for a DCOM client application, you can use one of the following options:
 * Call CoInitializeSecurity.
 * Create an AppId in the registry that has a double word (DWORD) value set for AuthenticationLevel and map this AppId to the executable file name of your DCOM client.

This article uses the second option.



MORE INFORMATION
DCOM security settings are process-specific. If you do not set the AuthenticationLevel of the client's process, then the default AuthenticationLevel is used. Because you do not use CoInitilizeSecurity from your client to set the AuthenticationLevel and your client executable is not included in the Dcomcnfg utility, you must create your own registry mapping in order to set the client's authentication level either programmatically or by using Regedit.

Do the mapping in the registry by starting under:

HKEY_CLASSES_ROOT\AppId\

and then adding a key with the name of your client's executable name (myclient.exe). In this key there should be a string value, AppId, with your generated GUID. You also need to have a key under:

HKEY_CLASSES_ROOT\AppId\

with your generated GUID. In this key there should be a DWORD value, AuthenticationLevel, which is set to the value of the authentication level you want the client to use. This completes the registry mapping for the client's executable.

Assuming that your client executable is named myclient.exe, the mapping should look like the following in the registry.

The following is sample code for an application that creates the preceding registry entries for a given executable:  Start a Standard EXE Project in Visual Basic. Form1 is created by default. Add two TextBox, two Label, and two CommandButton controls to the form.  Add the following code to the form: Private Sub Command1_Click If Text1 = &quot;&quot; Or Val(Text2) < 1 Or Val(Text2) > 6 Then Exit Sub SetAuthentication Val(Text2), Text1 End Sub

Private Sub Command2_Click Dim auth As Integer auth = GetAuthenticationLevel(Text1) MsgBox &quot;The Authentication Level for: &quot; & Text1 & &quot; is &quot; & auth End Sub

Private Sub Form_Load With Me       .Width = 3225 .Height = 1950 .Caption = &quot;Set DCOM Client Authentication Level&quot; End With With Command1 .Height = 615 .Width = 1450 .Left = 1650 .Top = 120 .Caption = &quot;Set Authentication Level&quot; .TabIndex = 2 End With With Command2 .Height = 615 .Width = 1450 .Left = 1650 .Top = 840 .Caption = &quot;Get Authentication Level&quot; .TabIndex = 2 End With With Text1 .Text = &quot;&quot; .Height = 285 .Left = 0 .Top = 360 .Width = 1455 .TabIndex = 0 End With With Text2 .Text = &quot;&quot; .Height = 285 .Left = 0 .Top = 1200 .Width = 255 .TabIndex = 1 End With With Label1 .Caption = &quot;Client EXE name&quot; .Left = 0 .Top = 0 End With With Label2 .Caption = &quot;Authentication Level (1-6)&quot; .Left = 0 .Top = 720 End With End Sub   On the Project menu, add a standard module to the project, and then paste the following code in the code module: Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(7) As Byte End Type

Private Const GUID_LENGTH As Long = 39

Private Const REG_SZ As Long = 1 Private Const REG_DWORD As Long = 4

Private Const ERROR_SUCCESS = 0 Private Const ERROR_NONE = 0

Private Const HKEY_CLASSES_ROOT = &H80000000

Private Const KEY_ALL_ACCESS = &H3F Private Const KEY_QUERY_VALUE = &H1

Private Const REG_OPTION_NON_VOLATILE = 0

Private Declare Function RegCloseKey Lib &quot;advapi32.dll&quot; _ (ByVal hKey As Long) As Long

Private Declare Function RegCreateKeyEx Lib &quot;advapi32.dll&quot; Alias _ &quot;RegCreateKeyExA&quot; (ByVal hKey As Long, ByVal lpSubKey As String, _ ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions _ As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes _ As Long, phkResult As Long, lpdwDisposition As Long) As Long

Private Declare Function RegSetValueExString Lib &quot;advapi32.dll&quot; Alias _ &quot;RegSetValueExA&quot; (ByVal hKey As Long, ByVal lpValueName As String, _ ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As _ String, ByVal cbData As Long) As Long

Private Declare Function RegSetValueExLong Lib &quot;advapi32.dll&quot; Alias _ &quot;RegSetValueExA&quot; (ByVal hKey As Long, ByVal lpValueName As String, _ ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, _ ByVal cbData As Long) As Long

Private Declare Function RegOpenKeyEx Lib &quot;advapi32.dll&quot; Alias _ &quot;RegOpenKeyExA&quot; (ByVal hKey As Long, ByVal lpSubKey As String, _ ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) _ As Long

Private Declare Function RegQueryValue Lib &quot;advapi32.dll&quot; Alias _ &quot;RegQueryValueA&quot; (ByVal hKey As Long, ByVal lpSubKey As String, _ ByVal lpValue As String, lpcbValue As Long) As Long

Private Declare Function RegQueryValueExString Lib &quot;advapi32.dll&quot; _ Alias &quot;RegQueryValueExA&quot; (ByVal hKey As Long, ByVal lpValueName As String, _ ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, _ lpcbData As Long) As Long

Private Declare Function RegQueryValueExLong Lib &quot;advapi32.dll&quot; Alias _ &quot;RegQueryValueExA&quot; (ByVal hKey As Long, ByVal lpValueName As _ String, ByVal lpReserved As Long, lpType As Long, lpData As _ Long, lpcbData As Long) As Long Private Declare Function CoCreateGuid Lib &quot;ole32.dll&quot; (pGUID As GUID) _ As Long

Private Declare Function StringFromGUID2 Lib &quot;ole32.dll&quot; (pGUID As GUID, _ ByVal PointerToString As Long, ByVal MaxLength As Long) As Long

Public Function CreateGUID As String Dim udtGUID As GUID Dim sGUID As String Dim lResult As Long If (CoCreateGuid(udtGUID) = 0) Then sGUID = String$(GUID_LENGTH, 0) lResult = StringFromGUID2(udtGUID, StrPtr(sGUID), GUID_LENGTH) CreateGUID = sGUID End If End Function

Public Sub SetAuthentication(AuthLevel As Integer, ExeName As String) Dim hNewKey As Long Dim lRetVal As Long Dim sMyGUID As String Dim sNewKey As String Dim lpData As Long Dim lpcbData As Long lpData = CLng(AuthLevel) lpcbData = Len(lpData) sMyGUID = CreateGUID sNewKey = &quot;AppId\&quot; & sMyGUID lRetVal = RegCreateKeyEx(HKEY_CLASSES_ROOT, sNewKey, 0&, _      vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, _       hNewKey, lRetVal) lRetVal = RegSetValueExLong(hNewKey, &quot;AuthenticationLevel&quot;, 0&, _      REG_DWORD, lpData, lpcbData) RegCloseKey (hNewKey) sNewKey = &quot;AppId\&quot; & ExeName & &quot;.exe&quot; lRetVal = RegCreateKeyEx(HKEY_CLASSES_ROOT, sNewKey, 0&, _      vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, _       hNewKey, lRetVal) lRetVal = RegSetValueExString(hNewKey, &quot;AppId&quot;, 0&, REG_SZ, _      sMyGUID, Len(sMyGUID)) RegCloseKey (hNewKey) MsgBox sMyGUID End Sub

Public Function GetAuthenticationLevel(ExeName As String) As Integer Dim hKey As Long Dim lRetVal As Long Dim sKeyName As String Dim lpData As String Dim lpdata2 As Long Dim lpcbData As Long Dim myappid As String Dim auth As Integer If ExeName = &quot;&quot; Then GetAuthenticationLevel = -1 MsgBox &quot;Invalid File Name&quot; Exit Function End If   lRetVal = RegOpenKeyEx(HKEY_CLASSES_ROOT, &quot;AppId\&quot; & ExeName, 0, _       KEY_QUERY_VALUE, hKey) If lRetVal = ERROR_SUCCESS Then lpcbData = 40 lpData = Space$(lpcbData) lRetVal = RegQueryValueExString(hKey, &quot;AppId&quot;, 0&, REG_SZ, _          lpData, lpcbData) If lRetVal = ERROR_NONE Then myappid = Left$(lpData, lpcbData - 1) RegCloseKey (hKey) sKeyName = &quot;AppID\&quot; & myappid & &quot;\&quot; lRetVal = RegOpenKeyEx(HKEY_CLASSES_ROOT, sKeyName, 0&, _           KEY_ALL_ACCESS, hKey) If lRetVal = ERROR_SUCCESS Then lpdata2 = CLng(0) lpcbData = Len(lpdata2) lRetVal = RegQueryValueExLong(hKey, _                  &quot;AuthenticationLevel&quot;, 0&, REG_DWORD, lpdata2, lpcbData) If lRetVal = ERROR_NONE Then auth = CInt(lpdata2) GetAuthenticationLevel = auth Else MsgBox lRetVal & &quot; - Unable to read authentication level.&quot; GetAuthenticationLevel = -2 End If           Else MsgBox lRetVal & &quot; - Cannot find AppID for &quot; & sKeyName GetAuthenticationLevel = -3 End If           RegCloseKey (hKey) Else MsgBox lRetVal & &quot; - Cannot read AppID value for &quot; & sKeyName GetAuthenticationLevel = -4 End If   Else MsgBox lRetVal & &quot; - Cannot find exe name - &quot; & sKeyName GetAuthenticationLevel = -5 End If

Exit Function QueryValueExExit: MsgBox lRetVal & &quot; - Unexpected error&quot; GetAuthenticationLevel = -7 Exit Function QueryValueExError: Resume QueryValueExExit End Function  Run the project. Type the executable file name of the client application into Text1, and then type the authentication level you want to set into Text2. Click the Set Authentication Level CommandButton. The mapping is now created in the registry and the authentication level is set for the client executable. Click the Get Authentication Level CommandButton and note that a message box appears containing the authentication level you just set.</ol>

<div class="references_section">