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

📄 regview.cpp

📁 windows ce5.0 注册表文件的读取
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    himl = ImageList_Create (16, 16, ILC_COLOR, 2, 0);
    // Load first two images from one bitmap.
    hBmp = LoadBitmap (hInst, MAKEINTRESOURCE (ID_BMPS));
    ImageList_Add (himl, hBmp, NULL);
    DeleteObject (hBmp);

    TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
    return hwndTV;
}
//----------------------------------------------------------------------
// InsertLV - Add an item to the list view control.
//
INT InsertLV (HWND hWnd, INT nItem, LPTSTR pszName, LPTSTR pszData) {

    HWND hwndLV = GetDlgItem (hWnd, ID_LISTV);
    LVITEM lvi;
    INT rc;

    lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
    lvi.iItem = nItem;
    lvi.iSubItem = 0;
    lvi.pszText = pszName;
    lvi.iImage = 0;
    lvi.lParam = nItem;
    rc = SendMessage (hwndLV, LVM_INSERTITEM, 0, (LPARAM)&lvi);

    lvi.mask = LVIF_TEXT;
    lvi.iItem = nItem;
    lvi.iSubItem = 1;
    lvi.pszText = pszData;
    rc = SendMessage (hwndLV, LVM_SETITEM, 0, (LPARAM)&lvi);
    return 0;
}
//----------------------------------------------------------------------
// InsertTV - Insert item into tree view control.
//
HTREEITEM InsertTV (HWND hWnd, HTREEITEM hParent, TCHAR *pszName, 
                    LPARAM lParam, DWORD nChildren) {
    TV_INSERTSTRUCT tvis;

    HWND hwndTV = GetDlgItem (hWnd, ID_TREEV);
    // Initialize the insertstruct.
    memset (&tvis, 0, sizeof (tvis));
    tvis.hParent = hParent;
    tvis.hInsertAfter = TVI_LAST;
    tvis.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_CHILDREN |
                     TVIF_IMAGE;
    tvis.item.pszText = pszName;
    tvis.item.cchTextMax = lstrlen (pszName);
    tvis.item.iImage = 1;
    tvis.item.iSelectedImage = 1;
    tvis.item.lParam = lParam;
    if (nChildren)
        tvis.item.cChildren = 1;
    else
        tvis.item.cChildren = 0;

    return TreeView_InsertItem (hwndTV, &tvis);
}
//----------------------------------------------------------------------
// GetTree - Compute the full path of the tree view item.
//
INT GetTree (HWND hWnd, HTREEITEM hItem, HKEY *pRoot, TCHAR *pszKey, 
             INT nMax) {
    TV_ITEM tvi;
    TCHAR szName[256];
    HTREEITEM hParent;
    HWND hwndTV = GetDlgItem (hWnd, ID_TREEV);

    memset (&tvi, 0, sizeof (tvi));

    hParent = TreeView_GetParent (hwndTV, hItem);
    if (hParent) { 
        // Get the parent of the parent of the...
        GetTree (hWnd, hParent, pRoot, pszKey, nMax);

        // Get the name and of item.
        tvi.mask = TVIF_TEXT;
        tvi.hItem = hItem;
        tvi.pszText = szName;
        tvi.cchTextMax = dim(szName);
        TreeView_GetItem (hwndTV, &tvi);

        lstrcat (pszKey, TEXT ("\\"));
        lstrcat (pszKey, szName);
    } else {
        *pszKey = TEXT ('\0');
        szName[0] = TEXT ('\0');
        // Get the name and of item.
        tvi.mask = TVIF_TEXT | TVIF_PARAM;
        tvi.hItem = hItem;
        tvi.pszText = szName;
        tvi.cchTextMax = dim(szName);
        if (TreeView_GetItem (hwndTV, &tvi))
            *pRoot = (HKEY)tvi.lParam;
        else {
            INT rc = GetLastError();
        }
    }
    return 0;
}
//----------------------------------------------------------------------
// DisplayValue - Display the data depending on the type.
//
INT DisplayValue (HWND hWnd, INT nCnt, LPTSTR pszName, PBYTE pbData, 
                  DWORD dwDSize, DWORD dwType) {
    TCHAR szData[512];
    INT i, len;

    switch (dwType) {
    case REG_MULTI_SZ:
		{
			LPTSTR pszBData=(LPTSTR)pbData;
			for(int i=0;i<(int)dwDSize/(int)sizeof(TCHAR)-1;i++)
			{
				if (pszBData[i]==0)
					pszBData[i]=(TCHAR)' ';	
			}
			pszBData[i]=0;
			lstrcpy(szData,pszBData);
		}
		break;
    case REG_EXPAND_SZ:
    case REG_SZ:
        lstrcpy (szData, (LPTSTR)pbData);
        break;

    case REG_DWORD:
        wsprintf (szData, TEXT ("%d"), *(int *)pbData);
        break;

    case REG_BINARY:
        szData[0] = TEXT ('\0');
        for (i = 0; i < (int)dwDSize; i++) {
            len = lstrlen (szData);
            wsprintf (&szData[len], TEXT ("%02X "), pbData[i]);
            if (len > dim(szData) - 6)
                break;
        }
        break;
    default:
        wsprintf (szData, TEXT ("Unknown type: %x"), dwType);
    }
    InsertLV (hWnd, nCnt, pszName, szData);
    return 0;
}
//----------------------------------------------------------------------
// EnumValues - Enumerate each of the values of a key.
//
INT EnumValues (HWND hWnd, HKEY hRoot, LPTSTR pszKey) {
    INT nCnt = 0, rc;
    DWORD dwNSize, dwDSize, dwType;
    TCHAR szName[MAX_PATH];
    BYTE bData[1024];
    HKEY hKey;

    if (lstrlen (pszKey)) {
        if (RegOpenKeyEx (hRoot, pszKey, 0, 0, &hKey) != ERROR_SUCCESS)
            return 0;
    } else 
        hKey = hRoot;

    // Clean out list view.
    ListView_DeleteAllItems (GetDlgItem (hWnd, ID_LISTV));

    // Enumerate the values in the list view control.
    nCnt = 0;
    dwNSize = dim(szName);
    dwDSize = dim(bData);
    rc = RegEnumValue (hKey, nCnt, szName, &dwNSize,
                       NULL, &dwType, bData, &dwDSize);

    while (rc == ERROR_SUCCESS) {
        // Display the value in the list view control.
        DisplayValue (hWnd, nCnt, szName, bData, dwDSize, dwType);

        dwNSize = dim(szName);
        dwDSize = dim(bData);
        nCnt++;
        rc = RegEnumValue (hKey, nCnt, szName, &dwNSize,
                           NULL, &dwType, bData, &dwDSize);
    }
    if (hKey != hRoot)
        RegCloseKey (hKey);
    return 1;
}
//----------------------------------------------------------------------
// CountChildren - Count the number of children of a key.
//
DWORD CountChildren (HKEY hRoot, LPTSTR pszKeyPath, LPTSTR pszKey) {
    TCHAR *pEnd;
    DWORD dwCnt;
    HKEY hKey;

    pEnd = pszKeyPath + lstrlen (pszKeyPath);
    lstrcpy (pEnd, TEXT ("\\"));
    lstrcat (pEnd, pszKey);

    if (RegOpenKeyEx(hRoot, pszKeyPath, 0, 0, &hKey) == ERROR_SUCCESS){
        RegQueryInfoKey (hKey, NULL, NULL, 0, &dwCnt, NULL, NULL, NULL,
                         NULL, NULL, NULL, NULL);
        RegCloseKey (hKey);
    }
    *pEnd = TEXT ('\0');
    return dwCnt;
}
//----------------------------------------------------------------------
// EnumChildren - Enumerate the child keys of a key.
//
INT EnumChildren (HWND hWnd, HTREEITEM hParent, HKEY hRoot, 
                  LPTSTR pszKey) {
    INT i = 0, rc;
    DWORD dwNSize;
    DWORD dwCSize;
    TCHAR szName[MAX_PATH];
    TCHAR szClass[256];
    FILETIME ft;
    DWORD nChild;
    HKEY hKey;
    TVITEM tvi;

    // All keys but root need to be opened.
    if (lstrlen (pszKey)) {
        if (RegOpenKeyEx (hRoot, pszKey, 0, 0, &hKey) != ERROR_SUCCESS) {
            rc = GetLastError();
            return 0;
        }
    } else 
        hKey = hRoot;

    dwNSize = dim(szName);
    dwCSize = dim(szClass);
    rc = RegEnumKeyEx (hKey, i, szName, &dwNSize, NULL,
                       szClass, &dwCSize, &ft);
    while (rc == ERROR_SUCCESS) {

        nChild = CountChildren (hRoot, pszKey, szName);
        // Add key to tree view.
        InsertTV (hWnd, hParent, szName, 0, nChild);
        dwNSize = dim(szName);
        rc = RegEnumKeyEx (hKey, ++i, szName, &dwNSize,
                           NULL, NULL, 0, &ft);
    }
    // If this wasn't the a root key, close it.
    if (hKey != hRoot)
        RegCloseKey (hKey);

    // If no children, remove expand button.
    if (i == 0) {
        tvi.hItem = hParent;
        tvi.mask = TVIF_CHILDREN;
        tvi.cChildren = 0;
        TreeView_SetItem (GetDlgItem (hWnd, ID_TREEV), &tvi);
    }
    return i;
}
//======================================================================
// About Dialog procedure
//
BOOL CALLBACK AboutDlgProc (HWND hWnd, UINT wMsg, WPARAM wParam, 
                            LPARAM lParam) {
    switch (wMsg) {
        case WM_COMMAND:
            switch (LOWORD (wParam)) {
                case IDOK:
                case IDCANCEL:
                    EndDialog (hWnd, 0);
                    return TRUE;
            }
        break;
    }
    return FALSE;
}


