7030249: Eliminate use of LoadLibrary and other clean-ups
authoralanb
Mon, 04 Apr 2011 18:35:16 +0100
changeset 9027 540bd52f1881
parent 9026 cdf11e673c21
child 9028 fa0ffd6693db
7030249: Eliminate use of LoadLibrary and other clean-ups Reviewed-by: ohair, chegar, mchung
jdk/make/java/java/Makefile
jdk/make/java/management/Makefile
jdk/src/windows/native/com/sun/management/OperatingSystem_md.c
jdk/src/windows/native/java/io/WinNTFileSystem_md.c
jdk/src/windows/native/java/lang/java_props_md.c
jdk/src/windows/native/sun/management/FileSystemImpl.c
jdk/src/windows/native/sun/nio/fs/WindowsNativeDispatcher.c
jdk/src/windows/native/sun/security/provider/WinCAPISeedGenerator.c
--- a/jdk/make/java/java/Makefile	Mon Apr 04 18:12:46 2011 +0100
+++ b/jdk/make/java/java/Makefile	Mon Apr 04 18:35:16 2011 +0100
@@ -198,10 +198,12 @@
 
 #
 # What to link?
+# On Windows, shell32 is not normally required and so it is delay loaded.
 #
 ifeq ($(PLATFORM),windows)
 OTHER_LDLIBS += $(JVMLIB) -libpath:$(OBJDIR)/../../../fdlibm/$(OBJDIRNAME) fdlibm.lib \
-		       -libpath:$(OBJDIR)/../../../verify/$(OBJDIRNAME) verify.lib
+		       -libpath:$(OBJDIR)/../../../verify/$(OBJDIRNAME) verify.lib \
+		       shell32.lib delayimp.lib /DELAYLOAD:shell32.dll
 else
 OTHER_LDLIBS += $(JVMLIB) -lverify $(LIBSOCKET) $(LIBNSL) -ldl \
 		-L$(OBJDIR)/../../../fdlibm/$(OBJDIRNAME) -lfdlibm.$(ARCH)
--- a/jdk/make/java/management/Makefile	Mon Apr 04 18:12:46 2011 +0100
+++ b/jdk/make/java/management/Makefile	Mon Apr 04 18:35:16 2011 +0100
@@ -86,7 +86,8 @@
   -I$(SHARE_SRC)/native/sun/management 
 
 ifeq ($(PLATFORM),windows)
-  OTHER_LDLIBS += $(JVMLIB)
+  # Need process status helper API (psapi) on Windows
+  OTHER_LDLIBS += $(JVMLIB) psapi.lib
 endif
 
 #
--- a/jdk/src/windows/native/com/sun/management/OperatingSystem_md.c	Mon Apr 04 18:12:46 2011 +0100
+++ b/jdk/src/windows/native/com/sun/management/OperatingSystem_md.c	Mon Apr 04 18:35:16 2011 +0100
@@ -30,6 +30,7 @@
 #include "management.h"
 #include "com_sun_management_OperatingSystem.h"
 
+#include <psapi.h>
 #include <errno.h>
 #include <stdlib.h>
 
@@ -53,41 +54,12 @@
   return result;
 }
 
-// From psapi.h
-typedef struct _PROCESS_MEMORY_COUNTERS {
-    DWORD cb;
-    DWORD PageFaultCount;
-    SIZE_T PeakWorkingSetSize;
-    SIZE_T WorkingSetSize;
-    SIZE_T QuotaPeakPagedPoolUsage;
-    SIZE_T QuotaPagedPoolUsage;
-    SIZE_T QuotaPeakNonPagedPoolUsage;
-    SIZE_T QuotaNonPagedPoolUsage;
-    SIZE_T PagefileUsage;
-    SIZE_T PeakPagefileUsage;
-} PROCESS_MEMORY_COUNTERS;
-
-static HINSTANCE hInstPsapi = NULL;
-typedef BOOL (WINAPI *LPFNGETPROCESSMEMORYINFO)(HANDLE, PROCESS_MEMORY_COUNTERS*, DWORD);
-
-static jboolean is_nt = JNI_FALSE;
 static HANDLE main_process;
 
 JNIEXPORT void JNICALL
 Java_com_sun_management_OperatingSystem_initialize
   (JNIEnv *env, jclass cls)
 {
-    OSVERSIONINFO oi;
-    oi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
-    GetVersionEx(&oi);
-    switch(oi.dwPlatformId) {
-        case VER_PLATFORM_WIN32_WINDOWS: is_nt = JNI_FALSE; break;
-        case VER_PLATFORM_WIN32_NT:      is_nt = JNI_TRUE;  break;
-        default:
-            throw_internal_error(env, "Unsupported Platform");
-            return;
-    }
-
     main_process = GetCurrentProcess();
 }
 
