ruby-changes:17704
From: usa <ko1@a...>
Date: Mon, 8 Nov 2010 11:03:46 +0900 (JST)
Subject: [ruby-changes:17704] Ruby:r29714 (trunk): * win32/win32.c (get_proc_address): refactoring.
usa 2010-11-08 11:03:40 +0900 (Mon, 08 Nov 2010) New Revision: 29714 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29714 Log: * win32/win32.c (get_proc_address): refactoring. * win32/win32.c (get_wsa_exetinsion_function): refactoring. Modified files: trunk/ChangeLog trunk/win32/win32.c Index: ChangeLog =================================================================== --- ChangeLog (revision 29713) +++ ChangeLog (revision 29714) @@ -1,3 +1,9 @@ +Mon Nov 8 11:02:21 2010 NAKAMURA Usaku <usa@r...> + + * win32/win32.c (get_proc_address): refactoring. + + * win32/win32.c (get_wsa_exetinsion_function): refactoring. + Mon Nov 8 09:45:35 2010 NARUSE, Yui <naruse@r...> * enc/trans/gbk-tbl.rb: Add euro sign. [ruby-core:33094] Index: win32/win32.c =================================================================== --- win32/win32.c (revision 29713) +++ win32/win32.c (revision 29714) @@ -450,18 +450,37 @@ } } +static FARPROC +get_proc_address(const char *module, const char *func, HANDLE *mh) +{ + HANDLE h; + FARPROC ptr; + + if (mh) + h = LoadLibrary(module); + else + h = GetModuleHandle(module); + if (!h) + return NULL; + + ptr = GetProcAddress(h, func); + if (mh) { + if (ptr) + *mh = h; + else + FreeLibrary(h); + } + return ptr; +} + static UINT get_system_directory(WCHAR *path, UINT len) { - HANDLE hKernel = GetModuleHandle("kernel32.dll"); - - if (hKernel) { - typedef UINT WINAPI wgetdir_func(WCHAR*, UINT); - FARPROC ptr = GetProcAddress(hKernel, "GetSystemWindowsDirectoryW"); - if (ptr) { - return (*(wgetdir_func *)ptr)(path, len); - } - } + typedef UINT WINAPI wgetdir_func(WCHAR*, UINT); + FARPROC ptr = + get_proc_address("kernel32", "GetSystemWindowsDirectoryW", NULL); + if (ptr) + return (*(wgetdir_func *)ptr)(path, len); return GetWindowsDirectoryW(path, len); } @@ -569,8 +588,7 @@ init_func(void) { if (!cancel_io) - cancel_io = (cancel_io_t)GetProcAddress(GetModuleHandle("kernel32"), - "CancelIo"); + cancel_io = (cancel_io_t)get_proc_address("kernel32", "CancelIo", NULL); } static void init_stdhandle(void); @@ -2663,6 +2681,19 @@ return r; } +static FARPROC +get_wsa_exetinsion_function(SOCKET s, GUID *guid) +{ + DWORD dmy; + FARPROC ptr = NULL; + + WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, guid, sizeof(*guid), + &ptr, sizeof(ptr), &dmy, NULL, NULL); + if (!ptr) + errno = ENOSYS; + return ptr; +} + #undef accept int WSAAPI @@ -3002,13 +3033,9 @@ if (!pWSARecvMsg) { static GUID guid = WSAID_WSARECVMSG; - DWORD dmy; - WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid), - &pWSARecvMsg, sizeof(pWSARecvMsg), &dmy, NULL, NULL); - if (!pWSARecvMsg) { - errno = ENOSYS; + pWSARecvMsg = (WSARecvMsg_t)get_wsa_exetinsion_function(s, &guid); + if (!pWSARecvMsg) return -1; - } } msghdr_to_wsamsg(msg, &wsamsg); @@ -3095,13 +3122,9 @@ if (!pWSASendMsg) { static GUID guid = WSAID_WSASENDMSG; - DWORD dmy; - WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid), - &pWSASendMsg, sizeof(pWSASendMsg), &dmy, NULL, NULL); - if (!pWSASendMsg) { - errno = ENOSYS; + pWSASendMsg = (WSASendMsg_t)get_wsa_exetinsion_function(s, &guid); + if (!pWSASendMsg) return -1; - } } msghdr_to_wsamsg(msg, &wsamsg); @@ -3847,16 +3870,9 @@ if (!pCreateHardLinkW && !myerrno) { HANDLE hKernel; - hKernel = GetModuleHandle("kernel32.dll"); - if (hKernel) { - pCreateHardLinkW = (BOOL (WINAPI *)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES))GetProcAddress(hKernel, "CreateHardLinkW"); - if (!pCreateHardLinkW) { - myerrno = ENOSYS; - } - } - else { - myerrno = map_errno(GetLastError()); - } + pCreateHardLinkW = (BOOL (WINAPI *)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES))get_proc_address("kernel32", "CreateHardLinkW", NULL); + if (!pCreateHardLinkW) + myerrno = ENOSYS; } if (!pCreateHardLinkW) { errno = myerrno; @@ -4701,12 +4717,8 @@ rb_pid_t ppid = 0; if (!IsWin95() && rb_w32_osver() >= 5) { - if (!pNtQueryInformationProcess) { - HANDLE hNtDll = GetModuleHandle("ntdll.dll"); - if (hNtDll) { - pNtQueryInformationProcess = (long (WINAPI *)(HANDLE, int, void *, ULONG, ULONG *))GetProcAddress(hNtDll, "NtQueryInformationProcess"); - } - } + if (!pNtQueryInformationProcess) + pNtQueryInformationProcess = (long (WINAPI *)(HANDLE, int, void *, ULONG, ULONG *))get_proc_address("ntdll.dll", "NtQueryInformationProcess", NULL); if (pNtQueryInformationProcess) { struct { long ExitStatus; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/