/*****************************************self define***********************/
LPARAM DoMainCommandExport (HWND hWnd, WORD idItem, HWND hwndCtl, 
                          WORD wNotifyCode) {
	TCHAR szKey[REGKEYNAME_SIZE_MAX];
	HKEY hRoot;
	HTREEITEM hItem= TreeView_GetSelection(GetDlgItem(hWnd,ID_TREEV));
	TCHAR szOut[2000]=TEXT("");
	GetTree(hWnd,hItem,&hRoot,szKey,dim(szKey));
	if (Export(hRoot,szKey))
		MessageBox(hWnd,TEXT("Export Completed."),TEXT("Info"),IDOK);

    return 0;
}

LPARAM DoMainCommandImport (HWND hWnd, WORD idItem, HWND hwndCtl, 
                          WORD wNotifyCode) {
	if (Import())
		MessageBox(hWnd,TEXT("Import Completed."),TEXT("Info"),IDOK);
	return 0;
}

LPARAM DoMainCommandDel(HWND hWnd, WORD idItem, HWND hwndCtl, 
                          WORD wNotifyCode) {
	TCHAR szKey[REGKEYNAME_SIZE_MAX];
	HKEY hRoot;
	HTREEITEM hItem= TreeView_GetSelection(GetDlgItem(hWnd,ID_TREEV));
	TCHAR szOut[2000]=TEXT("");
	GetTree(hWnd,hItem,&hRoot,szKey,dim(szKey));
	if (ERROR_SUCCESS==RegDeleteKey(hRoot,szKey))
		MessageBox(hWnd,TEXT("Key deleted"),TEXT("info"),IDOK);
	return 0;
}
LRESULT Export(HKEY hRoot,LPTSTR pszKey)
{
	TCHAR szFileName[260];
	GetFileName(hwndMain,szFileName,260);
	HANDLE hFile=CreateFile(szFileName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);	
	if (INVALID_HANDLE_VALUE==hFile)
		return 0;
	Export(hRoot,pszKey,hFile);
	CloseHandle(hFile);
	return 1;
}

