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

📄 regedit.c

📁 wince 用的注册表编辑器源代码。可以看和编辑wince 注册表
💻 C
📖 第 1 页 / 共 2 页
字号:
	len = _tcslen( src ) ;
	len2 = *size ;
	if ( len2 < len ) {
		if ( !len2 -- ) {
			return FALSE ;
		}
		if ( len2 ) {
			memcpy( *dst, src, len2 * sizeof (TCHAR) ) ;
		}
		*dst += len2 ;
		**dst = 0 ;
		*size = 0 ;
		return FALSE ;
	}
	_tcscpy( *dst, src ) ;
	*dst += len ;
	*size -= len ;
	return TRUE ;
}

void
GetBriefValueData( LPTSTR buf, DWORD size )
{
	TCHAR	tmpbuf[ 20 ] ;
	LPBYTE	src = ValueData ;

	if ( !size -- ) {
		return ;
	}
	*buf = 0 ;
	/* 抣偺撪梕偑偁傞偐僠僃僢僋偡傞 */
	if ( !ValueSize ) {
		return ;
	}
	if ( size > 30 ) {
		size = 30 ;
	}
	/* 抣偺撪梕傪僐僺乕偡傞 */
	switch ( ValueType ) {
	case REG_DWORD:
		wsprintf( tmpbuf, TEXT("%d"), *(LPDWORD)ValueData ) ;
		if ( !AppendStr( &buf, &size, tmpbuf ) ) {
			return ;
		}
		break ;
	case REG_SZ:
	case REG_MULTI_SZ:
		if ( !AppendStr( &buf, &size, (LPCTSTR) src ) ) {
			break ;
		}
		break ;
	default:
		do {
			wsprintf( tmpbuf, TEXT("%02X "), 0xFF & *src++ ) ;
			if ( !AppendStr( &buf, &size, tmpbuf ) ) {
				break ;
			}
		} while ( -- ValueSize ) ;
		break ;
	}
	*buf = 0 ;
}

void
GetValueStr( LPTSTR buf, DWORD size )
{
	LPCTSTR	ptr ;

	if ( !size -- ) {
		return ;
	}
	/* 抣偺柤徧傪僐僺乕偡傞 */
	ptr = *ValueName ? ValueName : szNoName ;
	if ( !AppendStr( &buf, &size, ptr ) ) {
		return ;
	}
	if ( !AppendStr( &buf, &size, TEXT(" : ") ) ) {
		return ;
	}
	/* 抣偺宆傪僐僺乕偡傞 */
	if ( !GetTypeStr( &ptr ) ) {
		ptr = szUnknown ;
	}
	if ( !AppendStr( &buf, &size, ptr ) ) {
		return ;
	}
	if ( !AppendStr( &buf, &size, TEXT(" : ") ) ) {
		return ;
	}
	GetBriefValueData( buf, size ) ;
}

BOOL
GetValueData( LPTSTR buf, DWORD size )
{
	LPCTSTR	ptr ;
	DWORD	count ;
	TCHAR	tmpbuf[ 20 ], c ;
	LPBYTE	src = ValueData ;

	if ( !size -- ) {
		return FALSE ;
	}
	*buf = 0 ;
	/* 抣偺撪梕偑偁傞偐僠僃僢僋偡傞 */
	if ( !ValueSize ) {
		return FALSE ;
	}
	/* 抣偺撪梕傪僐僺乕偡傞 */
	switch ( ValueType ) {
	case REG_DWORD:
		wsprintf( tmpbuf, TEXT("%d"), *(LPDWORD)ValueData ) ;
		if ( !AppendStr( &buf, &size, tmpbuf ) ) {
			return FALSE ;
		}
		break ;
	case REG_SZ:
		if ( !AppendStr( &buf, &size, (LPCTSTR) src ) ) {
			return FALSE ;
		}
		break ;
	case REG_MULTI_SZ:
		ptr = (LPCTSTR) src ;
		count = ValueSize / sizeof (TCHAR) ;
		while ( *ptr ) {
			while ( c = *ptr++ ) {
				if ( !size -- ) {
					return FALSE ;
				}
				*buf++ = c ;
			}
			if ( !AppendStr( &buf, &size, TEXT("\r\n" ) ) ) {
				return FALSE ;
			}
		}
		break ;
	default:
		do {
			wsprintf( tmpbuf, TEXT("%02X "), 0xFF & *src++ ) ;
			if ( !AppendStr( &buf, &size, tmpbuf ) ) {
				return FALSE ;
			}
		} while ( -- ValueSize ) ;
		break ;
	}
	*buf = 0 ;
	return TRUE ;
}

