xref: /core/jurt/source/pipe/wrapper/wrapper.c (revision 928b1b04)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <stddef.h>
23 #include <stdlib.h>
24 
25 #define WIN32_LEAN_AND_MEAN
26 #include <windows.h>
27 
28 #include <jni.h>
29 #include <sal/types.h>
30 
31 
32 static HMODULE   module   = NULL;
33 static HINSTANCE hInstDLL = NULL;
34 static CRITICAL_SECTION CriticalSection;
35 
InitWrapper(void)36 static void InitWrapper(void) {
37     #define MAXPATH 512
38     wchar_t path[MAXPATH];
39     DWORD size;
40 
41     size = GetModuleFileNameW(hInstDLL, path, MAXPATH);
42     if (size == 0) {
43         abort();
44     }
45     path[size - 5] = L'x'; /* ...\jpipe.dll -> ...\jpipx.dll */
46     module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
47     if (module == NULL) {
48         abort();
49     }
50 }
51 
getFunction(char const * name)52 static FARPROC getFunction(char const * name)
53 {
54     {
55         EnterCriticalSection(&CriticalSection);
56 
57         if(module == NULL)
58             InitWrapper();
59 
60         LeaveCriticalSection(&CriticalSection);
61     }
62 
63     return GetProcAddress(module, name);
64 }
65 
DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)66 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
67     (void) lpvReserved;
68 
69     if (fdwReason == DLL_PROCESS_ATTACH)
70     {
71         InitializeCriticalSection(&CriticalSection);
72         hInstDLL = hinstDLL;
73     }
74 
75     return TRUE;
76 }
77 
78 SAL_DLLPUBLIC_EXPORT void JNICALL
Java_com_sun_star_lib_connections_pipe_PipeConnection_createJNI(JNIEnv * env,jobject obj_this,jstring name)79 Java_com_sun_star_lib_connections_pipe_PipeConnection_createJNI(
80     JNIEnv * env, jobject obj_this, jstring name)
81 {
82     (*(void (*)(JNIEnv *, jobject, jstring))
83      getFunction("PipeConnection_create"))(env, obj_this, name);
84 }
85 
86 SAL_DLLPUBLIC_EXPORT void JNICALL
Java_com_sun_star_lib_connections_pipe_PipeConnection_closeJNI(JNIEnv * env,jobject obj_this)87 Java_com_sun_star_lib_connections_pipe_PipeConnection_closeJNI(
88     JNIEnv * env, jobject obj_this)
89 {
90     (*(void (*)(JNIEnv *, jobject))
91      getFunction("PipeConnection_close"))(env, obj_this);
92 }
93 
94 SAL_DLLPUBLIC_EXPORT jint JNICALL
Java_com_sun_star_lib_connections_pipe_PipeConnection_readJNI(JNIEnv * env,jobject obj_this,jobjectArray buffer,jint len)95 Java_com_sun_star_lib_connections_pipe_PipeConnection_readJNI(
96     JNIEnv * env, jobject obj_this, jobjectArray buffer, jint len)
97 {
98     return (*(jint (*)(JNIEnv *, jobject, jobjectArray, jint))
99             getFunction("PipeConnection_read"))(env, obj_this, buffer, len);
100 }
101 
102 SAL_DLLPUBLIC_EXPORT void JNICALL
Java_com_sun_star_lib_connections_pipe_PipeConnection_writeJNI(JNIEnv * env,jobject obj_this,jbyteArray buffer)103 Java_com_sun_star_lib_connections_pipe_PipeConnection_writeJNI(
104     JNIEnv * env, jobject obj_this, jbyteArray buffer)
105 {
106     (*(void (*)(JNIEnv *, jobject, jbyteArray))
107      getFunction("PipeConnection_write"))(env, obj_this, buffer);
108 }
109 
110 SAL_DLLPUBLIC_EXPORT void JNICALL
Java_com_sun_star_lib_connections_pipe_PipeConnection_flushJNI(JNIEnv * env,jobject obj_this)111 Java_com_sun_star_lib_connections_pipe_PipeConnection_flushJNI(
112     JNIEnv * env, jobject obj_this)
113 {
114     (*(void (*)(JNIEnv *, jobject))
115      getFunction("PipeConnection_flush"))(env, obj_this);
116 }
117 
118 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
119