@@ -95,31 +67,12 @@
 Java_com_sun_management_OperatingSystem_getCommittedVirtualMemorySize0
   (JNIEnv *env, jobject mbean)
 {
-
-    /*
-     * In bytes.  NT/2000/XP only - using GetProcessMemoryInfo from psapi.dll
-     */
-    static LPFNGETPROCESSMEMORYINFO lpfnGetProcessMemoryInfo = NULL;
-    static volatile jboolean psapi_inited = JNI_FALSE;
     PROCESS_MEMORY_COUNTERS pmc;
-
-    if (!is_nt) return -1;
-
-    if (!psapi_inited) {
-        psapi_inited = JNI_TRUE;
-        if ((hInstPsapi = LoadLibrary("PSAPI.DLL")) == NULL) return -1;
-        if ((lpfnGetProcessMemoryInfo = (LPFNGETPROCESSMEMORYINFO)
-               GetProcAddress( hInstPsapi, "GetProcessMemoryInfo")) == NULL) {
-            FreeLibrary(hInstPsapi);
-            return -1;
-        }
+    if (GetProcessMemoryInfo(main_process, &pmc, sizeof(PROCESS_MEMORY_COUNTERS)) == 0) {
+        return (jlong)-1L;
+    } else {
+        return (jlong) pmc.PagefileUsage;
     }
-
-    if (lpfnGetProcessMemoryInfo == NULL) return -1;
-
-    lpfnGetProcessMemoryInfo(main_process, &pmc,
-                             sizeof(PROCESS_MEMORY_COUNTERS));
-    return (jlong) pmc.PagefileUsage;
 }
 
 JNIEXPORT jlong JNICALL
@@ -148,20 +101,15 @@
     FILETIME process_creation_time, process_exit_time,
              process_user_time, process_kernel_time;
 
-    // Windows NT only
-    if (is_nt) {
-        // Using static variables declared above
-        // Units are 100-ns intervals.  Convert to ns.
-        GetProcessTimes(main_process, &process_creation_time,
-                        &process_exit_time,
-                        &process_kernel_time, &process_user_time);
-        return (jlong_from(process_user_time.dwHighDateTime,
-                           process_user_time.dwLowDateTime) +
-               jlong_from(process_kernel_time.dwHighDateTime,
-                           process_kernel_time.dwLowDateTime)) * 100;
-    } else {
-        return -1;
-    }
+    // Using static variables declared above
+    // Units are 100-ns intervals.  Convert to ns.
+    GetProcessTimes(main_process, &process_creation_time,
+                    &process_exit_time,
+                    &process_kernel_time, &process_user_time);
+    return (jlong_from(process_user_time.dwHighDateTime,
+                        process_user_time.dwLowDateTime) +
+            jlong_from(process_kernel_time.dwHighDateTime,
+                        process_kernel_time.dwLowDateTime)) * 100;
 }
 
 JNIEXPORT jlong JNICALL
--- a/jdk/src/windows/native/java/io/WinNTFileSystem_md.c	Mon Apr 04 18:12:46 2011 +0100
+++ b/jdk/src/windows/native/java/io/WinNTFileSystem_md.c	Mon Apr 04 18:35:16 2011 +0100
@@ -23,9 +23,9 @@
  * questions.
  */
 
-/* Access APIs for Win2K and above */
+/* Access APIs for WinXP and above */
 #ifndef _WIN32_WINNT
-#define _WIN32_WINNT 0x0500
+#define _WIN32_WINNT 0x0501
 #endif
 
 #include <assert.h>
