1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_odk.hxx"
30 
31 #if defined _MSC_VER
32 #pragma warning(push, 1)
33 #endif
34 #include <windows.h>
35 #if defined _MSC_VER
36 #pragma warning(pop)
37 #endif
38 
39 #include <jni.h>
40 
41 extern "C" BOOL __stdcall _DllMainCRTStartup(HINSTANCE, DWORD, LPVOID)
42 {
43     return TRUE;
44 }
45 
46 extern "C" JNIEXPORT jboolean JNICALL
47     Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenClassesRoot(
48     JNIEnv *env, jclass, jlongArray hkresult)
49 {
50     jboolean ret = JNI_FALSE;
51     PHKEY phkey = (PHKEY)env->GetLongArrayElements(hkresult, 0);
52     if (RegOpenKeyEx(HKEY_CLASSES_ROOT, NULL, 0, KEY_READ, phkey)
53         == ERROR_SUCCESS)
54         ret = JNI_TRUE;
55     env->ReleaseLongArrayElements(hkresult, (jlong *)phkey, 0);
56     return ret;
57 }
58 
59 extern "C" JNIEXPORT jboolean JNICALL
60     Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenCurrentConfig(
61     JNIEnv *env, jclass, jlongArray hkresult)
62 {
63     jboolean ret = JNI_FALSE;
64     PHKEY phkey = (PHKEY)env->GetLongArrayElements(hkresult, 0);
65     if (RegOpenKeyEx(HKEY_CURRENT_CONFIG, NULL, 0, KEY_READ, phkey)
66         == ERROR_SUCCESS)
67         ret = JNI_TRUE;
68     env->ReleaseLongArrayElements(hkresult, (jlong *)phkey, 0);
69     return ret;
70 }
71 
72 extern "C" JNIEXPORT jboolean JNICALL
73     Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenCurrentUser(
74     JNIEnv *env, jclass, jlongArray hkresult)
75 {
76     jboolean ret = JNI_FALSE;
77     PHKEY phkey = (PHKEY)env->GetLongArrayElements(hkresult, 0);
78     if (RegOpenKeyEx(HKEY_CURRENT_USER, NULL, 0, KEY_READ, phkey)
79         == ERROR_SUCCESS)
80         ret = JNI_TRUE;
81     env->ReleaseLongArrayElements(hkresult, (jlong *)phkey, 0);
82     return ret;
83 }
84 
85 extern "C" JNIEXPORT jboolean JNICALL
86     Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenLocalMachine(
87     JNIEnv *env, jclass, jlongArray hkresult)
88 {
89     jboolean ret = JNI_FALSE;
90     PHKEY phkey = (PHKEY)env->GetLongArrayElements(hkresult, 0);
91     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, NULL, 0, KEY_READ, phkey)
92         == ERROR_SUCCESS)
93         ret = JNI_TRUE;
94     env->ReleaseLongArrayElements(hkresult, (jlong *)phkey, 0);
95     return ret;
96 }
97 
98 extern "C" JNIEXPORT jboolean JNICALL
99     Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenUsers(
100     JNIEnv *env, jclass, jlongArray hkresult)
101 {
102     jboolean ret = JNI_FALSE;
103     PHKEY phkey = (PHKEY)env->GetLongArrayElements(hkresult, 0);
104     if (RegOpenKeyEx(HKEY_USERS, NULL, 0, KEY_READ, phkey) == ERROR_SUCCESS)
105         ret = JNI_TRUE;
106     env->ReleaseLongArrayElements(hkresult, (jlong *)phkey, 0);
107     return ret;
108 }
109 
110 extern "C" JNIEXPORT jboolean JNICALL
111     Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenKeyEx(
112     JNIEnv *env, jclass, jlong parent, jstring name, jlongArray hkresult)
113 {
114     jboolean ret = JNI_FALSE;
115     const char *namestr = env->GetStringUTFChars(name, 0);
116     PHKEY phkey = (PHKEY)env->GetLongArrayElements(hkresult, 0);
117     if (RegOpenKeyEx((HKEY)parent, namestr, 0, KEY_READ, phkey)
118         == ERROR_SUCCESS)
119         ret = JNI_TRUE;
120     env->ReleaseStringUTFChars(name, namestr);
121     env->ReleaseLongArrayElements(hkresult, (jlong *)phkey, 0);
122     return ret;
123 }
124 
125 
126 extern "C" JNIEXPORT jboolean JNICALL
127     Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegCloseKey(
128     JNIEnv *, jclass, jlong hkey)
129 {
130     jboolean ret = JNI_FALSE;
131     if (RegCloseKey((HKEY)hkey) == ERROR_SUCCESS)
132         ret = JNI_TRUE;
133     return ret;
134 }
135 
136 extern "C" JNIEXPORT jboolean
137     JNICALL Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegQueryValueEx(
138     JNIEnv *env, jclass, jlong hkey, jstring value, jlongArray type,
139     jbyteArray data, jlongArray size)
140 {
141     jboolean ret = JNI_FALSE;
142     const char* valuestr = env->GetStringUTFChars(value, 0);
143     LPDWORD ptype = (LPDWORD)env->GetLongArrayElements(type, 0);
144     LPBYTE  pdata = (LPBYTE)env->GetByteArrayElements(data, 0);
145     LPDWORD psize = (LPDWORD)env->GetLongArrayElements(size, 0);
146     if (RegQueryValueEx((HKEY)hkey, valuestr, NULL, ptype, pdata, psize)
147         == ERROR_SUCCESS)
148         ret = JNI_TRUE;
149     env->ReleaseStringUTFChars(value, valuestr);
150     env->ReleaseLongArrayElements(type, (jlong *)ptype, 0);
151     env->ReleaseByteArrayElements(data, (jbyte *)pdata, 0);
152     env->ReleaseLongArrayElements(size, (jlong *)psize, 0);
153     return ret;
154 }
155 
156 extern "C" JNIEXPORT jboolean JNICALL
157     Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegQueryInfoKey(
158     JNIEnv *env, jclass, jlong hkey, jlongArray subkeys,
159     jlongArray maxSubkeyLen, jlongArray values, jlongArray maxValueNameLen,
160     jlongArray maxValueLen, jlongArray secDescriptor)
161 {
162     jboolean ret = JNI_FALSE;
163     LPDWORD psubkeys = (LPDWORD)env->GetLongArrayElements(subkeys, 0);
164     LPDWORD pmaxSubkeyLen =
165         (LPDWORD)env->GetLongArrayElements(maxSubkeyLen, 0);
166     LPDWORD pvalues = (LPDWORD)env->GetLongArrayElements(values, 0);
167     LPDWORD pmaxValueNameLen =
168         (LPDWORD)env->GetLongArrayElements(maxValueNameLen, 0);
169     LPDWORD pmaxValueLen =
170         (LPDWORD)env->GetLongArrayElements(maxValueLen, 0);
171     LPDWORD psecDescriptor =
172         (LPDWORD)env->GetLongArrayElements(secDescriptor, 0);
173     FILETIME ft;
174     if (RegQueryInfoKey((HKEY)hkey, NULL, NULL, NULL, psubkeys, pmaxSubkeyLen,
175                         NULL, pvalues, pmaxValueNameLen, pmaxValueLen,
176                         psecDescriptor, &ft) == ERROR_SUCCESS)
177         ret = JNI_TRUE;
178     env->ReleaseLongArrayElements(subkeys, (jlong*)psubkeys, 0);
179     env->ReleaseLongArrayElements(maxSubkeyLen, (jlong*)pmaxSubkeyLen, 0);
180     env->ReleaseLongArrayElements(values, (jlong*)pvalues, 0);
181     env->ReleaseLongArrayElements(maxValueNameLen, (jlong*)pmaxValueNameLen, 0);
182     env->ReleaseLongArrayElements(maxValueLen, (jlong*)pmaxValueLen, 0);
183     env->ReleaseLongArrayElements(secDescriptor, (jlong*)psecDescriptor, 0);
184     return ret;
185 }
186