diff --git a/bindings/java/src/jni/win32/service.cpp b/bindings/java/src/jni/win32/service.cpp index ed5308d1..d4eb12c4 100644 --- a/bindings/java/src/jni/win32/service.cpp +++ b/bindings/java/src/jni/win32/service.cpp @@ -20,147 +20,35 @@ extern "C" { id = env->GetFieldID(cls, field, "I"); \ env->SetIntField(obj, id, val) -/** - * This code is stolen from misc/win32/misc.c and apr_private.h - * This helper code resolves late bound entry points - * missing from one or more releases of the Win32 API... - * This is covered under the Apache Software License - * - * ==================================================================== - * The Apache Software License, Version 1.1 - * - * Copyright (c) 2000-2003 The Apache Software Foundation. All rights - * reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. The end-user documentation included with the redistribution, - * if any, must include the following acknowledgment: - * "This product includes software developed by the - * Apache Software Foundation (http://www.apache.org/)." - * Alternately, this acknowledgment may appear in the software itself, - * if and wherever such third-party acknowledgments normally appear. - * - * 4. The names "Apache" and "Apache Software Foundation" must - * not be used to endorse or promote products derived from this - * software without prior written permission. For written - * permission, please contact apache@apache.org. - * - * 5. Products derived from this software may not be called "Apache", - * nor may "Apache" appear in their name, without prior written - * permission of the Apache Software Foundation. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - * Portions of this software are based upon public domain software - * originally written at the National Center for Supercomputing Applications, - * University of Illinois, Urbana-Champaign. - */ - -typedef enum { - DLL_WINBASEAPI = 0, // kernel32 From WinBase.h - DLL_WINADVAPI = 1, // advapi32 From WinBase.h - DLL_WINSOCKAPI = 2, // mswsock From WinSock.h - DLL_WINSOCK2API = 3, // ws2_32 From WinSock2.h - DLL_SHSTDAPI = 4, // shell32 From ShellAPI.h - DLL_NTDLL = 5, // shell32 From our real kernel - DLL_defined = 6 // must define as last idx_ + 1 -} dlltoken_e; - -FARPROC load_dll_func(dlltoken_e fnLib, TCHAR* fnName, int ordinal); - -#define DECLARE_LATE_DLL_FUNC(lib, rettype, calltype, fn, ord, args, names) \ -typedef rettype (calltype *winapi_fpt_##fn) args; \ -static winapi_fpt_##fn winapi_pfn_##fn = NULL; \ -__inline rettype winapi_##fn args \ -{ if (!winapi_pfn_##fn) \ -winapi_pfn_##fn = (winapi_fpt_##fn) load_dll_func(lib, (wchar_t *)#fn, ord); \ -return (*(winapi_pfn_##fn)) names; }; \ - -/* Win2K kernel only */ -DECLARE_LATE_DLL_FUNC(DLL_WINADVAPI, BOOL, WINAPI, - ChangeServiceConfig2W, 0, - (SC_HANDLE hService, - DWORD dwInfoLevel, - LPVOID lpInfo), - (hService, dwInfoLevel, lpInfo)); -#undef ChangeServiceConfig2 -#define ChangeServiceConfig2 winapi_ChangeServiceConfig2W - -/* End Apache licensed code */ - -static TCHAR* lateDllName[] = { - L"kernel32", L"advapi32", L"mswsock", L"ws2_32" }; -static HMODULE lateDllHandle[] = { - NULL, NULL, NULL, NULL }; - -FARPROC load_dll_func(dlltoken_e fnLib, TCHAR* fnName, int ordinal) -{ - if (!lateDllHandle[fnLib]) { - lateDllHandle[fnLib] = LoadLibrary(lateDllName[fnLib]); - if (!lateDllHandle[fnLib]) { - DWORD err = GetLastError(); - fprintf(stderr, "GetLastError(): %d %s\r\n", - err, lateDllName[fnLib]); - return NULL; - } - } - if (ordinal) - return GetProcAddress(lateDllHandle[fnLib], (char *)ordinal); - else - return GetProcAddress(lateDllHandle[fnLib], (char *)fnName); -} - -/*End ASF licensed code */ +typedef DWORD (CALLBACK *ChangeServiceConfig2_func_t)(SC_HANDLE, + DWORD, LPVOID); JNIEXPORT jboolean SIGAR_JNI(win32_Service_ChangeServiceDescription) (JNIEnv *env, jclass, jlong handle, jstring description) { - jboolean bResult = TRUE; + jboolean result = FALSE; SERVICE_DESCRIPTION servdesc; + HINSTANCE lib; + ChangeServiceConfig2_func_t change_config; + + if ((lib = LoadLibrary(L"advapi32"))) { + change_config = (ChangeServiceConfig2_func_t) + GetProcAddress(lib, "ChangeServiceConfig2W"); - OSVERSIONINFO osver; /* VER_PLATFORM_WIN32_NT */ - osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - GetVersionEx(&osver); + if (change_config) { + servdesc.lpDescription = + (LPTSTR)env->GetStringChars(description, NULL); - if ((osver.dwPlatformId == VER_PLATFORM_WIN32_NT) - && (osver.dwMajorVersion > 4) - && (ChangeServiceConfig2 != NULL)) { - servdesc.lpDescription = (LPTSTR)env->GetStringChars(description, - NULL); - bResult = ChangeServiceConfig2((SC_HANDLE)handle, - SERVICE_CONFIG_DESCRIPTION, &servdesc); - env->ReleaseStringChars(description, - (const jchar *)servdesc.lpDescription); + result = change_config((SC_HANDLE)handle, + SERVICE_CONFIG_DESCRIPTION, &servdesc); + env->ReleaseStringChars(description, + (const jchar *)servdesc.lpDescription); + } + + FreeLibrary(lib); } - return bResult; + + return result; } JNIEXPORT jboolean SIGAR_JNI(win32_Service_CloseServiceHandle)