@@ -60,13 +60,17 @@
 JNIEXPORT void JNICALL
 Java_java_io_WinNTFileSystem_initIDs(JNIEnv *env, jclass cls)
 {
-    HANDLE handle;
+    HMODULE handle;
     jclass fileClass = (*env)->FindClass(env, "java/io/File");
     if (!fileClass) return;
     ids.path =
              (*env)->GetFieldID(env, fileClass, "path", "Ljava/lang/String;");
-    handle = LoadLibrary("kernel32");
-    if (handle != NULL) {
+
+    // GetFinalPathNameByHandle requires Windows Vista or newer
+    if (GetModuleHandleExW((GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
+                            GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT),
+                           (LPCWSTR)&CreateFileW, &handle) != 0)
+    {
         GetFinalPathNameByHandle_func = (GetFinalPathNameByHandleProc)
             GetProcAddress(handle, "GetFinalPathNameByHandleW");
     }
@@ -824,8 +828,6 @@
     return ret;
 }
 
-typedef BOOL (WINAPI* GetVolumePathNameProc) (LPCWSTR, LPWSTR, DWORD);
-
 JNIEXPORT jlong JNICALL
 Java_java_io_WinNTFileSystem_getSpace0(JNIEnv *env, jobject this,
                                        jobject file, jint t)
@@ -834,14 +836,7 @@
     jlong rv = 0L;
     WCHAR *pathbuf = fileToNTPath(env, file, ids.path);
 
-    HMODULE h = LoadLibrary("kernel32");
-    GetVolumePathNameProc getVolumePathNameW = NULL;
-    if (h) {
-        getVolumePathNameW
-            = (GetVolumePathNameProc)GetProcAddress(h, "GetVolumePathNameW");
-    }
-
-    if (getVolumePathNameW(pathbuf, volname, MAX_PATH_LENGTH)) {
+    if (GetVolumePathNameW(pathbuf, volname, MAX_PATH_LENGTH)) {
         ULARGE_INTEGER totalSpace, freeSpace, usableSpace;
         if (GetDiskFreeSpaceExW(volname, &usableSpace, &totalSpace, &freeSpace)) {
             switch(t) {
@@ -860,9 +855,6 @@
         }
     }
 
-    if (h) {
-        FreeLibrary(h);
-    }
     free(pathbuf);
     return rv;
 }
--- a/jdk/src/windows/native/java/lang/java_props_md.c	Mon Apr 04 18:12:46 2011 +0100
+++ b/jdk/src/windows/native/java/lang/java_props_md.c	Mon Apr 04 18:35:16 2011 +0100
@@ -196,42 +196,23 @@
 /*
  * Code to figure out the user's home directory using shell32.dll
  */
-typedef HRESULT (WINAPI *GetSpecialFolderType)(HWND, int, LPITEMIDLIST *);
-typedef BOOL (WINAPI *GetPathFromIDListType)(LPCITEMIDLIST, LPSTR);
-
 WCHAR*
 getHomeFromShell32()
 {
-    HMODULE lib = LoadLibraryW(L"SHELL32.DLL");
-    GetSpecialFolderType do_get_folder;
-    GetPathFromIDListType do_get_path;
     HRESULT rc;
     LPITEMIDLIST item_list = 0;
     WCHAR *p;
     WCHAR path[MAX_PATH+1];
     int size = MAX_PATH+1;
 
-    if (lib == 0) {
-        // We can't load the library !!??
-        return NULL;
-    }
-
-    do_get_folder = (GetSpecialFolderType)GetProcAddress(lib, "SHGetSpecialFolderLocation");
-    do_get_path = (GetPathFromIDListType)GetProcAddress(lib, "SHGetPathFromIDListW");
-
-    if (do_get_folder == 0 || do_get_path == 0) {
-        // the library doesn't hold the right functions !!??
-        return NULL;
-    }
-
-    rc = (*do_get_folder)(NULL, CSIDL_DESKTOPDIRECTORY, &item_list);
+    rc = SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOPDIRECTORY, &item_list);
     if (!SUCCEEDED(rc)) {
         // we can't find the shell folder.
         return NULL;
     }
 
     path[0] = 0;
-    (*do_get_path)(item_list, (LPSTR)path);
+    SHGetPathFromIDListW(item_list, (LPWSTR)path);
 
     /* Get the parent of Desktop directory */
     p = wcsrchr(path, L'\\');
@@ -253,17 +234,7 @@
 static boolean
 haveMMX(void)
 {
-    boolean mmx = 0;
-    HMODULE lib = LoadLibrary("KERNEL32");
-    if (lib != NULL) {
-        BOOL (WINAPI *isProcessorFeaturePresent)(DWORD) =
-            (BOOL (WINAPI *)(DWORD))
-            GetProcAddress(lib, "IsProcessorFeaturePresent");
-        if (isProcessorFeaturePresent != NULL)
-            mmx = isProcessorFeaturePresent(PF_MMX_INSTRUCTIONS_AVAILABLE);
-        FreeLibrary(lib);
-    }
-    return mmx;
+    return IsProcessorFeaturePresent(PF_MMX_INSTRUCTIONS_AVAILABLE);
 }
 
 static const char *
@@ -532,10 +503,19 @@
         if (uname != NULL && wcslen(uname) > 0) {
             sprops.user_name = _wcsdup(uname);
         } else {
-            WCHAR buf[100];
-            int buflen = sizeof(buf);
-            sprops.user_name =
-                GetUserNameW(buf, &buflen) ? _wcsdup(buf) : L"unknown";
+            DWORD buflen = 0;
+            if (GetUserNameW(NULL, &buflen) == 0 &&
+                GetLastError() == ERROR_INSUFFICIENT_BUFFER)
+            {
+                uname = (WCHAR*)malloc(buflen * sizeof(WCHAR));
+                if (uname != NULL && GetUserNameW(uname, &buflen) == 0) {
+                    free(uname);
+                    uname = NULL;
+                }
+            } else {
+                uname = NULL;
+            }
+            sprops.user_name = (uname != NULL) ? uname : L"unknown";
         }
     }
 
@@ -633,8 +613,8 @@
     /* Current directory */
     {
         WCHAR buf[MAX_PATH];
-        GetCurrentDirectoryW(sizeof(buf), buf);
-        sprops.user_dir = _wcsdup(buf);
+        if (GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR), buf) != 0)
+            sprops.user_dir = _wcsdup(buf);
     }
 
     sprops.file_separator = "\\";
