xref: /aoo42x/main/jurt/source/pipe/wrapper/wrapper.c (revision f2de8a8b)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "sal/config.h"
25 
26 #include <stddef.h>
27 
28 #include <Windows.h>
29 
30 #include "jni.h"
31 #include "sal/types.h"
32 
33 
34 static HMODULE   module   = NULL;
35 static HINSTANCE hInstDLL = NULL;
36 static CRITICAL_SECTION CriticalSection;
37 
38 void InitWrapper(void) {
39     #define MAXPATH 512
40     wchar_t path[MAXPATH];
41     DWORD size;
42 
43     size = GetModuleFileNameW(hInstDLL, path, MAXPATH);
44     if (size == 0) {
45         abort();
46     }
47     path[size - 5] = L'x'; /* ...\jpipe.dll -> ...\jpipx.dll */
48     module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
49     if (module == NULL) {
50         abort();
51     }
52 }
53 
54 static FARPROC getFunction(char const * name)
55 {
56     {
57         EnterCriticalSection(&CriticalSection);
58 
59         if(module == NULL)
60             InitWrapper();
61 
62         LeaveCriticalSection(&CriticalSection);
63     }
64 
65     return GetProcAddress(module, name);
66 }
67 
68 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
69     (void) lpvReserved;
70 
71     if (fdwReason == DLL_PROCESS_ATTACH)
72     {
73         InitializeCriticalSection(&CriticalSection);
74         hInstDLL = hinstDLL;
75     }
76 
77     return TRUE;
78 }
79 
80 SAL_DLLPUBLIC_EXPORT void JNICALL
81 Java_com_sun_star_lib_connections_pipe_PipeConnection_createJNI(
82     JNIEnv * env, jobject obj_this, jstring name)
83 {
84     (*(void (*)(JNIEnv *, jobject, jstring))
85      getFunction("PipeConnection_create"))(env, obj_this, name);
86 }
87 
88 SAL_DLLPUBLIC_EXPORT void JNICALL
89 Java_com_sun_star_lib_connections_pipe_PipeConnection_closeJNI(
90     JNIEnv * env, jobject obj_this)
91 {
92     (*(void (*)(JNIEnv *, jobject))
93      getFunction("PipeConnection_close"))(env, obj_this);
94 }
95 
96 SAL_DLLPUBLIC_EXPORT jint JNICALL
97 Java_com_sun_star_lib_connections_pipe_PipeConnection_readJNI(
98     JNIEnv * env, jobject obj_this, jobjectArray buffer, jint len)
99 {
100     return (*(jint (*)(JNIEnv *, jobject, jobjectArray, jint))
101             getFunction("PipeConnection_read"))(env, obj_this, buffer, len);
102 }
103 
104 SAL_DLLPUBLIC_EXPORT void JNICALL
105 Java_com_sun_star_lib_connections_pipe_PipeConnection_writeJNI(
106     JNIEnv * env, jobject obj_this, jbyteArray buffer)
107 {
108     (*(void (*)(JNIEnv *, jobject, jbyteArray))
109      getFunction("PipeConnection_write"))(env, obj_this, buffer);
110 }
111 
112 SAL_DLLPUBLIC_EXPORT void JNICALL
113 Java_com_sun_star_lib_connections_pipe_PipeConnection_flushJNI(
114     JNIEnv * env, jobject obj_this)
115 {
116     (*(void (*)(JNIEnv *, jobject))
117      getFunction("PipeConnection_flush"))(env, obj_this);
118 }
119