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

📄 rasreg.c

📁 WinCE5.0部分核心源码
💻 C
📖 第 1 页 / 共 5 页
字号:

DWORD
CopyRegEntryValues(
    HKEY    hkeyDst,
    HKEY    hkeySrc)
//
//  Copy all the values from the source key to the destination.
//
{
    DWORD   dwResult, iValue;
    LPTSTR  ValueName;              // The name of the value read in
    DWORD   dwValueLen;             // Length of the name
    LPBYTE  pData;                  // Pointer to the data of the value
    DWORD   dwDataLen;              // Length of the data
    DWORD   dwType;                 // Type of the data 
    DWORD   cValues, cchMaxValueName;
    DWORD   cbMaxValueData;

    // Find out how many values are in the source key, and the max sizes
    dwResult = RegQueryInfoKey (hkeySrc, NULL, NULL, NULL, NULL, NULL, NULL,
                            &cValues, &cchMaxValueName, &cbMaxValueData,
                            NULL, NULL);

    if (ERROR_SUCCESS != dwResult)
    {
        // What should I return for this.
        DEBUGMSG( ZONE_RAS | ZONE_ERROR, (TEXT(" CopyRegEntryValues: ERROR %d from QueryInfo\n"), dwResult));
    }
    else
    {
        // Allocate the space for value names and data
        ValueName = (LPTSTR)LocalAlloc (LPTR, (cchMaxValueName+1)*sizeof(TCHAR));
        pData = (LPBYTE)LocalAlloc (LPTR, cbMaxValueData+1);

        DEBUGMSG (ZONE_RAS, (TEXT("ValueName=0x%X (Len=%d) pData=0x%X (Len=%d)\r\n"),
               ValueName, (cchMaxValueName+1)*(sizeof(TCHAR)),
               pData, cbMaxValueData));

        if ((NULL == ValueName) || (NULL == pData))
        {
            DEBUGMSG(ZONE_RAS | ZONE_ERROR, (TEXT(" CopyRegEntryValues: ERROR: Out of memory\n")));
            dwResult = ERROR_CANNOT_FIND_PHONEBOOK_ENTRY;
        }
        else
        {
            // Iterate over the values.

            for (iValue = 0; iValue != cValues; iValue++)
            {
                dwValueLen = cchMaxValueName+1;
                dwDataLen = cbMaxValueData;

                // Read the source value
                dwResult = RegEnumValue (hkeySrc, iValue, ValueName,
                                     &dwValueLen, NULL, &dwType,
                                     pData, &dwDataLen);

                if (ERROR_SUCCESS != dwResult)
                    break;

                DEBUGMSG (ZONE_RAS, (TEXT("Read Entry '%s' Len=%d\n"), ValueName, dwDataLen));

                // Write the dest value.
                dwResult = RegSetValueEx (hkeyDst, ValueName, 0, dwType, pData, dwDataLen);

                if (ERROR_SUCCESS != dwResult)
                    break;
            }
        }
        LocalFree(ValueName);
        LocalFree(pData);
    }

    return dwResult;
}

DWORD
RegCreateNewKey(
	IN  HKEY    hKey, 
	IN  LPCWSTR lpSubKey, 
	OUT PHKEY   phkResult)
//
//  Create a new registry key.
//  Fail and return ERROR_ALREADY_EXISTS if the key is already present.
//
{
	DWORD dwDisposition,
		  dwResult;

	dwResult = RegCreateKeyEx(hKey, lpSubKey, 0, NULL, 0, 0, NULL, phkResult, &dwDisposition);
	if (dwResult == NO_ERROR)
	{
		if (dwDisposition == REG_OPENED_EXISTING_KEY)
		{
			// Can't rename to a key that already exists
			dwResult = ERROR_ALREADY_EXISTS;
			RegCloseKey(*phkResult);
			*phkResult = NULL;
		}
	}
	return dwResult;
}

BOOL
GetTempRegKey(
	IN	HKEY	hKeyBase,
	OUT PWSTR   wszTempKeyName,
	OUT PHKEY   phTempKey)
{
	DWORD triesLeft,
		  dwRandom,
		  dwResult;

	for (triesLeft = 25; triesLeft; triesLeft--)
	{
		CeGenRandom(sizeof(dwRandom), (PBYTE)&dwRandom);
		swprintf(wszTempKeyName, L"$tmp%d", dwRandom);

		dwResult = RegCreateNewKey(hKeyBase, wszTempKeyName, phTempKey);
		if (dwResult == NO_ERROR)
		{
			// Successfully created new temp key
			break;
		}
	}

	return triesLeft > 0;
}