--- a/jdk/src/windows/native/sun/management/FileSystemImpl.c	Mon Apr 04 18:12:46 2011 +0100
+++ b/jdk/src/windows/native/sun/management/FileSystemImpl.c	Mon Apr 04 18:35:16 2011 +0100
@@ -37,45 +37,6 @@
 #define ANY_ACCESS (FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE)
 
 /*
- * Function prototypes for security functions - we can't statically
- * link because these functions aren't on Windows 9x.
- */
-typedef BOOL (WINAPI *GetFileSecurityFunc)
-    (LPCTSTR lpFileName, SECURITY_INFORMATION RequestedInformation,
-     PSECURITY_DESCRIPTOR pSecurityDescriptor, DWORD nLength,
-     LPDWORD lpnLengthNeeded);
-
-typedef BOOL (WINAPI *GetSecurityDescriptorOwnerFunc)
-    (PSECURITY_DESCRIPTOR pSecurityDescriptor, PSID *pOwner,
-     LPBOOL lpbOwnerDefaulted);
-
-typedef BOOL (WINAPI *GetSecurityDescriptorDaclFunc)
-    (PSECURITY_DESCRIPTOR pSecurityDescriptor, LPBOOL lpbDaclPresent,
-     PACL *pDacl, LPBOOL lpbDaclDefaulted);
-
-typedef BOOL (WINAPI *GetAclInformationFunc)
-    (PACL pAcl, LPVOID pAclInformation, DWORD nAclInformationLength,
-     ACL_INFORMATION_CLASS dwAclInformationClass);
-
-typedef BOOL (WINAPI *GetAceFunc)
-    (PACL pAcl, DWORD dwAceIndex, LPVOID *pAce);
-
-typedef BOOL (WINAPI *EqualSidFunc)(PSID pSid1, PSID pSid2);
-
-
-/* Addresses of the security functions */
-static GetFileSecurityFunc GetFileSecurity_func;
-static GetSecurityDescriptorOwnerFunc GetSecurityDescriptorOwner_func;
-static GetSecurityDescriptorDaclFunc GetSecurityDescriptorDacl_func;
-static GetAclInformationFunc GetAclInformation_func;
-static GetAceFunc GetAce_func;
-static EqualSidFunc EqualSid_func;
-
-/* True if this OS is NT kernel based (NT/2000/XP) */
-static int isNT;
-
-
-/*
  * Returns JNI_TRUE if the specified file is on a file system that supports
  * persistent ACLs (On NTFS file systems returns true, on FAT32 file systems
  * returns false).
@@ -165,7 +126,7 @@
     SECURITY_INFORMATION info =
         OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
 
-    (*GetFileSecurity_func)(path, info , 0, 0, &len);
+    GetFileSecurityA(path, info , 0, 0, &len);
     if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
         JNU_ThrowIOExceptionWithLastError(env, "GetFileSecurity failed");
         return NULL;
@@ -174,7 +135,7 @@
     if (sd == NULL) {
         JNU_ThrowOutOfMemoryError(env, 0);
     } else {
-        if (!(*GetFileSecurity_func)(path, info, sd, len, &len)) {
+        if (!(*GetFileSecurityA)(path, info, sd, len, &len)) {
             JNU_ThrowIOExceptionWithLastError(env, "GetFileSecurity failed");
             free(sd);
             return NULL;
@@ -191,7 +152,7 @@
     SID* owner;
     BOOL defaulted;
 
-    if (!(*GetSecurityDescriptorOwner_func)(sd, &owner, &defaulted)) {
+    if (!GetSecurityDescriptorOwner(sd, &owner, &defaulted)) {
         JNU_ThrowIOExceptionWithLastError(env, "GetSecurityDescriptorOwner failed");
         return NULL;
     }
@@ -206,7 +167,7 @@
     ACL *acl;
     int defaulted, present;
 
-    if (!(*GetSecurityDescriptorDacl_func)(sd, &present, &acl, &defaulted)) {
+    if (!GetSecurityDescriptorDacl(sd, &present, &acl, &defaulted)) {
         JNU_ThrowIOExceptionWithLastError(env, "GetSecurityDescriptorDacl failed");
         return NULL;
     }
@@ -235,8 +196,8 @@
     /*
      * Get the ACE count
      */
