⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rasreg.c

📁 WinCE5.0部分核心源码
💻 C
📖 第 1 页 / 共 5 页
字号:
                        ASSERT(hRes != ERROR_SUCCESS);
                        DEBUGMSG (ZONE_RAS | ZONE_ERROR,
                          (TEXT(" AfdRasSetEntryProperties: ERROR %d from CryptProtectData\n"), hRes));
                    }
                }
                if (ERROR_SUCCESS == hRes)
                    hRes = RegSetValueEx(hKey, RASBOOK_DEVCFG_VALUE, 0, REG_BINARY, lpb, dwSize);
                if (ERROR_SUCCESS != hRes)
                {
                    DEBUGMSG (ZONE_RAS | ZONE_ERROR,
                              (TEXT(" AfdRasSetEntryProperties: ERROR %d from RegSetValueEx\n"), hRes));
                    RetVal = ERROR_CANNOT_OPEN_PHONEBOOK;
                }
                // CryptProtectData allocates a buffer
                if (dataOut.pbData)
                    LocalFree(dataOut.pbData);
            }

            RegCloseKey (hKey);
        }
    }
                       
    DEBUGMSG (ZONE_RAS | (ZONE_ERROR && RetVal != ERROR_SUCCESS),
              (TEXT("-AfdRasSetEntryProperties Result=%d\n"), RetVal));
    return RetVal;
}