DWORD
RegMoveKey(
	IN      HKEY   hKeyBase,
	IN	OUT PHKEY  phKeyDst,
	IN      PWSTR  wszNameDst,
	IN  OUT PHKEY  phKeySrc,
	IN      PWSTR  wszNameSrc)
//
//  Move the contents of the source key to the destination key.
//  If successful, the source key and its contents will be deleted.
//  If unsuccessful, the source key and its contents will be unaffected, and no new registry entries will be created.
//
//
{
	DWORD dwResult;

	dwResult = CopyRegEntryValues(*phKeyDst, *phKeySrc);
	if (dwResult == NO_ERROR)
	{
		// Successfully copied src to dst, can delete src
		RegCloseKey(*phKeySrc);
		*phKeySrc = NULL;
		dwResult = RegDeleteKey(hKeyBase, wszNameSrc);
	}
	else
	{
		// Unsuccessful attempt to copy src to dst.
		// Cleanup the partially created dst key
		RegCloseKey(*phKeyDst);
		*phKeyDst = NULL;
		(void)RegDeleteKey(hKeyBase, wszNameDst);
	}

	return dwResult;
}

DWORD
RegRenameKey(
	IN	HKEY	hKeyBase,
	IN  PWSTR   wszOldKeyName,
	IN  PWSTR   wszNewKeyName)
//
//  Rename hKeyBase\OldKey to hKeyBase\NewKey.
//
//  If OldKey and NewKey are identical, this is a no-op.
//
//  If OldKey and NewKey are different, then we need to copy all of
//  OldKey to NewKey, then delete OldKey.
//
//  If OldKey and NewKey are only different in upper/lower case characters,
//  then we must copy OldKey to a temporary key, delete OldKey, then
//  copy the temporary key to NewKey. Yuck. Would be nice if the OS would
//  support a RenameKey API...
//
{
	DWORD dwResult;
	HKEY  hKeyOld = NULL,
		  hKeyNew = NULL,
		  hKeyTemp = NULL;
	WCHAR wszTempKeyName[64];

	dwResult = RegOpenKeyEx(hKeyBase, wszOldKeyName, 0, 0, &hKeyOld);
	if (dwResult == NO_ERROR)
	{
		if (0 == wcscmp(wszOldKeyName, wszNewKeyName))
		{
			// Nothing to do, identical names.
		}
		else if (0 == wcsicmp(wszOldKeyName, wszNewKeyName))
		{
			//
			// Changing the case of the key
			//

			// Create a  temporary key to copy old to
			if (FALSE == GetTempRegKey(hKeyBase, &wszTempKeyName[0], &hKeyTemp))
			{
				dwResult = ERROR_OUTOFMEMORY;
			}
			else
			{
				// Copy old to temp (note this deletes the old if successful)
				dwResult = RegMoveKey(hKeyBase, &hKeyTemp, &wszTempKeyName[0], &hKeyOld, wszOldKeyName);
				if (dwResult == NO_ERROR)
				{
					// Create the new key
					dwResult = RegCreateNewKey(hKeyBase, wszNewKeyName, &hKeyNew);
					if (dwResult == NO_ERROR)
					{
						// Copy temporary to new
						dwResult = RegMoveKey(hKeyBase, &hKeyNew, wszNewKeyName, &hKeyTemp, wszTempKeyName);
					}
				}
			}
		}
		else
		{
			//
			// Renaming to a new key value
			//
			dwResult = RegCreateNewKey(hKeyBase, wszNewKeyName, &hKeyNew);
			if (dwResult == NO_ERROR)
			{
				// Move the old to the new
				dwResult = RegMoveKey(hKeyBase, &hKeyNew, wszNewKeyName, &hKeyOld, wszOldKeyName);
			}
		}

	}

	if (hKeyTemp)
		RegCloseKey(hKeyTemp);

	if (hKeyNew)
		RegCloseKey(hKeyNew);

	if (hKeyOld)
		RegCloseKey(hKeyOld);

	return dwResult;
}

/*****************************************************************************
* 
*   @func   DWORD | RasMakeDefault |  Make the default Ras Entries
*
*   @rdesc  If the function succeeds the value is zero. Else an error
*           from raserror.h is returned.
*   @ecode  ERROR_UNKNOWN | Required reserved parameter is not NULL.
*   @ecode  ERROR_CANNOT_OPEN_PHONEBOOK | Invalid PhoneBookPath specified
*   
*   @parm   LPTSTR  | reserved          | reserved (must be null)
*   @parm   LPTSTR  | lpszPhoneBookPath | 
*       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.
*
*
*/