BOOL
CheckValueData( LPCTSTR src, DWORD size )
{
	TCHAR	c, last ;
	DWORD	count, len ;

	switch ( ValueType ) {
	case REG_DWORD:
		break ;
	case REG_SZ:
		len = (_tcslen( src ) + 1) * sizeof (TCHAR) ;
		if ( len >= sizeof ValueData ) {
			return FALSE ;
		}
		break ;
	case REG_MULTI_SZ:
		last = L'A' ;
		len = 0 ;
		count = _tcslen( src ) ;
		do {
			c = *src++ ;
			if ( c == L'\r' ) {
				continue ;
			} else if ( c == L'\n' ) {
				c = 0 ;
			}
			if ( !c && !last ) {
				return FALSE ;
			}
			last = c ;
			len ++ ;
		} while ( -- count ) ;
		if ( c ) {
			len ++ ;
		}
		len ++ ;
		len *= sizeof (TCHAR) ;
		if ( len >= sizeof ValueData ) {
			return FALSE ;
		}
		break ;
	default:
		return FALSE ;
	}
	return TRUE ;
}

BOOL
PutValueData( LPCTSTR src, DWORD size )
{
	TCHAR	c ;
	LPTSTR	dst ;
	DWORD	count ;

	switch ( ValueType ) {
	case REG_DWORD:
		*((LPDWORD) ValueData) = _ttol( src ) ;
		ValueSize = sizeof (DWORD) ;
		break ;
	case REG_SZ:
		_tcscpy( (LPTSTR) ValueData, src ) ;
		ValueSize = (_tcslen( src ) + 1) * sizeof (TCHAR) ;
		break ;
	case REG_MULTI_SZ:
		ValueSize = 0 ;
		dst = (LPTSTR) ValueData ;
		count = _tcslen( src ) ;
		do {
			c = *src++ ;
			if ( c == L'\r' ) {
				continue ;
			} else if ( c == L'\n' ) {
				c = 0 ;
			}
			*dst++ = c ;
			ValueSize ++ ;
		} while ( -- count ) ;
		if ( c ) {
			*dst++ = 0 ;
			ValueSize ++ ;
		}
		*dst++ = 0 ;
		ValueSize ++ ;
		ValueSize *= sizeof (TCHAR) ;
		break ;
	default:
		MessageBeep( MB_ICONHAND ) ;
		return FALSE ;
	}
	return TRUE ;
}

BOOL
NewValue( DWORD type )
{
	HKEY	hKey ;
	LONG	lret ;

	if ( hKeyRoot == INVALID_HANDLE_VALUE ) {
		MessageBeep( MB_ICONHAND ) ;
		return FALSE ;
	}
	ValueType = type ;
	*ValueName = 0 ;
	*Buffer = 0 ;
	if ( ValueType == REG_MULTI_SZ ) {
		if ( !DialogBox( (HANDLE) hInst,
						  MAKEINTRESOURCE(IDD_DLG_MULTI),
						  hMainWnd, (DLGPROC) InputBoxDlgProc ) ) {
			return FALSE ;
		}
	} else {
		if ( !DialogBox( (HANDLE) hInst,
						  MAKEINTRESOURCE(IDD_DLG_SINGLE),
						  hMainWnd, (DLGPROC) InputBoxDlgProc ) ) {
			return FALSE ;
		}
	}
	if ( !PutValueData( Buffer, sizeof Buffer / sizeof Buffer[0] ) ) {
		return FALSE ;
	}
	lret = RegOpenKeyEx( hKeyRoot, RegSubKey, 0,
#ifdef	_WIN32_WCE
						0,
#else	/* _WIN32_WCE */
						KEY_ALL_ACCESS,
#endif	/* _WIN32_WCE */
						&hKey ) ;
	if ( lret != ERROR_SUCCESS ) {
		MessageBox( hMainWnd, TEXT("can't open the key"), szWinName, MB_OK ) ;
		return FALSE ;
	}
	lret = RegSetValueEx( hKey, ValueName, 0, ValueType, ValueData, ValueSize ) ;
	if ( lret != ERROR_SUCCESS ) {
		RegCloseKey( hKey ) ;
		MessageBox( hMainWnd, TEXT("can't write the value"), ValueName, MB_OK ) ;
		return FALSE ;
	}
	RegCloseKey( hKey ) ;
	return TRUE ;
}

BOOL
EditValue( LPCTSTR name )
{
	HKEY	hKey ;
	LONG	lret ;
	LPTSTR	ptr = (LPTSTR) Buffer ;

	_tcscpy( ValueName, name ) ;
	lret = RegOpenKeyEx( hKeyRoot, RegSubKey, 0,
#ifdef	_WIN32_WCE
						0,
#else	/* _WIN32_WCE */
						KEY_ALL_ACCESS,
#endif	/* _WIN32_WCE */
						&hKey ) ;
	if ( lret != ERROR_SUCCESS ) {
		wsprintf( Buffer, TEXT("can't open the key : %d"), GetLastError() ) ;
		MessageBox( hMainWnd, Buffer, szWinName, MB_OK ) ;
		return FALSE ;
	}
	ValueSize = sizeof ValueData ;
	memset( ValueData, 0, sizeof ValueData ) ;
	lret = RegQueryValueEx( hKey, ValueName, NULL, &ValueType, ValueData, &ValueSize ) ;
	if ( lret != ERROR_SUCCESS ) {
		RegCloseKey( hKey ) ;
		MessageBox( hMainWnd, TEXT("can't read the value"), ValueName, MB_OK ) ;
		return FALSE ;
	}
	RegCloseKey( hKey ) ;
	if ( !GetValueData( Buffer, sizeof Buffer / sizeof Buffer[0] ) ) {
		MessageBox( hMainWnd, TEXT("registry data too large"), ValueName, MB_OK ) ;
		return FALSE ;
	}
	if ( ValueType == REG_MULTI_SZ ) {
		if ( !DialogBox( (HANDLE) hInst,
						  MAKEINTRESOURCE(IDD_DLG_MULTI),
						  hMainWnd, (DLGPROC) InputBoxDlgProc ) ) {
			return FALSE ;
		}
	} else {
		if ( !DialogBox( (HANDLE) hInst,
						  MAKEINTRESOURCE(IDD_DLG_SINGLE),
						  hMainWnd, (DLGPROC) InputBoxDlgProc ) ) {
			return FALSE ;
		}
	}
	if ( !PutValueData( Buffer, sizeof Buffer / sizeof Buffer[0] ) ) {
		return FALSE ;
	}
	lret = RegOpenKeyEx( hKeyRoot, RegSubKey, 0,
#ifdef	_WIN32_WCE
						0,
#else	/* _WIN32_WCE */
						KEY_ALL_ACCESS,
#endif	/* _WIN32_WCE */
						&hKey ) ;
	if ( lret != ERROR_SUCCESS ) {
		wsprintf( Buffer, TEXT("can't open the key : %d"), GetLastError() ) ;
		MessageBox( hMainWnd, Buffer, szWinName, MB_OK ) ;
		return FALSE ;
	}
	lret = RegSetValueEx( hKey, ValueName, 0, ValueType, ValueData, ValueSize ) ;
	if ( lret != ERROR_SUCCESS ) {
		RegCloseKey( hKey ) ;
		MessageBox( hMainWnd, TEXT("can't write the value"), ValueName, MB_OK ) ;
		return FALSE ;
	}
	RegCloseKey( hKey ) ;
	return TRUE ;
}

