PRB: RegCreateKeyEx() Gives Error 161 Under Windows NT

Last reviewed: May 15, 1997
Article ID: Q117261
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with: - Microsoft Windows NT version 3.5, 3.51, 4.0 - Microsoft Windows 95

SYMPTOMS

A call to RegCreateKeyEx() is successful under Windows NT version 3.1 and Windows 95, but the call fails with error 161 (ERROR_BAD_PATHNAME) under Windows NT version 3.5 and later.

The sample code below demonstrates this problem.

CAUSE

This is by design. Windows NT version 3.1 and Windows 95 allow the subkey to begin with a backslash ("\"), however Windows NT version 3.5 and later do not. The subkey is given as the second parameter to RegCreateKeyEx().

RESOLUTION

Remove the backslash from the beginning of the subkey name.

MORE INFORMATION

In the sample code below, RegCreateKeyEx() fails with error 161 while the string defined by SUBKEY_FORMAT_STRING begins with a backslash, but succeeds if the initial backslash is removed.

Sample Code

   #include <windows.h>
   #include <stdio.h>

   #define SUBKEY_FORMAT_STRING \
   "\\SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s"

   void main(int argc, char *argv[])
   {
      DWORD dwErrorCode;
      char lpszSubKey[MAX_PATH];
      HKEY hKey;
      DWORD dwDisposition;

      sprintf( lpszSubKey, SUBKEY_FORMAT_STRING, argv[1] );

      printf( "Trying to open: %s\n", lpszSubKey );

      dwErrorCode = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
                                   lpszSubKey,
                                   0,
                                   "",
                                   REG_OPTION_NON_VOLATILE,
                                   KEY_ALL_ACCESS,
                                   NULL, //Security
                                   &hKey,
                                   &dwDisposition );

      if (dwErrorCode != ERROR_SUCCESS)
         printf( "Code = %d.\n", dwErrorCode );

      RegCloseKey(hKey);
   }

NOTE: Double backslashes ("\\") are required in strings in C code to represent a single backslash, since a backslash ordinarily indicates the beginning of an escape sequence.


Keywords : BseMisc BseRegistry kbprg
Version : 3.5 3.51 4.0
Platform : NT Win95 WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: May 15, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.