From: Gurusamy Sarathy Date: Mon, 1 Nov 1999 17:08:28 +0000 (+0000) Subject: enable better Win32::DomainName() by demand loading netapi32.dll X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=625a29bd030689ee3f060be950e2f2ffc93c94c9;p=p5sagit%2Fp5-mst-13.2.git enable better Win32::DomainName() by demand loading netapi32.dll (from Jan Dubois) p4raw-id: //depot/perl@4504 --- diff --git a/pod/Win32.pod b/pod/Win32.pod index dfc78bd..08043e8 100644 --- a/pod/Win32.pod +++ b/pod/Win32.pod @@ -32,12 +32,13 @@ only available in the ActivePerl binary distribution. =item Win32::CopyFile(FROM, TO, OVERWRITE) -The Win32::CopyFile() function copies an existing file to a new file. All -file information like creation time and file attributes will be copied to -the new file. However it will B copy the security information. If the -destination file already exists it will only be overwritten when the -OVERWRITE parameter is true. But even this will not overwrite a read-only -file; you have to unlink() it first yourself. +[CORE] The Win32::CopyFile() function copies an existing file to a new +file. All file information like creation time and file attributes will +be copied to the new file. However it will B copy the security +information. If the destination file already exists it will only be +overwritten when the OVERWRITE parameter is true. But even this will +not overwrite a read-only file; you have to unlink() it first +yourself. =item Win32::DomainName() @@ -207,7 +208,7 @@ the SID type. =item Win32::LookupAccountSID(SYSTEM, SID, ACCOUNT, DOMAIN, SIDTYPE) -[EXT] ]Looks up SID on SYSTEM and returns the account name, domain name, +[EXT] Looks up SID on SYSTEM and returns the account name, domain name, and the SID type. =item Win32::MsgBox(MESSAGE [, FLAGS [, TITLE]]) diff --git a/win32/win32.c b/win32/win32.c index 52692d7..3f56f60 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -16,18 +16,6 @@ #endif #include -#ifndef __MINGW32__ -#include -#include -/* ugliness to work around a buggy struct definition in lmwksta.h */ -#undef LPTSTR -#define LPTSTR LPWSTR -#include -#undef LPTSTR -#define LPTSTR LPSTR -#include -#endif /* __MINGW32__ */ - /* #include "config.h" */ #define PERLIO_NOT_STDIO 0 @@ -3014,43 +3002,63 @@ static XS(w32_DomainName) { dXSARGS; -#ifndef HAS_NETWKSTAGETINFO - /* mingw32 (and Win95) don't have NetWksta*(), so do it the old way */ - char name[256]; - DWORD size = sizeof(name); + HINSTANCE hNetApi32 = LoadLibrary("netapi32.dll"); + DWORD (__stdcall *pfnNetApiBufferFree)(LPVOID Buffer); + DWORD (__stdcall *pfnNetWkstaGetInfo)(LPWSTR servername, DWORD level, + void *bufptr); + + if (hNetApi32) { + pfnNetApiBufferFree = (DWORD (__stdcall *)(void *)) + GetProcAddress(hNetApi32, "NetApiBufferFree"); + pfnNetWkstaGetInfo = (DWORD (__stdcall *)(LPWSTR, DWORD, void *)) + GetProcAddress(hNetApi32, "NetWkstaGetInfo"); + } EXTEND(SP,1); - if (GetUserName(name,&size)) { - char sid[1024]; - DWORD sidlen = sizeof(sid); + if (hNetApi32 && pfnNetWkstaGetInfo && pfnNetApiBufferFree) { + /* this way is more reliable, in case user has a local account. */ char dname[256]; DWORD dnamelen = sizeof(dname); - SID_NAME_USE snu; - if (LookupAccountName(NULL, name, (PSID)&sid, &sidlen, - dname, &dnamelen, &snu)) { - XSRETURN_PV(dname); /* all that for this */ + struct { + DWORD wki100_platform_id; + LPWSTR wki100_computername; + LPWSTR wki100_langroup; + DWORD wki100_ver_major; + DWORD wki100_ver_minor; + } *pwi; + /* NERR_Success *is* 0*/ + if (0 == pfnNetWkstaGetInfo(NULL, 100, &pwi)) { + if (pwi->wki100_langroup && *(pwi->wki100_langroup)) { + WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_langroup, + -1, (LPSTR)dname, dnamelen, NULL, NULL); + } + else { + WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_computername, + -1, (LPSTR)dname, dnamelen, NULL, NULL); + } + pfnNetApiBufferFree(pwi); + FreeLibrary(hNetApi32); + XSRETURN_PV(dname); } + FreeLibrary(hNetApi32); } -#else - /* this way is more reliable, in case user has a local account. - * XXX need dynamic binding of netapi32.dll symbols or this will fail on - * Win95. Probably makes more sense to move it into libwin32. */ - char dname[256]; - DWORD dnamelen = sizeof(dname); - PWKSTA_INFO_100 pwi; - EXTEND(SP,1); - if (NERR_Success == NetWkstaGetInfo(NULL, 100, (LPBYTE*)&pwi)) { - if (pwi->wki100_langroup && *(pwi->wki100_langroup)) { - WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_langroup, - -1, (LPSTR)dname, dnamelen, NULL, NULL); - } - else { - WideCharToMultiByte(CP_ACP, NULL, pwi->wki100_computername, - -1, (LPSTR)dname, dnamelen, NULL, NULL); + else { + /* Win95 doesn't have NetWksta*(), so do it the old way */ + char name[256]; + DWORD size = sizeof(name); + if (hNetApi32) + FreeLibrary(hNetApi32); + if (GetUserName(name,&size)) { + char sid[1024]; + DWORD sidlen = sizeof(sid); + char dname[256]; + DWORD dnamelen = sizeof(dname); + SID_NAME_USE snu; + if (LookupAccountName(NULL, name, (PSID)&sid, &sidlen, + dname, &dnamelen, &snu)) { + XSRETURN_PV(dname); /* all that for this */ + } } - NetApiBufferFree(pwi); - XSRETURN_PV(dname); } -#endif XSRETURN_UNDEF; }