aboutsummaryrefslogtreecommitdiff
path: root/src/pdj-win32.c
blob: c93e53ac91d4070253001ecba0d2f25d1aff6dad (plain)
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
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "pdj.h"

int getuglylibpath(char *path) {
	HMODULE hmodule = GetModuleHandle("pdj.dll");
	char dllPath[BUFFER_SIZE];
	int rc;
	
	if ( hmodule == NULL ) {
		post("pdj: can't get windows dll handle");
		strcpy(path, ".");
		return 1;
	}
		
	rc = GetModuleFileName(hmodule, dllPath, 1023);
	if ( rc == 0 || rc == 1023 ) {
		post("pdj: can't get windows dll path");
		strcpy(path, ".");
		return 1;
	}
	
	dllPath[strlen(dllPath)-8] = 0;
	strcpy(path, dllPath);
	return 0;
}


static char *getJavaHomeFromReg(char *javaHome) {
	char keyName[BUFFER_SIZE] = "Software\\JavaSoft\\Java Development Kit";
	char *dest;
	int size = BUFFER_SIZE;
	HKEY hKey;
	long rc;

	rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyName, 0, KEY_READ, &hKey);
	if ( rc != ERROR_SUCCESS )
		return NULL;

	strcat(keyName, "\\");
	dest = strlen(keyName) + keyName;
	rc = RegQueryValueEx(hKey, "CurrentVersion", NULL, NULL, 
		(LPBYTE) dest, &size);

	RegCloseKey(hKey);

	if ( rc != ERROR_SUCCESS )
		return NULL;

	rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyName, 0, KEY_READ, &hKey);
	if ( rc != ERROR_SUCCESS )
		return NULL;

	size = BUFFER_SIZE;
	rc = RegQueryValueEx(hKey, "JavaHome", NULL, NULL, 
		(LPBYTE) javaHome, &size);

	RegCloseKey(hKey);

	if ( rc != ERROR_SUCCESS ) 
		return NULL;

	return javaHome;
}


JNI_CreateJavaVM_func *linkjvm(char *vm_type) {
	HINSTANCE jvmDll = (HINSTANCE)NULL;
	JNI_CreateJavaVM_func *func;
	char work[BUFFER_SIZE];
	char *javahome = pdj_getProperty("pdj.JAVA_HOME");
	void *libVM;

	// default config or no config ? try 
	if ( javahome == NULL || javahome[0] == '/' ) {
		javahome = NULL;
		javahome = getenv("JAVA_HOME");
		if ( javahome == NULL ) {
			javahome = getJavaHomeFromReg(work);
			if ( javahome == NULL ) {
				post("pdj: unable to find any java VM. Please set JAVA_HOME environment variable");
				return NULL;
			}
		}
	}

	strcpy(work, javahome);
	strcat(work, "\\jre\\bin\\");
	strcat(work, vm_type);
	strcat(work, "\\jvm.dll");
	jvmDll = LoadLibrary(work);

	if ( jvmDll == NULL ) {
		post("post: unable to find jvm.dll in %s", work);
		return NULL;
	}
	func = GetProcAddress(jvmDll, "JNI_CreateJavaVM");
	if ( func == NULL ) {
		post("pdj: unable to find symbol: JNI_CreateJavaVM in jvm.dll ??");
		return NULL;
	}

	post("pdj: using JVM %s", javahome);

	return func;
}