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