/*****************************************************************************
*
*
*   @func   DWORD   |   RasGetEntryProperties |
*       The RasGetEntryProperties function retrieves the properties
*       of a phonebook entry.
*
*   @rdesc
*       If the function succeeds, the return value is zero.
*       <nl>If the function fails, the return value can be one of the
*       following error codes: <nl>
*       @ecode  ERROR_INVALID_PARAMETER             |
*           The function is called with an invalid parameter.
*       @ecode  ERROR_BUFFER_INVALID                |
*           The address or buffer specified by lpbEntryInfo is invalid.
*       @ecode  ERROR_BUFFER_TOO_SMALL              |
*           The buffer size indicated in lpdwEntryInfoSize is too small.
*       @ecode  ERROR_CANNOT_OPEN_PHONEBOOK         |
*           The phonebook is corrupted or missing components.
*       @ecode  ERROR_CANNOT_FIND_PHONEBOOK_ENTRY   |
*           The phonebook entry does not exist.
*
*   @parm   LPTSTR  |   lpszPhonebook   |
*       Points to a null terminated string that specifies the full path and
*       filename of the phonebook file.  This parameter can be NULL, in which
*       case the default phonebook file is used.  This parameter should always
*       be NULL for Windows 95 and WinCE since the phonebook entries are
*       stored in the registry.
*   @parm   LPTSTR  |   szEntry         |
*       Points to a null terminated string containing an existing entry name.
*       If a "" entry name is specified, structures containing default
*       values are returned.
*   @parm   LPBYTE  |   lpbEntry        |
*       Points to a RASENTRY structure that receives the connection data
*       associated with the phonebook entry specified by the lpszEntry member
*       followed by any additional bytes needed for the alternate phone number
*       list if any.  On entry, the dwSize member of this structure must
*       specify the size of the structure.
*   @parm   LPDWORD |   lpdwEntrySize   |
*       Points to a variable that contains the size, in bytes, of the buffer
*       specified by lpEntryInfo.  On return, the function sets this variable
*       to the number of bytes required.  This parameter can be NULL if the
*       information is not required.  The recommended method for determining
*       the required buffer size is to call RasGetEntryProperties with the
*       lpbEntryInfo set to null and the DWORD pointed to by lpdwEntryInfoSize
*       set to zero. The function will return the required buffer size in the
*       DWORD.
*   @parm   LPBYTE  |   lpb             |
*       Points to buffer to receive device-specific configuration information.
*       This parameter may be NULL if the data is not required.  The
*       recommended method for determining the required buffer size is to call
*       RasGetEntryProperties with the lpbDeviceInfo set to null and the DWORD
*       pointed to by lpdwDeviceInfoSize set to zero. The function will return
*       the required buffer size in the DWORD.
*   @parm   LPDWORD |   lpdwSize        |
*       Points to a variable that contains the size, in bytes, of the buffer
*       specified by lpb. On return, the function sets this variable to the
*       number of bytes required. This parameter can be NULL if the
*       information is not required.
*
*   @ex     An example of how to use this function follows |
*           No Example
*
*/
DWORD APIENTRY
AfdRasGetEntryProperties(
    LPTSTR  lpszPhonebook,
    LPTSTR  szEntry,
    LPBYTE  lpbEntry,
    LPDWORD lpdwEntrySize,
    LPBYTE  lpb,
    LPDWORD lpdwSize)
{
    HKEY    hKey;
    LONG    hRes;
    DWORD   RetVal = 0;
    DWORD   dwType;
    DWORD   dwSize;
    LPRASENTRY  pEntry;
    RASPENTRY   RaspEntry;  // Private version of structure.
    
    DEBUGMSG( ZONE_RAS | ZONE_FUNCTION,
             (TEXT("+AfdRasGetEntryProperties(0x%08X(%s), 0x%0X(%s), ")
              TEXT("0x%X, 0x%X, 0x%X, 0x%X)\r\n"), 
              lpszPhonebook,
              (lpszPhonebook != NULL) ? lpszPhonebook : TEXT("NULL"),
              szEntry,
              (szEntry != NULL) ? szEntry : TEXT("NULL"),
              lpbEntry, lpdwEntrySize, lpb,
              lpdwSize) );

	// If a RASENTRY structure is specified then the size of it must also be specified.
	if (lpbEntry != NULL && lpdwEntrySize == NULL)
		return ERROR_INVALID_PARAMETER;

    if (NULL == lpdwEntrySize)
        RetVal = ERROR_INVALID_PARAMETER;
    else if (szEntry == NULL)
        RetVal = ERROR_INVALID_NAME;
    else if (szEntry[0] != TEXT('\0')) {
        // It's ok to pass in a null string
        // you then get an initialized entry.
        RetVal = RaspCheckEntryName(szEntry);
    }

    // Did they just want the size of a RasEntry?
    if ((0 == RetVal) && (NULL == lpbEntry) && (*lpdwEntrySize == 0))
    {
        *lpdwEntrySize = sizeof(RASENTRY);

        DEBUGMSG( ZONE_RAS | ZONE_FUNCTION,
                 (TEXT("-AfdRasGetEntryProperties Returning sizeof(RASENTRY)\r\n"), 
                  RetVal) );
        
        return ERROR_BUFFER_TOO_SMALL;
    }

    // Check entry and size
    if (NULL == lpbEntry)
    {
        RetVal = ERROR_BUFFER_INVALID;
    }
    else if (((LPRASENTRY)lpbEntry)->dwSize != sizeof(RASENTRY_V3)
         &&  ((LPRASENTRY)lpbEntry)->dwSize != sizeof(RASENTRY))
    {
        // Not one of the two supported versions
        RetVal = ERROR_INVALID_SIZE;
    }
    else if (*lpdwEntrySize < ((LPRASENTRY)lpbEntry)->dwSize)
    {
        // Request that they provide a buffer sufficiently
        // big to store the data.
        *lpdwEntrySize = ((LPRASENTRY)lpbEntry)->dwSize;
        RetVal = ERROR_BUFFER_TOO_SMALL;
    }

    
    // Any parm errors?
    if (0 != RetVal) {
        DEBUGMSG( ZONE_RAS | ZONE_FUNCTION,
                 (TEXT("-AfdRasGetEntryProperties ParmError %d\r\n"), 
                  RetVal) );
        return RetVal;
    }

    // Shortcut to the return buffer
    pEntry = (LPRASENTRY)lpbEntry;

    if (TEXT('\0') == szEntry[0]) {
        // Return an initialized RasEntry structure.
        RaspInitRasEntry (pEntry);
        DEBUGMSG( ZONE_RAS | ZONE_FUNCTION,
                 (TEXT("-AfdRasGetEntryProperties Returning Default Entry\r\n"), 
                  RetVal) );
        return 0;
    }

    RetVal = OpenRasEntryKey(lpszPhonebook, szEntry, &hKey);
    if (ERROR_SUCCESS == RetVal)
    {
        // Get the Entry data.
        dwSize = sizeof(RASPENTRY);
        hRes = RegQueryValueEx (hKey, RASBOOK_ENTRY_VALUE, 0, &dwType,
                                (LPBYTE)&RaspEntry,
                                &dwSize);
        // What could have possibly happened?
        if ((ERROR_SUCCESS != hRes) || (dwType != REG_BINARY) ||
            (sizeof(RASPENTRY) != dwSize)) {
            // What's the proper return code when the value is bad?
            RetVal = ERROR_CANNOT_FIND_PHONEBOOK_ENTRY;
        } else {
            // Convert the private struct to the public one.
            RaspConvPrivPublic (&RaspEntry, pEntry);
        }

        // Did they want the DevConfig?
        if (lpdwSize != NULL) {
            hRes = RegQueryValueEx (hKey, RASBOOK_DEVCFG_VALUE, 0, &dwType, NULL, &dwSize);
            if (ERROR_SUCCESS == hRes) {
                // Do they just want the size?
                if ((lpb == NULL) || (*lpdwSize < dwSize)) {
                    RetVal = ERROR_BUFFER_TOO_SMALL;
                } else {
                    // Must have passed in a buffer, and it's big enough
                    hRes = RegQueryValueEx(hKey, RASBOOK_DEVCFG_VALUE, 0, &dwType, lpb, &dwSize);
                    if (hRes == ERROR_SUCCESS && !_tcscmp (RaspEntry.szDeviceType, RASDT_Vpn))
                    {
                        // For VPN type, the data is stored encrypted, so we have to decrypt it now.
                        BOOL fRet;
                        DATA_BLOB dataIn, dataOut;

						dataIn.cbData = dwSize;
						dataIn.pbData = lpb;
						dataOut.cbData = 0;
						dataOut.pbData = NULL;
                        fRet = CryptUnprotectData(&dataIn, NULL, NULL, NULL, NULL, CRYPTPROTECT_SYSTEM, &dataOut);
                        if (fRet)
                        {
                            ASSERT(dataOut.cbData <= dwSize);
                            if (dataOut.cbData <= dwSize)
                            {
                                memcpy(lpb, dataOut.pbData, dataOut.cbData);
                            }
                            else
                                RetVal = ERROR_BUFFER_TOO_SMALL;        // this should never happen
                            dwSize = dataOut.cbData;
                            LocalFree(dataOut.pbData);
                        }
                        else
                        {
                            RetVal = ERROR_CORRUPT_PHONEBOOK;
                            hRes = GetLastError();
                        }
                    }

                }
                
                // Return the correct size.
                *lpdwSize = dwSize;
            } else {
                // No dev config for this entry, Let's try to ask the miniport
                PNDISWAN_ADAPTER    pAdapter;
                DWORD               dwDeviceID;
                LPBYTE              pDeviceConfig;
                DWORD               dwDevCfgSize;

                if (SUCCESS == FindAdapter (pEntry->szDeviceName, pEntry->szDeviceType, &pAdapter,
                                            &dwDeviceID)) {
                    if (SUCCESS == NdisTapiGetDevConfig (pAdapter, dwDeviceID, &pDeviceConfig, &dwDevCfgSize)) {
                        // Ok it worked.  Now what?
                        if ((NULL != lpb) && (*lpdwSize >= dwDevCfgSize)) {
                            memcpy ((LPBYTE)lpb, pDeviceConfig, dwDevCfgSize);
                        }
                        // Tell them how big it is
                        *lpdwSize = dwDevCfgSize;
                        LocalFree (pDeviceConfig);
                    } else {
                        // Initialize to none
                        *lpdwSize = 0;
                    }
                    AdapterDelRef (pAdapter);
                    
                } else {
                    // Initialize to none
                    *lpdwSize = 0;
                }
                
            }
        }
        RegCloseKey (hKey);
    } else {
        // Could not open the key
        RetVal = ERROR_CANNOT_FIND_PHONEBOOK_ENTRY;
    }

    DEBUGMSG (ZONE_RAS || (RetVal && ZONE_ERROR),
              (TEXT("-AfdRasGetEntryProperties Return Code %d\r\n"), RetVal));
    return RetVal;
}

