www.pudn.com > SimpleInput.rar > HWRecognizer.cpp


// HWRecognizer.cpp : Defines the entry point for the DLL application. 
// 
 
#include "stdafx.h" 
#include "IMWnd.h" 
#include "InputMethod.h" 
#include "ClassFactory.h" 
#include "Common.h" 
 
long g_DllCnt = 0;                   // Global DLL reference count 
    
extern "C" { 
HINSTANCE g_hInst;                     // DLL instance handle 
} 
 
// GUID defines for  input method.  Create a new one with GUIDGEN. 
/* 
const GUID CLSID_HWXRecognizer = { 0xc8311f61, 0x12df,0x4107,{0xb5,0xea,0xb0,0xb0,0xd5,0x5c,0xec,0x50}}; 
const TCHAR szCLSIDHWXRecognizer[] = TEXT ("{C8311F61-12DF-4107-B5EA-B0B0D55CEC50}"); 
*/ 
 
/* 
const GUID CLSID_HWXRecognizer = { 0x9E06C6B8, 0xFE4D,0x4206,{0x9A,0x5B,0x6E,0x4C,0x1B,0x8B,0x6A,0xD7}}; 
const TCHAR szCLSIDHWXRecognizer[] = TEXT ("{9E06C6B8-FE4D-4206-9A5B-6E4C1B8B6AD7}"); 
*/ 
 
 
const GUID CLSID_HWXRecognizer = { 0x0CA0AAFA, 0xE999,0x4993,{0x9A,0xAA,0x4F,0xB0,0xEC,0xE2,0x8A,0x8D}}; 
const TCHAR szCLSIDHWXRecognizer[] = TEXT ("{0CA0AAFA-E999-4993-9AAA-4FB0ECE28A8D}"); 
 
const TCHAR szFriendlyName[] = TEXT ("Numeric Keypad"); 
    
    
//====================================================================== 
// DllMain - DLL initialization entry point 
// 
BOOL APIENTRY DllMain( HANDLE hModule,  
                       DWORD  ul_reason_for_call,  
                       LPVOID lpReserved 
					 ) 
{	 
	g_hInst = (HINSTANCE)hModule; 
	 
	WRITELOG("Get in the DllMain()!"); 
     
	return TRUE; 
} 
 
//====================================================================== 
// DllGetClassObject - Exported function called to get pointer to  
// Class factory object. 
// 
STDAPI DllGetClassObject (REFCLSID rclsid, REFIID riid, LPVOID *ppv)  
{ 
    CClassFactory *pcf; 
    HRESULT hr; 
    
	 
    // See if caller wants us. 
    if (IsEqualCLSID (rclsid, CLSID_HWXRecognizer))  
	{    
        // Create IClassFactory object. 
        pcf = new CClassFactory(&g_DllCnt,g_hInst); 
        if (pcf == NULL) 
            return E_OUTOFMEMORY; 
    
        // Call class factory's query interface method. 
        hr = pcf->QueryInterface (riid, ppv); 
        // This will cause an obj delete unless interface found. 
        pcf->Release(); 
		 
		WRITELOG("Oh, created IClassFactory object!"); 
 
        return hr; 
    } 
	 
	WRITELOG("Sorry, ClassFactory object doesn't create"); 
 
    return CLASS_E_CLASSNOTAVAILABLE; 
} 
//====================================================================== 
// DllCanUnloadNow - Exported function called when DLL can unload 
// 
STDAPI DllCanUnloadNow ()  
{ 
    
    if (g_DllCnt) 
	{ 
		WRITELOG("Dll unload now!"); 
        return S_FALSE; 
    } 
	return S_OK; 
} 
//====================================================================== 
// DllRegisterServer - Exported function called to register the server 
// 
STDAPI DllRegisterServer ()  
{ 
    TCHAR szName[MAX_PATH+2]; 
    TCHAR szTmp[128]; 
    DWORD dwDisp; 
    HKEY hKey, hSubKey; 
    int rc; 
    
    GetModuleFileName (g_hInst, szName, sizeof (szName)); 
    // Open the key. 
    wsprintf (szTmp, TEXT ("CLSID\\%s"), szCLSIDHWXRecognizer); 
    rc = RegCreateKeyEx (HKEY_CLASSES_ROOT, szTmp, 0, TEXT(""),  
                         0, 0, NULL, &hKey, &dwDisp); 
    if (rc != ERROR_SUCCESS) 
        return E_FAIL; 
    // Set the friendly name of the SIP. 
    RegSetValueEx (hKey, TEXT (""), 0, REG_SZ, (PBYTE)szFriendlyName, 
                        (lstrlen (szFriendlyName)+1) * sizeof (TCHAR)); 
    
    // Create subkeys. 
    // Set the module name of the SIP. 
    rc = RegCreateKeyEx (hKey, TEXT ("InProcServer32"), 0, TEXT(""),  
                         0, 0, NULL, &hSubKey, &dwDisp); 
    rc = RegSetValueEx (hSubKey, TEXT (""), 0, REG_SZ, (PBYTE)szName, 
                        (lstrlen (szName)+1) * sizeof (TCHAR)); 
    RegCloseKey (hSubKey); 
    
    // Set the default icon of the server. 
    RegCreateKeyEx (hKey, TEXT ("DefaultIcon"), 0, TEXT(""),  
                    0, 0, NULL, &hSubKey, &dwDisp); 
    lstrcat (szName, TEXT (",0")); 
    RegSetValueEx (hSubKey, TEXT (""), 0, REG_SZ, (PBYTE)szName, 
                   (lstrlen (szName)+1) * sizeof (TCHAR)); 
    RegCloseKey (hSubKey); 
    
    // Set the flag indicating this is a SIP. 
    RegCreateKeyEx (hKey, TEXT ("IsSIPInputMethod"), 0, TEXT(""),  
                    0, 0, NULL, &hSubKey, &dwDisp); 
    lstrcpy (szTmp, TEXT ("1")); 
    RegSetValueEx (hSubKey, TEXT (""), 0, REG_SZ, (PBYTE)szTmp, 4); 
    RegCloseKey (hSubKey); 
    
    RegCloseKey (hKey); 
 
	WRITELOG("DllRegisterServer is OK"); 
 
    return S_OK; 
} 
//====================================================================== 
// DllUnregisterServer - Exported function called to remove the server 
// information from the registry 
// 
STDAPI DllUnregisterServer()  
{ 
    int rc; 
    TCHAR szTmp[128]; 
    
    wsprintf (szTmp, TEXT ("CLSID\\%s"), szCLSIDHWXRecognizer); 
    rc = RegDeleteKey (HKEY_CLASSES_ROOT, szTmp); 
    if (rc == ERROR_SUCCESS) 
	{ 
		WRITELOG("DllUnregisterServer is OK"); 
        return S_OK; 
	} 
 
	WRITELOG("DllUnregisterServer is FAILED"); 
    return E_FAIL; 
}