remove dll late binding code from asf. not only is it super ugly, it is not

thread safe.  change to use GetProcAddress the same way everyone else in the
world does.  (M-x un-bloom-ify)
This commit is contained in:
Doug MacEachern 2005-06-26 01:11:20 +00:00
parent 0532dd1e1d
commit 5c9c3c6cf2
1 changed files with 21 additions and 133 deletions

View File

@ -20,147 +20,35 @@ extern "C" {
id = env->GetFieldID(cls, field, "I"); \ id = env->GetFieldID(cls, field, "I"); \
env->SetIntField(obj, id, val) env->SetIntField(obj, id, val)
/** typedef DWORD (CALLBACK *ChangeServiceConfig2_func_t)(SC_HANDLE,
* This code is stolen from misc/win32/misc.c and apr_private.h DWORD, LPVOID);
* 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
* <http://www.apache.org/>.
*
* 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 */
JNIEXPORT jboolean SIGAR_JNI(win32_Service_ChangeServiceDescription) JNIEXPORT jboolean SIGAR_JNI(win32_Service_ChangeServiceDescription)
(JNIEnv *env, jclass, jlong handle, jstring description) (JNIEnv *env, jclass, jlong handle, jstring description)
{ {
jboolean bResult = TRUE; jboolean result = FALSE;
SERVICE_DESCRIPTION servdesc; 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 */ if (change_config) {
osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); servdesc.lpDescription =
GetVersionEx(&osver); (LPTSTR)env->GetStringChars(description, NULL);
if ((osver.dwPlatformId == VER_PLATFORM_WIN32_NT) result = change_config((SC_HANDLE)handle,
&& (osver.dwMajorVersion > 4) SERVICE_CONFIG_DESCRIPTION, &servdesc);
&& (ChangeServiceConfig2 != NULL)) { env->ReleaseStringChars(description,
servdesc.lpDescription = (LPTSTR)env->GetStringChars(description, (const jchar *)servdesc.lpDescription);
NULL); }
bResult = ChangeServiceConfig2((SC_HANDLE)handle,
SERVICE_CONFIG_DESCRIPTION, &servdesc); FreeLibrary(lib);
env->ReleaseStringChars(description,
(const jchar *)servdesc.lpDescription);
} }
return bResult;
return result;
} }
JNIEXPORT jboolean SIGAR_JNI(win32_Service_CloseServiceHandle) JNIEXPORT jboolean SIGAR_JNI(win32_Service_CloseServiceHandle)