/*****************************************************************************
*
*
*   @func   DWORD   |   RasValidateEntryName    |
*       The RasValidateEntryName function validates the format of
*       a connection entry name.  It must contain at least one
*       non-white-space alpha-numeric character.
*
*   @rdesc
*       ERROR_SUCCESS - name is valid and not in use in phonebook
*       ERROR_ALREADY_EXISTS - name is valid and already in phonebook
*       ERROR_INVALID_NAME - name is invalid
*       ERROR_CANNOT_OPEN_PHONEBOOK - (CE) lpszPhonebook is non-NULL
*
*   @parm   LPTSTR  |   lpszPhonebook   |
*       Points to a null terminated string that specifies the full
*       path and filename of the phonebook file.  This parameter
*       can be NULL in which case the default phonebook file is
*       used.  This parameter should always be NULL for Windows 95 and
*       WinCE since the phonebook entries are stored in the registry.
*   @parm   LPTSTR  |   lpszEntry   |
*       Points to a null terminated string containing a entry name. For
*       WinCE the Entry name must include at least one alpha-numeric
*       character and be a valid registry key name.
*
*/
DWORD APIENTRY
AfdRasValidateEntryName(LPCTSTR lpszPhonebook,
                        LPCTSTR lpszEntry)
{
    HKEY    hKey;
    DWORD   RetVal;

    DEBUGMSG( ZONE_RAS | ZONE_FUNCTION,
             (TEXT("+AfdRasValidateEntryName (0x%08X, 0x%08X(%s))\r\n"), 
              lpszPhonebook, lpszEntry,
              (lpszEntry != 0) ? lpszEntry : TEXT("Null")) );

    RetVal = OpenRasEntryKey((LPTSTR)lpszPhonebook, (LPTSTR)lpszEntry, &hKey);
    if (ERROR_SUCCESS == RetVal)
    {
        RetVal = ERROR_ALREADY_EXISTS;
        RegCloseKey (hKey);
    }
    else if (ERROR_CANNOT_FIND_PHONEBOOK_ENTRY == RetVal)
    {
        RetVal = ERROR_SUCCESS;
    }

    DEBUGMSG( ZONE_RAS | ZONE_FUNCTION,
             (TEXT("-AfdRasValidateName Returning %d\r\n"), 
              RetVal) );
    return RetVal;
}

/*****************************************************************************
*
*
*   @func   DWORD   |   RasDeleteEntry |
*       The RasDeleteEntry function deletes an entry from the phone book.
*
*   @rdesc  
*       If the function succeeds, the return value is zero.
*       If the function fails, the return value is one of the following:
*       @ecode  ERROR_CANNOT_OPEN_PHONEBOOK |
*           Invalid phonebook entry specified.  For Win95 and WinCE the
*           lpszPhonebook parameter must be NULL.   
*       @ecode  ERROR_INVALID_NAME |
*           Invalid name specified in lpszEntry parameter.
*       @ecode  Others |
*           Errors returned from RegDeleteKey()
*
*   @parm   LPTSTR  |   lpszPhonebook   |
*       Points to a null terminated string that specifies the
*       full path and filename of the phonebook file.  This
*       parameter can be NULL, in which case the default
*       phonebook file is used.  This parameter should always
*       be NULL for Windows 95 and WinCE since the phonebook
*       entries are stored in the registry.
*   @PARM   LPTSTR  |   lpszEntry       |
*       Points to a null terminated string containing an
*       existing entry name that will be deleted.
*
*/
DWORD WINAPI 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -