www.pudn.com > freepy.rar > stub.cpp


// 
// Copyright (c) Microsoft Corporation.  All rights reserved. 
// 
// 
// Use of this source code is subject to the terms of the Microsoft end-user 
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT. 
// If you did not accept the terms of the EULA, you are not authorized to use 
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your 
// install media. 
// 
// 
// 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
 
 
Module Name: 
 
    Stub.c 
 
Abstract: 
 
    Sample IME for Windows CE. 
 
[Environment:] 
 
    Runs on all of Windows CE platform. 
 
[Notes:] 
 
    This code and information is provided "as is" without warranty of 
    any kind, either expressed or implied, including but not limited to 
    the implied warranties of merchantability and/or fitness for a 
    particular purpose. 
 
-------------------------------------------------------------------*/ 
#include "freepy.h" 
 
#define KEYBUFFLEN 300 
 
static TCHAR *RegKeyRoot = _T("SOFTWARE\\Microsoft\\TestIME\0"); 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
 
Function: 
 
    GetWindowsDirectory 
 
[Owner:] 
 
    Hidei 
 
Description: 
 
    Retrieces the path of the windows directory. 
 
[Arguments:] 
 
    OUT LPTSTR  lpBuffer - Pointer to buffer to receive the 
                           null-terminated string containing the path. 
    IN  UINT    uSize    - Specifies the maximum size, in characters, 
                           of the buffer specified by the lpBuffer 
                           parameter. 
 
[Return Value:] 
 
    Returns the length, in characters, of the string copied to the 
    buffer, not including the terminating null character. 
 
-------------------------------------------------------------------*/ 
 
UINT 
WINAPI 
GetWindowsDirectory( 
    OUT LPTSTR lpBuffer,    // Pointer to buffer to receive the null- 
                            // terminated string containg the path. 
    IN  UINT uSize          // Specifies the maximum size, in 
                            // characters, of the buffer specified by 
                            // the lpBuffer parameter. 
    ) 
{ 
    static TCHAR *ptcWindowsDir = _T("\\Windows\0"); 
 
    if ( lpBuffer ) 
        lstrcpy( lpBuffer, ptcWindowsDir ); 
    return lstrlen( ptcWindowsDir ); 
} 
 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
 
Function: 
 
    lstrcpyn 
 
[Owner:] 
 
    Hidei 
 
Description: 
 
    Copies a specified number of characters from a source string 
    into a buffer. 
 
[Arguments:] 
 
    OUT LPTSTR  lpString1  - Pointer to destination buffer. 
    IN  LPCTSTR lpString2  - Pointer to source buffer. 
    IN  int     iMaxLength - Number of bytes or characters to copy. 
 
[Return Value:] 
 
    Returns pointer to distinarion buffer, if succeed. NULL otherwise. 
 
-------------------------------------------------------------------*/ 
 
LPTSTR 
lstrcpyn( 
    OUT LPTSTR lpString1,   // Pointer to destination buffer. 
    IN  LPCTSTR lpString2,  // Pointer to source buffer. 
    IN  int iMaxLength      // Number of bytes or characters to copy. 
    ) 
{ 
    TCHAR *lptc1 = lpString1; 
    TCHAR *lptc2 = (LPTSTR)lpString2; 
    int n = iMaxLength; 
 
    while ( *lptc1++ ); 
 
    while ( n-- ) 
        *lptc1++ = *lptc2++; 
 
    *lptc1 = TEXT('\0'); 
    return lpString1; 
} 
 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
 
Function: 
 
    GetPrivateProfileString 
 
[Owner:] 
 
    Hidei 
 
Description: 
 
    Retrieves a string from the specified section in an ini-file. 
 
[Arguments:] 
 
	IN  LPCTSTR lpSecName   - Section name. 
	IN  LPCTSTR lpKeyName   - Key name. 
	IN  LPCTSTR lpDefault   - Default string. 
	OUT LPTSTR  lpString    - Destination buffer. 
	IN  DWORD   nSize       - Size of destination buffer. 
	IN  LPCTSTR lpFileName  - Filename. 
 
[Return Value:] 
 
    Number of copied characters to buffer 
 
-------------------------------------------------------------------*/ 
 
DWORD 
GetPrivateProfileString( 
	LPCTSTR lpSecName,      // Section name. 
	LPCTSTR lpKeyName,      // Key name. 
	LPCTSTR lpDefault,      // Default string. 
	LPTSTR  lpString,       // Destination buffer. 
	DWORD   nSize,          // Size of destination buffer. 
	LPCTSTR lpFileName      // Filename. 
    ) 
{ 
    HKEY    hKey; 
    LONG    lRetCode; 
    TCHAR   KeyBuff[KEYBUFFLEN]; 
    DWORD   dwSize, dwSize2, dwTotalSize; 
    DWORD dwIndex; 
    LPTSTR lpSubKey; 
    LPTSTR lpKeyBuf; 
 
//    FILETIME ft; 
 
    // This stub only supports with case that lpKeyName is NULL. 
    if ( lpKeyName ) { 
        return FALSE; 
    } 
 
    //This stub only supports with case that lpSecName has only uppercase string. 
    lpKeyBuf = (LPTSTR)lpSecName; 
    while(*lpKeyBuf){ 
        if (IsCharLower(*lpKeyBuf)) { 
             return FALSE; 
        } 
        lpKeyBuf++; 
    } 
 
	KeyBuff[0] = TEXT('\0'); 
	StringCchCat( KeyBuff, KEYBUFFLEN, RegKeyRoot); 
    StringCchCat( KeyBuff, KEYBUFFLEN, lpFileName ); 
    StringCchCat( KeyBuff, KEYBUFFLEN, TEXT("\\") ); 
    StringCchCat( KeyBuff, KEYBUFFLEN, lpSecName ); 
 
    lRetCode = RegOpenKeyEx( 
                    HKEY_LOCAL_MACHINE, 
                    KeyBuff, 
                    0, KEY_ALL_ACCESS, &hKey); 
 
    if (lRetCode != ERROR_SUCCESS) { 
        return FALSE; 
    } 
 
    dwTotalSize = 0; 
    dwIndex = 0; 
    lpSubKey = lpString; 
    dwSize = nSize; 
    _tcscpy( lpString, lpDefault ); 
    dwSize = (DWORD)nSize; 
 
    while ( TRUE ) { 
        dwSize2 = dwSize; 
//        lRetCode = RegEnumKeyEx( hKey, dwIndex, lpSubKey, &dwSize2, 
//                                 NULL, NULL, NULL, &ft ); 
        lRetCode = RegEnumValue( hKey, dwIndex, lpSubKey, &dwSize2, 
                                 NULL, NULL, NULL, NULL ); 
 
        if ( lRetCode == ERROR_NO_MORE_ITEMS ) { 
            //lpSubKey += ((_tcslen( lpSubKey )+1) * sizeof (TCHAR) ); 
//            *lpSubKey = (TCHAR)NULL; 
//            dwTotalSize += 1; 
		if(dwIndex > 0){ 
	            *lpSubKey = (TCHAR)NULL; 
	            dwTotalSize += 1; 
		} 
 
            break; 
        } else if ( lRetCode == ERROR_SUCCESS ) { 
            dwTotalSize += dwSize2+1; 
            dwSize -= dwSize2+1; 
            lpSubKey += dwSize2+1; 
            dwIndex++; 
        } else { 
            lstrcpy( lpString, lpDefault ); 
            dwTotalSize = 0; 
            break; 
        } 
    } 
 
    RegCloseKey( hKey ); 
    return dwTotalSize; 
} 
 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
 
Function: 
 
    WritePrivateProfileString 
 
[Owner:] 
 
    Hidei 
 
Description: 
 
    Copies a string into the specified section of the specified ini-file. 
 
[Arguments:] 
 
	IN LPCTSTR  lpSecName  - Section name. 
	IN LPCTSTR  lpKeyName  - Key name. 
	IN LPCTSTR  lpString   - String for specified key. 
	IN LPCTSTR  lpFileName - Filename. 
 
[Return Value:] 
 
    TRUE = success, FALSE = fail 
    if lpString is NULL, then Key name is deleted 
 
-------------------------------------------------------------------*/ 
 
BOOL 
WritePrivateProfileString( 
	LPCTSTR  lpSecName,	    // Section name. 
	LPCTSTR  lpKeyName,	    // Key name. 
	LPCTSTR  lpString,	    // String for specified key. 
	LPCTSTR  lpFileName   	// Filename. 
    ) 
{ 
    HKEY    hKey; 
    DWORD   dwDisposition; 
    LONG    lRetCode; 
    TCHAR   KeyBuff[KEYBUFFLEN]; 
 
// Create the .INI file key 
    StringCchCopy( KeyBuff, KEYBUFFLEN, RegKeyRoot ); 
    StringCchCat( KeyBuff, KEYBUFFLEN, lpFileName ); 
    lRetCode = RegCreateKeyEx( 
                    HKEY_LOCAL_MACHINE, 
                    KeyBuff, 
                    0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 
                    NULL, &hKey, &dwDisposition); 
 
    if (lRetCode != ERROR_SUCCESS) 
        return FALSE; 
 
    RegCloseKey( hKey ); 
 
// Create a Section Name 
    StringCchCat( KeyBuff, KEYBUFFLEN, TEXT("\\") ); 
    StringCchCat( KeyBuff, KEYBUFFLEN, lpSecName ); 
    lRetCode = RegCreateKeyEx( 
                    HKEY_LOCAL_MACHINE, 
                    KeyBuff, 
                    0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 
                    NULL, &hKey, &dwDisposition); 
 
    if (lRetCode != ERROR_SUCCESS) 
        return FALSE; 
 
//    RegCloseKey( hKey ); 
 
// Set Key value 
    lRetCode = RegSetValueEx( hKey, lpKeyName, 0, REG_SZ, 
                    (LPBYTE)lpString, lstrlen(lpString)*sizeof(TCHAR)); 
 
    if (lRetCode != ERROR_SUCCESS){ 
	RegCloseKey( hKey ); 
        return FALSE; 
    } 
 
    RegCloseKey( hKey ); 
 
    return TRUE; 
}