void Export(HKEY hRoot,LPTSTR pszKey,HANDLE hFile)
{
	RegKey regKey;
	//获取键的绝对路径
	TCHAR szName[REGKEYNAME_SIZE_MAX]=TEXT("");
	GetFullPath(hRoot,pszKey,szName);				
	regKey.Init(szName);
	GetValues(hRoot,pszKey,&regKey);		
	
	TCHAR szOut[KEYBLOCK_SIZE_MAX]=TEXT("");
	GetChildren(hRoot,pszKey,&regKey,TRUE);
	regKey.ToString(szOut);	
	//MessageBox(hwndMain,szOut,TEXT("Export"),IDOK);//可以在此将数据写入文件
	DWORD wWrited;
	WriteFile(hFile,szOut,lstrlen(szOut)*sizeof(TCHAR),&wWrited,NULL);

	RegKey *pChildKey;
	pChildKey=regKey.pChildListHead;
	while (pChildKey!=NULL)
	{
		TCHAR *cPos;
		cPos=wcschr(pChildKey->szName,(TCHAR)'\\');
		lstrcpy(pChildKey->szName,cPos);
		Export(hRoot,pChildKey->szName,hFile);
		pChildKey=pChildKey->next;
	}

	regKey.free();
}

LRESULT Import()
{
	TCHAR szFileName[260],szBlock[255]=TEXT("");
	DWORD rc,startpos;
	GetFileName(hwndMain,szFileName,260);
	HANDLE hFile=CreateFile(szFileName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);	
	if (INVALID_HANDLE_VALUE==hFile)
	{
		
		MessageBox(hwndMain,TEXT("Can not open file"),TEXT(""),IDOK);
		CloseHandle(hFile);
		return 0;
	}
	startpos=SetFilePointer(hFile,0,NULL,FILE_BEGIN);
	rc=ReadBlock(hFile,startpos,(TCHAR)'[',szBlock,TRUE);
	while ((long)rc>0)
	{
		RegKey regKey;
		ProcessKeyBlock(szBlock,&regKey);
		regKey.WriteToReg();
		regKey.free();

		rc=ReadBlock(hFile,rc,(TCHAR)'[',szBlock,TRUE);	
	}
	if ((long)rc==0)
	{
		//MessageBox(hWnd,szBlock,TEXT(""),IDOK);
		RegKey regKey;
		ProcessKeyBlock(szBlock,&regKey);
		regKey.WriteToReg();
		regKey.free();
	}

	CloseHandle(hFile);
	return 1;
}