-    if (!(*GetAclInformation_func)(acl, (void *) &acl_size_info, sizeof(acl_size_info),
-                                  AclSizeInformation)) {
+    if (!GetAclInformation(acl, (void *) &acl_size_info, sizeof(acl_size_info),
+                           AclSizeInformation)) {
         JNU_ThrowIOExceptionWithLastError(env, "GetAclInformation failed");
         return JNI_FALSE;
     }
@@ -250,7 +211,7 @@
         ACCESS_ALLOWED_ACE *access;
         SID* sid;
 
-        if (!(*GetAce_func)(acl, i, &ace)) {
+        if (!GetAce(acl, i, &ace)) {
             JNU_ThrowIOExceptionWithLastError(env, "GetAce failed");
             return -1;
         }
@@ -280,51 +241,7 @@
 JNIEXPORT void JNICALL Java_sun_management_FileSystemImpl_init0
   (JNIEnv *env, jclass ignored)
 {
-    OSVERSIONINFO ver;
-    HINSTANCE hInst;
-
-    /*
-     * Get the OS version. If dwPlatformId is VER_PLATFORM_WIN32_NT
-     * it means we're running on a Windows NT, 2000, or XP machine.
-     */
-    ver.dwOSVersionInfoSize = sizeof(ver);
-    GetVersionEx(&ver);
-    isNT = (ver.dwPlatformId == VER_PLATFORM_WIN32_NT);
-    if (!isNT) {
-        return;
-    }
-
-    /*
-     * On NT/2000/XP we need the addresses of the security functions
-     */
-    hInst = LoadLibrary("ADVAPI32.DLL");
-    if (hInst == NULL) {
-        JNU_ThrowIOExceptionWithLastError(env, "Unable to load ADVAPI32.DLL");
-        return;
-    }
-
-
-    GetFileSecurity_func = (GetFileSecurityFunc)GetProcAddress(hInst, "GetFileSecurityA");
-    GetSecurityDescriptorOwner_func =
-        (GetSecurityDescriptorOwnerFunc)GetProcAddress(hInst, "GetSecurityDescriptorOwner");
-    GetSecurityDescriptorDacl_func =
-        (GetSecurityDescriptorDaclFunc)GetProcAddress(hInst, "GetSecurityDescriptorDacl");
-    GetAclInformation_func =
-        (GetAclInformationFunc)GetProcAddress(hInst, "GetAclInformation");
-    GetAce_func = (GetAceFunc)GetProcAddress(hInst, "GetAce");
-    EqualSid_func = (EqualSidFunc)GetProcAddress(hInst, "EqualSid");
-
-    if (GetFileSecurity_func == NULL ||
-        GetSecurityDescriptorDacl_func == NULL ||
-        GetSecurityDescriptorDacl_func == NULL ||
-        GetAclInformation_func == NULL ||
-        GetAce_func == NULL ||
-        EqualSid_func == NULL)
-    {
-        JNU_ThrowIOExceptionWithLastError(env,
-            "Unable to get address of security functions");
-        return;
-    }
+        /* nothing to do */
 }
 
 /*
@@ -339,10 +256,6 @@
     jboolean isCopy;
     const char* path;
 
-    if (!isNT) {
-        return JNI_FALSE;
-    }
-
     path = JNU_GetStringPlatformChars(env, str, &isCopy);
     if (path != NULL) {
         res = isSecuritySupported(env, path);
--- a/jdk/src/windows/native/sun/nio/fs/WindowsNativeDispatcher.c	Mon Apr 04 18:12:46 2011 +0100
+++ b/jdk/src/windows/native/sun/nio/fs/WindowsNativeDispatcher.c	Mon Apr 04 18:35:16 2011 +0100
@@ -24,7 +24,7 @@
  */
 
 #ifndef _WIN32_WINNT
-#define _WIN32_WINNT 0x0500
+#define _WIN32_WINNT 0x0501
 #endif
 
 #include <stdio.h>
@@ -36,6 +36,7 @@
 #include <windows.h>
 #include <aclapi.h>
 #include <winioctl.h>
+#include <Sddl.h>
 
 #include "jni.h"
 #include "jni_util.h"
@@ -77,40 +78,20 @@
 
 
 /**
- * Win32 APIs not defined in Visual Studio 2003 header files
+ * Win32 APIs not available in Windows XP
  */
-
-typedef enum {
-  FindStreamInfoStandard
-} MY_STREAM_INFO_LEVELS;
-
-typedef struct _MY_WIN32_FIND_STREAM_DATA {
-  LARGE_INTEGER StreamSize;
-  WCHAR cStreamName[MAX_PATH + 36];
-} MY_WIN32_FIND_STREAM_DATA;
-
-typedef HANDLE (WINAPI* FindFirstStream_Proc)(LPCWSTR, MY_STREAM_INFO_LEVELS, LPVOID, DWORD);
+typedef HANDLE (WINAPI* FindFirstStream_Proc)(LPCWSTR, STREAM_INFO_LEVELS, LPVOID, DWORD);
 typedef BOOL (WINAPI* FindNextStream_Proc)(HANDLE, LPVOID);
 
 typedef BOOLEAN (WINAPI* CreateSymbolicLinkProc) (LPCWSTR, LPCWSTR, DWORD);
-typedef BOOL (WINAPI* CreateHardLinkProc) (LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
 typedef BOOL (WINAPI* GetFinalPathNameByHandleProc) (HANDLE, LPWSTR, DWORD, DWORD);
 
-typedef BOOL (WINAPI* ConvertSidToStringSidProc) (PSID, LPWSTR*);
-typedef BOOL (WINAPI* ConvertStringSidToSidProc) (LPWSTR, PSID*);
-typedef DWORD (WINAPI* GetLengthSidProc) (PSID);
-
 static FindFirstStream_Proc FindFirstStream_func;
 static FindNextStream_Proc FindNextStream_func;
 
 static CreateSymbolicLinkProc CreateSymbolicLink_func;
-static CreateHardLinkProc CreateHardLink_func;
 static GetFinalPathNameByHandleProc GetFinalPathNameByHandle_func;
 
-static ConvertSidToStringSidProc ConvertSidToStringSid_func;
-static ConvertStringSidToSidProc ConvertStringSidToSid_func;
-static GetLengthSidProc GetLengthSid_func;
-
 static void throwWindowsException(JNIEnv* env, DWORD lastError) {
     jobject x = JNU_NewObjectByName(env, "sun/nio/fs/WindowsException",
         "(I)V", lastError);
@@ -190,33 +171,23 @@
     backupResult_bytesTransferred = (*env)->GetFieldID(env, clazz, "bytesTransferred", "I");
     backupResult_context = (*env)->GetFieldID(env, clazz, "context", "J");
 
-
-    h = LoadLibrary("kernel32");
-    if (h != INVALID_HANDLE_VALUE) {
+    // get handle to kernel32
+    if (GetModuleHandleExW((GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
+                            GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT),
+                           (LPCWSTR)&CreateFileW, &h) != 0)
+    {
+        // requires Windows Server 2003 or newer
         FindFirstStream_func =
             (FindFirstStream_Proc)GetProcAddress(h, "FindFirstStreamW");
         FindNextStream_func =
             (FindNextStream_Proc)GetProcAddress(h, "FindNextStreamW");
+
+        // requires Windows Vista or newer
         CreateSymbolicLink_func =
             (CreateSymbolicLinkProc)GetProcAddress(h, "CreateSymbolicLinkW");
-        CreateHardLink_func =
-            (CreateHardLinkProc)GetProcAddress(h, "CreateHardLinkW");
         GetFinalPathNameByHandle_func =
             (GetFinalPathNameByHandleProc)GetProcAddress(h, "GetFinalPathNameByHandleW");
-        FreeLibrary(h);
     }
-
-    h = LoadLibrary("advapi32");
-    if (h != INVALID_HANDLE_VALUE) {
-        ConvertSidToStringSid_func =
-            (ConvertSidToStringSidProc)GetProcAddress(h, "ConvertSidToStringSidW");
-        ConvertStringSidToSid_func =
-            (ConvertStringSidToSidProc)GetProcAddress(h, "ConvertStringSidToSidW");
-        GetLengthSid_func =
-            (GetLengthSidProc)GetProcAddress(h, "GetLengthSid");
-        FreeLibrary(h);
-    }
-
 }
 
 JNIEXPORT jstring JNICALL
@@ -413,7 +384,7 @@
 Java_sun_nio_fs_WindowsNativeDispatcher_FindFirstStream0(JNIEnv* env, jclass this,
     jlong address, jobject obj)
 {
-    MY_WIN32_FIND_STREAM_DATA data;
+    WIN32_FIND_STREAM_DATA data;
     LPCWSTR lpFileName = jlong_to_ptr(address);
     HANDLE handle;
 
@@ -443,7 +414,7 @@
 Java_sun_nio_fs_WindowsNativeDispatcher_FindNextStream(JNIEnv* env, jclass this,
     jlong handle)
 {
-    MY_WIN32_FIND_STREAM_DATA data;
+    WIN32_FIND_STREAM_DATA data;
     HANDLE h = (HANDLE)jlong_to_ptr(handle);
 
     if (FindNextStream_func == NULL) {
@@ -909,12 +880,7 @@
     jclass this, jlong address)
 {
     PSID sid = jlong_to_ptr(address);
-
-    if (GetLengthSid_func == NULL) {
-        JNU_ThrowInternalError(env, "Should not get here");
-        return 0;
-    }
-    return (jint)(*GetLengthSid_func)(sid);
+    return (jint)GetLengthSid(sid);
 }
 
 
@@ -924,13 +890,7 @@
 {
     PSID sid = jlong_to_ptr(address);
     LPWSTR string;
-
-    if (ConvertSidToStringSid_func == NULL) {
-        JNU_ThrowInternalError(env, "Should not get here");
-        return NULL;
-    }
-
-    if ((*ConvertSidToStringSid_func)(sid, &string) == 0) {
+    if (ConvertSidToStringSidW(sid, &string) == 0) {
         throwWindowsException(env, GetLastError());
         return NULL;
     } else {
@@ -947,15 +907,8 @@
 {
     LPWSTR lpStringSid = jlong_to_ptr(address);
     PSID pSid;
-
-    if (ConvertStringSidToSid_func == NULL) {
-        JNU_ThrowInternalError(env, "Should not get here");
-        return (jlong)0;
-    }
-
-    if ((*ConvertStringSidToSid_func)(lpStringSid, &pSid) == 0)
+    if (ConvertStringSidToSidW(lpStringSid, &pSid) == 0)
         throwWindowsException(env, GetLastError());
-
     return ptr_to_jlong(pSid);
 }
 
@@ -1137,11 +1090,7 @@
     LPCWSTR newFile = jlong_to_ptr(newFileAddress);
     LPCWSTR existingFile = jlong_to_ptr(existingFileAddress);
 
-    if (CreateHardLink_func == NULL) {
-        JNU_ThrowInternalError(env, "Should not get here");
-        return;
-    }
-    if ((*CreateHardLink_func)(newFile, existingFile, NULL) == 0)
+    if (CreateHardLinkW(newFile, existingFile, NULL) == 0)
         throwWindowsException(env, GetLastError());
 }
 
--- a/jdk/src/windows/native/sun/security/provider/WinCAPISeedGenerator.c	Mon Apr 04 18:12:46 2011 +0100
+++ b/jdk/src/windows/native/sun/security/provider/WinCAPISeedGenerator.c	Mon Apr 04 18:35:16 2011 +0100
@@ -33,11 +33,6 @@
 #include <jni.h>
 #include "sun_security_provider_NativeSeedGenerator.h"
 
-/* Typedefs for runtime linking. */
-typedef BOOL (WINAPI *CryptAcquireContextType)(HCRYPTPROV*, LPCTSTR, LPCTSTR, DWORD, DWORD);
-typedef BOOL (WINAPI *CryptGenRandomType)(HCRYPTPROV, DWORD, BYTE*);
-typedef BOOL (WINAPI *CryptReleaseContextType)(HCRYPTPROV, DWORD);
-
 /*
  * Get a random seed from the MS CryptoAPI. Return true if successful, false
  * otherwise.
@@ -49,48 +44,27 @@
 JNIEXPORT jboolean JNICALL Java_sun_security_provider_NativeSeedGenerator_nativeGenerateSeed
   (JNIEnv *env, jclass clazz, jbyteArray randArray)
 {
-    HMODULE lib;
-    CryptAcquireContextType acquireContext;
-    CryptGenRandomType genRandom;
-    CryptReleaseContextType releaseContext;
-
     HCRYPTPROV hCryptProv;
     jboolean result = JNI_FALSE;
     jsize numBytes;
     jbyte* randBytes;
 
-    lib = LoadLibrary("ADVAPI32.DLL");
-    if (lib == NULL) {
-        return result;
-    }
-
-    acquireContext = (CryptAcquireContextType)GetProcAddress(lib, "CryptAcquireContextA");
-    genRandom = (CryptGenRandomType)GetProcAddress(lib, "CryptGenRandom");
-    releaseContext = (CryptReleaseContextType)GetProcAddress(lib, "CryptReleaseContext");
-
-    if (acquireContext == NULL || genRandom == NULL || releaseContext == NULL) {
-        FreeLibrary(lib);
-        return result;
-    }
-
-    if (acquireContext(&hCryptProv, "J2SE", NULL, PROV_RSA_FULL, 0) == FALSE) {
+    if (CryptAcquireContextA(&hCryptProv, "J2SE", NULL, PROV_RSA_FULL, 0) == FALSE) {
         /* If CSP context hasn't been created, create one. */
-        if (acquireContext(&hCryptProv, "J2SE", NULL, PROV_RSA_FULL,
+        if (CryptAcquireContextA(&hCryptProv, "J2SE", NULL, PROV_RSA_FULL,
                 CRYPT_NEWKEYSET) == FALSE) {
-            FreeLibrary(lib);
             return result;
         }
     }
 
     numBytes = (*env)->GetArrayLength(env, randArray);
     randBytes = (*env)->GetByteArrayElements(env, randArray, NULL);
-    if (genRandom(hCryptProv, numBytes, randBytes)) {
+    if (CryptGenRandom(hCryptProv, numBytes, randBytes)) {
         result = JNI_TRUE;
     }
     (*env)->ReleaseByteArrayElements(env, randArray, randBytes, 0);
 
-    releaseContext(hCryptProv, 0);
-    FreeLibrary(lib);
+    CryptReleaseContext(hCryptProv, 0);
 
     return result;
 }