forked from kohsuke/com4j
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathregistry.cpp
More file actions
95 lines (79 loc) · 2.02 KB
/
registry.cpp
File metadata and controls
95 lines (79 loc) · 2.02 KB
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
#include "stdafx.h"
#include "com4j_tlbimp_Native.h"
#include "com4j.h"
// HKEY wrapper that automates deallocation
class KeyHolder {
private:
HKEY key;
public:
KeyHolder() { key=NULL; }
~KeyHolder() {
if(key!=NULL)
RegCloseKey(key);
key=NULL;
}
HKEY* operator & () {
return &key;
}
operator HKEY () {
return key;
}
};
JNIEXPORT jstring JNICALL Java_com4j_tlbimp_Native_readRegKey(
JNIEnv* env, jclass clazz, jstring _key ) {
JString key(env,_key);
wchar_t w[256];
long size = sizeof(w);
LONG r = RegQueryValueW(HKEY_CLASSES_ROOT,key,w,&size);
if(r!=0) {
error(env,__FILE__,__LINE__,r,"incorrect key name \"%s\"",static_cast<LPCSTR>(key));
return NULL;
}
return env->NewString(w,(jsize)wcslen(w));
}
JNIEXPORT jobjectArray JNICALL Java_com4j_tlbimp_Native_enumRegKeys(
JNIEnv* env, jclass clazz, jstring _key ) {
JString key(env,_key);
KeyHolder hKey;
LONG r = RegOpenKey(HKEY_CLASSES_ROOT,key,&hKey);
if(r!=0) {
error(env,__FILE__,__LINE__,r,"incorrect key name \"%s\"",static_cast<LPCSTR>(key));
return NULL;
}
DWORD nSubKeys;
RegQueryInfoKey(hKey,NULL,NULL,NULL,&nSubKeys,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
jobjectArray array = env->NewObjectArray( nSubKeys, javaLangString, NULL );
for( DWORD i=0; i<nSubKeys; i++ ) {
wchar_t w[256];
RegEnumKeyW(hKey,i,w,sizeof(w));
env->SetObjectArrayElement( array, i,
env->NewString(w,(jsize)wcslen(w)) );
}
return array;
}
/*
jstring foo( JNIEnv* env, jstring _guid, jstring _version, jstring localeId ) {
KeyHolder libKey;
LONG r;
r = RegOpenKey(HKEY_CLASSES_ROOT,JString(env,_guid),&libKey);
if(r!=0) {
error(env,r,"incorrect LIBID");
return NULL;
}
KeyHolder verKey;
if(_version!=NULL) {
r = RegOpenKey(libKey,JString(env,_version),&verKey);
if(r!=0) {
error(env,r,"incorrect version");
return NULL;
}
} else {
DWORD nSubKeys;
RegQueryInfoKey(libKey,NULL,NULL,NULL,&nSubKeys,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
for( DWORD i=0; i<nSubKeys; i++ ) {
char w[32];
RegEnumKey(libKey,i,w,sizeof(w));
}
}
}
*/