BOOL
DeleteValue( LPCTSTR name )
{
	HKEY	hKey ;
	LONG	lret ;

	_tcscpy( ValueName, name ) ;
	lret = RegOpenKeyEx( hKeyRoot, RegSubKey, 0,
#ifdef	_WIN32_WCE
						0,
#else	/* _WIN32_WCE */
						KEY_ALL_ACCESS,
#endif	/* _WIN32_WCE */
						&hKey ) ;
	if ( lret != ERROR_SUCCESS ) {
		wsprintf( Buffer, TEXT("can't open the key : %d"), GetLastError() ) ;
		MessageBox( hMainWnd, Buffer, szWinName, MB_OK ) ;
		return FALSE ;
	}
	wsprintf( Buffer, TEXT("Delete Value : %s"), ValueName ) ;
	if ( MessageBox( hMainWnd, TEXT("Are you sure?"), Buffer, MB_YESNO ) != IDYES ) {
		RegCloseKey( hKey ) ;
		return FALSE ;
	}
	RegDeleteValue( hKey, ValueName ) ;
	RegCloseKey( hKey ) ;
	return TRUE ;
}

BOOL
NewKey( void )
{
	HKEY	hKey ;
	LONG	lret ;
	DWORD	disposition ;
	TCHAR	subkey[ MAX_PATH ] ;

	if ( hKeyRoot == INVALID_HANDLE_VALUE ) {
		MessageBeep( MB_ICONHAND ) ;
		return FALSE ;
	}
	if ( !DialogBox( (HANDLE) hInst,
					  MAKEINTRESOURCE(IDD_DLG_KEY),
					  hMainWnd, (DLGPROC) NewKeyDlgProc ) ) {
		return FALSE ;
	}
	/* Key偺柤慜傪嶌惉偡傞 */
	_tcscpy( subkey, RegSubKey ) ;
	if ( *subkey && *KeyName ) {
		_tcscat( subkey, TEXT("\\") ) ;
	}
	_tcscat( subkey, KeyName ) ;
	/* Key傪嶌惉偡傞 */
	lret = RegCreateKeyEx( hKeyRoot, subkey, 0, ClassName, 0, 0, NULL, &hKey, &disposition ) ;
	if ( lret != ERROR_SUCCESS ) {
		MessageBox( hMainWnd, TEXT("can't create the key"), szWinName, MB_OK ) ;
		return FALSE ;
	}
	RegCloseKey( hKey ) ;
	return TRUE ;
}

BOOL
DeleteKey( LPCTSTR keyname )
{
	TCHAR	subkey[ MAX_PATH ] ;

	if ( hKeyRoot == INVALID_HANDLE_VALUE ) {
		return FALSE ;
	}
	/* Key偺柤慜傪嶌惉偡傞 */
	_tcscpy( subkey, RegSubKey ) ;
	if ( *subkey && *keyname ) {
		_tcscat( subkey, TEXT("\\") ) ;
	}
	_tcscat( subkey, keyname ) ;
	if ( MessageBox( hMainWnd, TEXT("Are you sure you want to delete this key?"), TEXT("Confirm Key Delete"), MB_YESNO ) != IDYES ) {
		return FALSE ;
	}
	return RegDeleteKey( hKeyRoot, subkey ) == ERROR_SUCCESS ;
}

BOOL CALLBACK
InputBoxDlgProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
{
	LPTSTR		ptr ;
	HWND		hWnd ;
	LONG		tmp ;

	switch ( message ) {
	case WM_INITDIALOG:
		/* 抣偺柤慜傪愝掕偡傞 */
		hWnd = GetDlgItem( hDlg, IDC_EDT_NAME ) ;
		if ( *ValueName ) {
			SetWindowText( hWnd, ValueName ) ;
			tmp = GetWindowLong( hWnd, GWL_STYLE ) ;
			SetWindowLong( hWnd, GWL_STYLE, tmp | WS_DISABLED ) ;
		}
		/* 抣偺撪梕傪愝掕偡傞 */
		hWnd = GetDlgItem( hDlg, IDC_EDT_DATA ) ;
		SetWindowText( hWnd, Buffer ) ;
		/* 抣偺宆傪愝掕偡傞 */
		if ( !GetTypeStr( &ptr ) ) {
			ptr = TEXT("Unknown") ;
		}
		wsprintf( Buffer, TEXT("%s Editor"), ptr ) ;
		SetWindowText( hDlg, Buffer ) ;
		/* 弶婜僼僅乕僇僗傪寛掕偡傞 */
		hWnd = GetDlgItem( hDlg, *ValueName ? IDC_EDT_DATA : IDC_EDT_NAME ) ;
		SendMessage( hWnd, EM_SETSEL, (WPARAM) 0, (LPARAM) -1 ) ;
		SetFocus( hWnd ) ;
		return FALSE ;

	case WM_COMMAND:
		switch ( LOWORD( wParam ) ) {
		case IDCANCEL:
			EndDialog( hDlg, FALSE ) ;
			return TRUE ;
		case IDOK:
			if ( !*ValueName ) {
				hWnd = GetDlgItem( hDlg, IDC_EDT_NAME ) ;
				GetWindowText( hWnd, ValueName, sizeof ValueName / sizeof (TCHAR) ) ;
				if ( !*ValueName ) {
					SetFocus( hWnd ) ;
					MessageBeep( MB_ICONHAND ) ;
					return TRUE ;
				}
			}
			hWnd = GetDlgItem( hDlg, IDC_EDT_DATA ) ;
			GetWindowText( hWnd, (LPTSTR) Buffer, sizeof Buffer / sizeof (TCHAR) ) ;
			if ( !CheckValueData( Buffer, sizeof Buffer / sizeof (TCHAR) ) ) {
				SetFocus( hWnd ) ;
				MessageBeep( MB_ICONHAND ) ;
				return TRUE ;
			}
			EndDialog( hDlg, TRUE ) ;
			return TRUE ;
		}
	}
	return FALSE ;
}

BOOL CALLBACK
NewKeyDlgProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
{
	HWND		hWnd ;

	switch ( message ) {
	case WM_INITDIALOG:
		hWnd = GetDlgItem( hDlg, IDC_EDT_KEY ) ;
		SetFocus( hWnd ) ;
		return FALSE ;

	case WM_COMMAND:
		switch ( LOWORD( wParam ) ) {
		case IDCANCEL:
			EndDialog( hDlg, FALSE ) ;
			return TRUE ;
		case IDOK:
			*KeyName = 0 ;
			hWnd = GetDlgItem( hDlg, IDC_EDT_KEY ) ;
			GetWindowText( hWnd, KeyName, sizeof KeyName / sizeof (TCHAR) ) ;
			if ( !*KeyName ) {
				SetFocus( hWnd ) ;
				MessageBeep( MB_ICONHAND ) ;
				return TRUE ;
			}
			*ClassName = 0 ;
			hWnd = GetDlgItem( hDlg, IDC_EDT_CLASS ) ;
			GetWindowText( hWnd, ClassName, sizeof ClassName / sizeof (TCHAR) ) ;
			EndDialog( hDlg, TRUE ) ;
			return TRUE ;
		}
	}
	return FALSE ;
}

BOOL CALLBACK
AboutDlgFunc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
{
	switch ( message ) {
	case WM_INITDIALOG:
		/* 僶乕僕儑儞忣曬傪弶婜壔偡傞 */
		wsprintf( MessageBuf, TEXT("%s Ver %s  for WindowsCE"),
				  szWinName, Version ) ;
		SetDlgItemText( hDlg, IDC_VERSION, MessageBuf ) ;
		/* DLL僶乕僕儑儞 */
		if ( DllVersion ) {
			wsprintf( MessageBuf, TEXT("KCTRL.DLL Ver %d.%02d"),
					  DllVersion / 100, DllVersion % 100 ) ;
		} else {
			*MessageBuf = 0 ;
		}
		SetDlgItemText( hDlg, IDC_DLLVERSION, MessageBuf ) ;
		return TRUE ;
	case WM_COMMAND:
		switch ( GET_WM_COMMAND_ID( wParam, lParam ) ) {
		case IDOK:
		case IDCANCEL:
			EndDialog( hDlg, TRUE ) ;
			return TRUE ;
		}
		break ;
	}
	return FALSE ;
}

static	BOOL
ExportKey( LPCTSTR keyname )
{
	TCHAR	subkey[ MAX_PATH ] ;

	if ( hKeyRoot == INVALID_HANDLE_VALUE ) {
		return FALSE ;
	}
	_tcscpy( subkey, RegSubKey ) ;
	if ( *subkey && *keyname ) {
		_tcscat( subkey, TEXT("\\") ) ;
	}
	_tcscat( subkey, keyname ) ;
	return export_reg( hKeyRoot, subkey ) ;
}

⌨️ 快捷键说明

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