LRESULT Import(LPTSTR szFilename)
{
	HANDLE hFile=CreateFile(szFilename,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);	
	if (INVALID_HANDLE_VALUE==hFile)
		return 0;
	
	DWORD nNum=1;
	TCHAR szKeyBlock[KEYBLOCK_SIZE_MAX]=TEXT("");
	int iIndex=0;
	TCHAR c;
	while (nNum>0)
	{		
		ReadFile(hFile,&c,sizeof(TCHAR),&nNum,NULL);
		if (c==(TCHAR)'\n')
		{
			szKeyBlock[iIndex]=0;
			if (lstrlen(szKeyBlock)>0)
			{
				RegKey regKey;
				ProcessKeyBlock(szKeyBlock,&regKey);
				regKey.WriteToReg();
				/*
				TCHAR szOut[KEYBLOCK_SIZE_MAX]=TEXT("");
				regKey.ToString(szOut);
				MessageBox(hwndMain,szOut,TEXT("import"),IDOK);
				*/
			}
			iIndex=0;
			SetFilePointer(hFile,sizeof(TCHAR),NULL,FILE_CURRENT);
		}
		else
		{
			szKeyBlock[iIndex++]=c;
		}		
	}
	
	szKeyBlock[iIndex]=0;
	if (lstrlen(szKeyBlock)>0)
	{
		RegKey regKey;
		ProcessKeyBlock(szKeyBlock,&regKey);
		regKey.WriteToReg();
		//regKey.free();
		/*
		TCHAR szOut[KEYBLOCK_SIZE_MAX]=TEXT("");
		regKey.ToString(szOut);

⌨️ 快捷键说明

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