-
Notifications
You must be signed in to change notification settings - Fork 0
/
adll.cpp
206 lines (163 loc) · 6.11 KB
/
adll.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright 2010 Keng-Yu Lin <[email protected]>
*
* This file is part of Array31.
*
* Array31 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Array31 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Array31. If not, see <http://www.gnu.org/licenses/>.
*/
#define UNICODE
#include "aclassfactory.h"
#include "acandidatewindows.h"
#include "aarrayts.h"
#include <string.h>
#include <olectl.h>
#include <Windows.h>
#include <Msctf.h>
BOOL WINAPI DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID pvReserved )
{
ACandidateWindow::hInstance = hInstance;
switch( dwReason )
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
if( gArrayTS )
{
delete gArrayTS;
}
break;
}
return TRUE;
}
STDAPI DllGetClassObject( REFCLSID rclsid, REFIID riid, void ** ppInterface )
{
// rclsid is the factory's class id
// riid is the interface to be queried in the factory
if( rclsid != CLSID_ARRAY_31 )
return CLASS_E_CLASSNOTAVAILABLE;
else
{
AClassFactory * pAClassFactory = new AClassFactory;
if( ! pAClassFactory )
return E_OUTOFMEMORY;
HRESULT hR = pAClassFactory->QueryInterface( riid, ppInterface );
if( FAILED( hR ) )
{
delete pAClassFactory;
return hR;
}
else
return NOERROR;
}
}
STDAPI DllCanUnloadNow()
{
//TRACEX( "DllCanUnloadNow\n\n\n" );
if( AClassFactory::m_cObj == 0 && AClassFactory::m_cLock == 0 )
return S_OK;
else
return S_FALSE;
}
STDAPI DllRegisterServer( void )
{
// HKEY_CLASSES_ROOT\CLSID\{}\InProcServer32 = c:\*.dll
// wget CLSID string from arrayts.h: CLSID_ARRAY_31
wstring clsid = L"CLSID\\";
wchar_t guid[ 256 ];
if( ! StringFromGUID2( CLSID_ARRAY_31, guid, 255 ) )
return SELFREG_E_CLASS;
clsid += guid;
// create clsid
HKEY hkey;
DWORD dw;
if( RegCreateKeyExW( HKEY_CLASSES_ROOT, clsid.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &dw) != ERROR_SUCCESS )
return SELFREG_E_CLASS;
if( RegSetValueExA( hkey, NULL, 0, REG_SZ, (BYTE *) "ARRAY_31", 9 ) != ERROR_SUCCESS )
return SELFREG_E_CLASS;
//InProcServer32
HKEY hsubkey;
if( RegCreateKeyExA( hkey, "InprocServer32", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hsubkey, &dw) != ERROR_SUCCESS )
return SELFREG_E_CLASS;
char path[ 256 ];
int szPath;
if( ( szPath = GetModuleFileNameA( ACandidateWindow::hInstance, path, 255 ) ) == 0 )
return SELFREG_E_CLASS;
if( RegSetValueExA( hsubkey, NULL, 0, REG_SZ, (BYTE *) path, szPath + 1 ) != ERROR_SUCCESS )
return SELFREG_E_CLASS;
if( RegSetValueExA( hsubkey, "ThreadingModel", 0, REG_SZ, (BYTE *) "Apartment", 10 ) != ERROR_SUCCESS )
return SELFREG_E_CLASS;
// DefaultIME
if( RegCreateKeyExW( HKEY_CURRENT_USER, L"Software\\Array31", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &dw) != ERROR_SUCCESS )
return SELFREG_E_CLASS;
if( RegSetValueExA( hkey, "DefaultIME", 0, REG_SZ, (BYTE *) "30", 3 ) != ERROR_SUCCESS )
return SELFREG_E_CLASS;
RegCloseKey( hkey );
RegCloseKey( hsubkey );
// Register with TSF
ITfInputProcessorProfiles * pInputProcessProfiles;
HRESULT hr = CoCreateInstance( CLSID_TF_InputProcessorProfiles, NULL, CLSCTX_INPROC_SERVER, IID_ITfInputProcessorProfiles, ( LPVOID* ) & pInputProcessProfiles );
if( hr != S_OK ) return SELFREG_E_CLASS;
// Register
hr = pInputProcessProfiles->Register( CLSID_ARRAY_31 );
if( hr != S_OK ) return SELFREG_E_CLASS;
// Language Profile
WCHAR wpath[ 256 ];
szPath = GetModuleFileNameW( ACandidateWindow::hInstance, wpath, 255 );
/*
int l( 0 );
for( int i = 0; i < szPath; ++i )
if( wpath[i] == L'\\' )
l = i;
wpath[ l + 1 ] = 0;
WCHAR * wpath_ico = wcscat( wpath, L"array31.ico" );
*/ WCHAR * wpath_ico = wpath;
hr = pInputProcessProfiles->AddLanguageProfile( CLSID_ARRAY_31, MAKELANGID( LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL ), AArrayTextService::gLangProfile, L"行列輸入法(3.1α6)", -1, wpath_ico, -1, 0 );
if( hr != S_OK ) return SELFREG_E_CLASS;
pInputProcessProfiles->Release();
// Register category
ITfCategoryMgr * pCatMgr;
hr = CoCreateInstance( CLSID_TF_CategoryMgr, NULL, CLSCTX_INPROC_SERVER, IID_ITfCategoryMgr, ( void ** ) & pCatMgr );
if( hr != S_OK ) return SELFREG_E_CLASS;
hr = pCatMgr->RegisterCategory( CLSID_ARRAY_31, GUID_TFCAT_TIP_KEYBOARD, CLSID_ARRAY_31 );
if( hr != S_OK ) return SELFREG_E_CLASS;
hr = pCatMgr->RegisterCategory( CLSID_ARRAY_31, GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER, CLSID_ARRAY_31 );
if( hr != S_OK ) return SELFREG_E_CLASS;
pCatMgr->Release();
return S_OK;
}
STDAPI DllUnregisterServer( void )
{
HRESULT hr;
// Unregister category
ITfCategoryMgr * pCatMgr;
hr = CoCreateInstance( CLSID_TF_CategoryMgr, NULL, CLSCTX_INPROC_SERVER, IID_ITfCategoryMgr, ( void ** ) & pCatMgr );
if( hr != S_OK ) return SELFREG_E_CLASS;
hr = pCatMgr->UnregisterCategory( CLSID_ARRAY_31, GUID_TFCAT_TIP_KEYBOARD, CLSID_ARRAY_31 );
if( hr != S_OK ) return SELFREG_E_CLASS;
hr = pCatMgr->UnregisterCategory( CLSID_ARRAY_31, GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER, CLSID_ARRAY_31 );
if( hr != S_OK ) return SELFREG_E_CLASS;
pCatMgr->Release();
// Unegister with TSF
ITfInputProcessorProfiles * pInputProcessProfiles;
hr = CoCreateInstance( CLSID_TF_InputProcessorProfiles, NULL, CLSCTX_INPROC_SERVER, IID_ITfInputProcessorProfiles, ( LPVOID* ) & pInputProcessProfiles );
if( hr != S_OK ) return SELFREG_E_CLASS;
GUID guidprofile;
if( pInputProcessProfiles->RemoveLanguageProfile( CLSID_ARRAY_31, MAKELANGID( LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL ), guidprofile ) != S_OK )
return SELFREG_E_CLASS;
// Unregister
hr = pInputProcessProfiles->Unregister( CLSID_ARRAY_31 );
if( hr != S_OK ) return SELFREG_E_CLASS;
pInputProcessProfiles->Release();
return S_OK;
}