DWORD APIENTRY
AfdRasMakeDefault (LPTSTR          reserved, 
                   LPTSTR          lpszPhonebook )
{
    TCHAR       szEntry[128];
    RASENTRY    RasEntry;
    DWORD       dwSize;

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

    RasEntry.dwSize = sizeof(RASENTRY);
    RaspInitRasEntry(&RasEntry);

    RasEntry.ipaddr.d = 192;
    RasEntry.ipaddr.c = 168;
    RasEntry.ipaddr.b = 55;
    RasEntry.ipaddr.a = 100;

    // _tcscat( KeyName, TEXT("`Desktop @ 19200`") );
    // Get the string from the resource file
    dwSize = (DWORD)CallGetNetString (NETUI_GETNETSTR_DIRECT_NAME,
                                      szEntry, sizeof(szEntry)/sizeof(TCHAR));
    if (!dwSize) {
        HKEY        hKey;
        DWORD       dwDisp;
        LONG        hRes;
        RETAILMSG (1, (TEXT("Unable to get default name, just creating key\r\n")));

        hRes = RegCreateKeyEx(HKEY_CURRENT_USER, RASBOOK_KEY, 0, NULL,
                              REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
                              &hKey, &dwDisp);
        if (hRes == ERROR_SUCCESS) {
            RegCloseKey(hKey);
        }
        return 0;
    }

    AfdRasSetEntryProperties(lpszPhonebook, szEntry, (LPBYTE)&RasEntry, sizeof(RASENTRY),
                             NULL,0);
    return 0;
}

/*****************************************************************************
* 
*   @func   DWORD | RasEnumEntries |  Lists all entry names in a remote 
*                                       access phone book.
*
*   @rdesc  If the function succeeds the value is zero. Else an error
*           from raserror.h is returned.
*   @ecode  ERROR_UNKNOWN | Required reserved parameter is not NULL.
*   @ecode  ERROR_CANNOT_OPEN_PHONEBOOK | Invalid PhoneBookPath specified
*   @ecode  ERROR_INVALID_SIZE | Invalid lpcb parameter
*   
*   @parm   LPTSTR  | reserved          | reserved
*   @parm   LPTSTR  | lpszPhoneBookPath | path name of phone book
*   @parm   LPRASENTRYNAME | lprasentryname |
*       Points to a buffer that receives an array of <f RASENTRYNAME>
*       structures, one for each phonebook entry. Before calling the
*       function, an application must set the dwSize member of the first
*       <f RASENTRYNAME> structure in the buffer to sizeof(<f RASENTRYNAME>)
*       in order to identify the version of the structure being passed.
*   @parm   LPDWORD | lpcb              |
*       Points to a variable that contains the size, in bytes, of the buffer
*       specified by lprasentryname. On return, the function sets this
*       variable to the number of bytes required to successfully complete
*       the call.
*   @parm   LPDWORD | lpcEntries        |
*       Points to a variable that the function, if successful, sets to
*       the number of phonebook entries written to the buffer specified
*       by lprasentryname.
*
*
*/

DWORD APIENTRY
AfdRasEnumEntries (LPTSTR          reserved, 
                   LPTSTR          lpszPhoneBookPath, 
                   LPRASENTRYNAME  lpRasEntryName, 
                   LPDWORD         lpcb, 
                   LPDWORD         lpcEntries )
{
    HKEY        hKey;
    LONG        hRes;
    DWORD       Index;
    DWORD       cbRequired = 0;
    DWORD       RetVal = ERROR_SUCCESS;
    TCHAR       ValueString[RAS_MaxEntryName + 1];
    DWORD       ValueLen;
    DWORD       cEntries;   // Number of entries found.
    FILETIME    LastWrite;

    DEBUGMSG( ZONE_RAS | ZONE_FUNCTION,
              (TEXT("+AfdRasEnumEntries( %08X, %08X, %08X, %08X, %08X )\r\n"), 
               reserved, lpszPhoneBookPath, lpRasEntryName, lpcb, lpcEntries));

    // Check the required parameters.
    if (NULL != reserved) {
        RetVal = ERROR_UNKNOWN;     
    }
    if (NULL != lpszPhoneBookPath) {

⌨️ 快捷键说明

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