// @pymethod <o PyHANDLE>|win32wnet|WNetOpenEnum|Opens an Enumeration Handle for Enumerating Resources with <om win32wnet.WNetEnumResource> static PyObject * PyWNetOpenEnum(PyObject *self, PyObject *args) { // @comm See the Microsoft SDK for complete information on WNetOpenEnum. PyObject * ob_nr; NETRESOURCE * p_nr; DWORD dwScope, dwType, dwUsage; // not the same as the ones in NETRESOURCE DWORD Errno; HANDLE hEnum; // @pyparm int|scope||Specifies the scope of the enumeration. // @pyparm int|type||Specifies the resource types to enumerate. // @pyparm int|usage||Specifies the resource usage to be enumerated. // @pyparm <o PyNETRESOURCE>|resource||Python NETRESOURCE object. if (!PyArg_ParseTuple(args, "iiiO", &dwScope,&dwType,&dwUsage,&ob_nr)) return NULL; if (!PyWinObject_AsNETRESOURCE(ob_nr, &p_nr, TRUE)) return NULL; Py_BEGIN_ALLOW_THREADS Errno = WNetOpenEnum(dwScope, dwType, dwUsage, p_nr, &hEnum); Py_END_ALLOW_THREADS if (Errno != NO_ERROR) return(ReturnNetError("WNetOpenEnum", Errno)); return (PyNETENUMObject_FromHANDLE(hEnum)); // @rdesc PyHANDLE representing the Win32 HANDLE for the open resource. // This handle will be automatically be closed via <om win32wnet.WNetCloseEnum>, but // good style dictates it still be closed manually. };
// @pymethod |win32wnet|WNetCancelConnection2|Closes network connections made by WNetAddConnection2 or 3 static PyObject * PyWNetCancelConnection2 (PyObject *self, PyObject *args) { LPTSTR lpName; // @pyparm string|name||Name of existing connection to be closed DWORD dwFlags; // @pyparm int|flags||Currently determines if the persisent connection information will be updated as a result of this call. DWORD bForce; // @pyparm int|force||indicates if the close operation should be forced. (i.e. ignore open files and connections) DWORD ErrorNo; PyObject *obName; if(!PyArg_ParseTuple(args, "Okk", &obName, &dwFlags, &bForce)) return NULL; if (!PyWinObject_AsTCHAR(obName, &lpName, FALSE)) return NULL; Py_BEGIN_ALLOW_THREADS ErrorNo = WNetCancelConnection2(lpName, dwFlags, (BOOL)bForce); Py_END_ALLOW_THREADS PyWinObject_FreeTCHAR(lpName); if (ErrorNo != NO_ERROR) { return ReturnNetError("WNetCancelConnection2", ErrorNo); } Py_INCREF(Py_None); return Py_None; };
// @pymethod string|win32wnet|WNetGetConnection|Retrieves the name of the network resource associated with a local device. static PyObject * PyWNetGetConnection(PyObject *self, PyObject *args) { PyObject *ret = NULL; PyObject *obConnection = Py_None; DWORD length = 0; DWORD errcode; TCHAR *szConnection = NULL; TCHAR *buf = NULL; // @pyparm string|connection|None|A string that is a drive-based path for a network resource. // For example, if drive H has been mapped to a network drive share, and the network resource of interest is a file named Sample.doc in the directory \Win32\Examples on that share, the drive-based path is H:\Win32\Examples\Sample.doc. if (!PyArg_ParseTuple(args, "|O", &obConnection)) return NULL; if (!PyWinObject_AsTCHAR(obConnection, &szConnection, TRUE)) goto done; // get the buffer size { Py_BEGIN_ALLOW_THREADS errcode=WNetGetConnection(szConnection, NULL, &length); Py_END_ALLOW_THREADS } if (length==0) { ReturnNetError("WNetGetConnection", errcode); goto done; } buf = (TCHAR *)malloc( sizeof( TCHAR) * length); if (buf == NULL){ PyErr_Format(PyExc_MemoryError, "Unable to allocate %d bytes", sizeof(TCHAR)*length); goto done; } Py_BEGIN_ALLOW_THREADS errcode = WNetGetConnection(szConnection, buf, &length); Py_END_ALLOW_THREADS if (0 != errcode) { ReturnNetError("WNetGetConnection", errcode); goto done; } // length includes the NULL - drop it (safely!) ret = PyWinObject_FromTCHAR(buf, (length > 0) ? length-1 : 0); done: PyWinObject_FreeTCHAR(szConnection); if (buf) free(buf); return ret; }
// @pymethod string|win32wnet|WNetGetUser|Retrieves the current default user name, or the user name used to establish a network connection. static PyObject * PyWNetGetUser(PyObject *self, PyObject *args) { PyObject *ret = NULL; PyObject *obConnection = Py_None; DWORD length = 0; DWORD errcode; TCHAR *szConnection = NULL; TCHAR *buf = NULL; // @pyparm string|connection|None|A string that specifies either the name of a local device that has been redirected to a network resource, or the remote name of a network resource to which a connection has been made without redirecting a local device. // If this parameter is None, the system returns the name of the current user for the process. if (!PyArg_ParseTuple(args, "|O", &obConnection)) return NULL; if (!PyWinObject_AsTCHAR(obConnection, &szConnection, TRUE)) goto done; // get the buffer size { Py_BEGIN_ALLOW_THREADS errcode=WNetGetUser(szConnection, NULL, &length); Py_END_ALLOW_THREADS } if (length==0) { ReturnNetError("WNetGetUser", errcode); goto done; } buf = (TCHAR *)malloc( sizeof( TCHAR) * length); if (buf == NULL){ PyErr_Format(PyExc_MemoryError, "Unable to allocate %d bytes", sizeof(TCHAR)*length); goto done; } Py_BEGIN_ALLOW_THREADS errcode = WNetGetUser(szConnection, buf, &length); Py_END_ALLOW_THREADS if (0 != errcode) { ReturnNetError("WNetGetUser", errcode); goto done; } // length includes the NULL - drop it (safely!) ret = PyWinObject_FromTCHAR(buf, (length > 0) ? length-1 : 0); done: PyWinObject_FreeTCHAR(szConnection); if (buf) free(buf); return ret; }
// @pymethod ([dict, ...], total, resumeHandle)|win32net|NetUseEnum|Retrieves information about transport protocols that are currently managed by the redirector // @rdesc The result is a list of items read (with each item being a dictionary of format // <o PyUSE_INFO_*>, depending on the level parameter), // the total available, and a new "resume handle". The first time you call // this function, you should pass zero for the resume handle. If more data // is available than what was returned, a new non-zero resume handle will be // returned, which can be used to call the function again to fetch more data. // This process may repeat, each time with a new resume handle, until zero is // returned for the new handle, indicating all the data has been read. PyObject * PyNetUseEnum(PyObject *self, PyObject *args) { WCHAR *szServer = NULL, *szDomain = NULL; PyObject *obServer, *obDomain = Py_None; PyObject *ret = NULL; PyNET_STRUCT *pInfo; DWORD err; DWORD dwPrefLen = MAX_PREFERRED_LENGTH; DWORD level; BOOL ok = FALSE; DWORD resumeHandle = 0; DWORD numRead, i; PyObject *list; BYTE *buf = NULL; DWORD totalEntries = 0; // @pyparm string/<o PyUnicode>|server||The name of the server to execute on, or None. // @pyparm int|level||The level of data required. Currently levels 0, 1 and // 2 are supported. // @pyparm int|resumeHandle|0|A resume handle. See the return description for more information. // @pyparm int|prefLen|MAX_PREFERRED_LENGTH|The preferred length of the data buffer. if (!PyArg_ParseTuple(args, "Oi|ii", &obServer, &level, &resumeHandle, &dwPrefLen)) return NULL; if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE)) goto done; if (!PyWinObject_AsWCHAR(obDomain, &szDomain, TRUE)) goto done; if (!FindNET_STRUCT(level, use_infos, &pInfo)) goto done; err = NetUseEnum(szServer, level, &buf, dwPrefLen, &numRead, &totalEntries, &resumeHandle); if (err!=0 && err != ERROR_MORE_DATA) { ReturnNetError("NetUseEnum",err); goto done; } list = PyList_New(numRead); if (list==NULL) goto done; for (i=0;i<numRead;i++) { PyObject *sub = PyObject_FromNET_STRUCT(pInfo, buf+(i*pInfo->structsize)); if (sub==NULL) goto done; PyList_SetItem(list, i, sub); } resumeHandle = err==0 ? 0 : resumeHandle; ret = Py_BuildValue("Oll", list, totalEntries, resumeHandle); Py_DECREF(list); ok = TRUE; done: if (buf) NetApiBufferFree(buf); if (!ok) { Py_XDECREF(ret); ret = NULL; } PyWinObject_FreeWCHAR(szServer); return ret; // @pyseeapi NetUseEnum }
// @pymethod (int,str,str)|win32wnet|WNetGetLastError|Retrieves extended error information set by a network provider when one of the WNet* functions fails // @rdesc Returns the error code, a text description of the error, and the name of the network provider // @comm The error description or the network provider name may be truncated if they exceed 1024 and 256 characters, respectively PyObject *PyWNetGetLastError(PyObject *self, PyObject *args) { if (!PyArg_ParseTuple(args, ":WNetGetLastError")) return NULL; DWORD err, extendederr; TCHAR errstr[1024], provider[256]; Py_BEGIN_ALLOW_THREADS err=WNetGetLastError(&extendederr, errstr, sizeof(errstr)/sizeof(TCHAR), provider, sizeof(provider)/sizeof(TCHAR)); Py_END_ALLOW_THREADS if (err==NO_ERROR) return Py_BuildValue("kNN", extendederr, PyWinObject_FromTCHAR(errstr), PyWinObject_FromTCHAR(provider)); return ReturnNetError("WNetGetLastError", err); }
// @pymethod (<o PyNETRESOURCE>, str)|win32wnet|WNetGetResourceInformation|Finds the type and provider of a network resource // @rdesc Returns a NETRESOURCE and a string containing the trailing part of the remote path PyObject * PyWNetGetResourceInformation(PyObject *self, PyObject *args) { PyObject *NRT, *ret=NULL; //object placeholder for incoming NETRESOURCE object NETRESOURCE *nrin, *nrout=NULL; // buffer holds a NETRESOURCE struct and all its string members DWORD bufsize=sizeof(NETRESOURCE)+256; DWORD err; LPTSTR szFilePath = NULL; #ifdef Py_DEBUG bufsize=sizeof(NETRESOURCE); // make sure it loops thru again in debug mode #endif if (!PyArg_ParseTuple(args, "O!", &PyNETRESOURCEType, &NRT)) // @pyparm <o PyNETRESOURCE>|NetResource||Describes a network resource. lpRemoteName is required, dwType and lpProvider can be supplied if known return NULL; if (!PyWinObject_AsNETRESOURCE(NRT, &nrin, FALSE)) return NULL; while (1){ // function will not take NULL to return the buffer size, always pass in a valid buffer if (nrout) free(nrout); nrout=(NETRESOURCE *)malloc(bufsize); if (nrout==NULL) return PyErr_Format(PyExc_MemoryError,"Unable to allocate %d bytes", bufsize); Py_BEGIN_ALLOW_THREADS err = WNetGetResourceInformation(nrin, nrout, &bufsize, &szFilePath); Py_END_ALLOW_THREADS if (err == NO_ERROR){ ret=Py_BuildValue("NN", PyWinObject_FromNETRESOURCE(nrout), PyWinObject_FromTCHAR(szFilePath)); break; } else if (err!=ERROR_MORE_DATA){ ReturnNetError("WNetGetResourceInformation", err); break; } } if (nrout) free(nrout); return ret; }
// @pymethod |win32net|NetUserChangePassword|Changes the password for a user. PyObject *PyNetUserChangePassword(PyObject *self, PyObject *args) { // @comm A server or domain can be configured to require that a // user log on to change the password on a user account. // If that is the case, you need administrator or account operator access // to change the password for another user acount. // If logging on is not required, you can change the password for // any user account, so long as you know the current password. WCHAR *szServer = NULL; WCHAR *szName = NULL; WCHAR *szOld = NULL; WCHAR *szNew = NULL; PyObject *obName, *obServer, *obOld, *obNew; PyObject *ret = NULL; DWORD err = 0; // @pyparm string/<o PyUnicode>|server||The name of the server, or None. // @pyparm string/<o PyUnicode>|username||The user name, or None for the current username. // @pyparm string/<o PyUnicode>|oldPassword||The old password // @pyparm string/<o PyUnicode>|newPassword||The new password if (!PyArg_ParseTuple(args, "OOOO", &obServer, &obName, &obOld, &obNew)) return NULL; if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE)) goto done; if (!PyWinObject_AsWCHAR(obName, &szName, TRUE)) goto done; if (!PyWinObject_AsWCHAR(obOld, &szOld, FALSE)) goto done; if (!PyWinObject_AsWCHAR(obNew, &szNew, FALSE)) goto done; err = NetUserChangePassword(szServer, szName, szOld, szNew); if (err) { ReturnNetError("NetUserChangePassword",err); // @pyseeapi NetUserChangePassword goto done; } ret = Py_None; Py_INCREF(Py_None); done: PyWinObject_FreeWCHAR(szServer); PyWinObject_FreeWCHAR(szName); PyWinObject_FreeWCHAR(szOld); PyWinObject_FreeWCHAR(szNew); return ret; }
// @pymethod |win32wnet|WNetAddConnection3|Creates a connection to a network resource. // @comm Accepts keyword arguments. // @pyseeapi WNetAddConnection3 static PyObject *PyWNetAddConnection3 (PyObject *self, PyObject *args, PyObject *kwargs) { LPTSTR Username = NULL; LPTSTR Password = NULL; DWORD ErrorNo; // holds the returned error number, if any DWORD flags = 0; NETRESOURCE *pNetResource; PyObject *obPassword=Py_None, *obUsername=Py_None, *ret=NULL; PyObject *obhwnd, *obnr; static char *keywords[] = {"HwndOwner", "NetResource","Password","UserName","Flags", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|OOk", keywords, &obhwnd, // @pyparm int|hwnd||Handle to a parent window. &obnr, // @pyparm <o PyNETRESOURCE>|NetResource||Describes the network resource for the connection. &obPassword, // @pyparm str|Password|None|The password to use. Use None for default credentials. &obUsername, // @pyparm str|UserName|None|The user name to connect as. Use None for default credentials. &flags)) // @pyparm int|Flags|0|Combination win32netcon.CONNECT_* flags return NULL; HWND hwnd; if (!PyWinObject_AsHANDLE(obhwnd, (HANDLE *)&hwnd)) return NULL; if (!PyWinObject_AsNETRESOURCE(obnr, &pNetResource, FALSE)) return NULL; if (!PyWinObject_AsTCHAR(obPassword, &Password, TRUE) || !PyWinObject_AsTCHAR(obUsername, &Username, TRUE)) goto done; Py_BEGIN_ALLOW_THREADS ErrorNo = WNetAddConnection3(hwnd, pNetResource, Password, Username, flags); Py_END_ALLOW_THREADS if (ErrorNo != NO_ERROR) ReturnNetError("WNetAddConnection3", ErrorNo); else{ Py_INCREF(Py_None); ret = Py_None; } done: PyWinObject_FreeTCHAR(Password); PyWinObject_FreeTCHAR(Username); return ret; };
// @pymethod dict|win32net|NetUseGetInfo|Retrieves information about the configuration elements for a workstation PyObject * PyNetUseGetInfo(PyObject *self, PyObject *args) { WCHAR *szServer = NULL, *szUse = NULL; PyObject *obServer, *obUse; PyNET_STRUCT *pInfo; BYTE *buf = NULL; PyObject *ret = NULL; int level = 0; DWORD err; // @pyparm string/<o PyUnicode>|server||The name of the server to execute on, or None. // @pyparm string/<o PyUnicode>|usename||The name of the locally mapped resource. // @pyparm int|level|0|The information level contained in the data. NOTE: levels 302 and 402 don't seem to work correctly. They return error 124. So currently these info levels are not available. if (!PyArg_ParseTuple(args, "OO|i", &obServer, &obUse, &level)) return NULL; if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE)) goto done; if (!PyWinObject_AsWCHAR(obUse, &szUse, TRUE)) goto done; if (!FindNET_STRUCT(level, use_infos, &pInfo)) goto done; err = NetUseGetInfo(szServer, szUse, level, &buf); if (err) { ReturnNetError("NetUseGetInfo",err); goto done; } ret= PyObject_FromNET_STRUCT(pInfo, buf); done: if (buf) NetApiBufferFree(buf); PyWinObject_FreeWCHAR(szServer); PyWinObject_FreeWCHAR(szUse); return ret; // @pyseeapi NetUseGetInfo // @rdesc The result will be a dictionary in one of the <o PyUSE_INFO_*> // formats, depending on the level parameter. }
// @pymethod <o PyNETRESOURCE>|win32wnet|WNetGetResourceParent|Finds the parent resource of a network resource PyObject *PyWNetGetResourceParent(PyObject *self, PyObject *args) { NETRESOURCE *nr, *parentnr=NULL; DWORD err, bufsize=sizeof(NETRESOURCE)+256; #ifdef Py_DEBUG bufsize=sizeof(NETRESOURCE); #endif PyObject *obnr, *ret=NULL; if (!PyArg_ParseTuple(args, "O:WNetGetResourceParent", &obnr)) // @pyparm <o PyNETRESOURCE>|NetResource||Describes a network resource. lpRemoteName and lpProvider are required, dwType is recommended for efficiency return NULL; if (!PyWinObject_AsNETRESOURCE(obnr, &nr, FALSE)) return NULL; // buffer includes NETRESOURCE plus character strings it contains // Will not accept NULL to retrieve buffer size while (1){ if (parentnr) free(parentnr); parentnr=(NETRESOURCE *)malloc(bufsize); if (parentnr==NULL) return PyErr_Format(PyExc_MemoryError,"Unable to allocate %d bytes", bufsize); Py_BEGIN_ALLOW_THREADS err=WNetGetResourceParent(nr, parentnr, &bufsize); Py_END_ALLOW_THREADS if (err==NO_ERROR){ ret=PyWinObject_FromNETRESOURCE(parentnr); break; } else if (err!=ERROR_MORE_DATA){ ReturnNetError("WNetGetResourceParent", err); break; } } if (parentnr) free(parentnr); return ret; }
// @pymethod |win32net|NetUseAdd|Establishes connection between local or NULL device name and a shared resource through redirector PyObject * PyNetUseAdd(PyObject *self, PyObject *args) { WCHAR *szServer = NULL; PyObject *obServer, *obData; PyNET_STRUCT *pInfo; BYTE *buf = NULL; PyObject *ret = NULL; DWORD level; DWORD err = 0; // @pyparm string/<o PyUnicode>|server||The name of the server, or None. // @pyparm int|level||The information level contained in the data // @pyparm mapping|data||A dictionary holding the share data in the format of <o PyUSE_INFO_*>. if (!PyArg_ParseTuple(args, "OiO", &obServer, &level, &obData)) return NULL; if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE)) goto done; if (!FindNET_STRUCT(level, use_infos, &pInfo)) goto done; if (!PyObject_AsNET_STRUCT(obData, pInfo, &buf)) goto done; err = NetUseAdd(szServer, level, buf, NULL); if (err) { ReturnNetError("NetUseAdd",err); goto done; } ret= Py_None; Py_INCREF(ret); done: if (buf) PyObject_FreeNET_STRUCT(pInfo, buf); PyWinObject_FreeWCHAR(szServer); return ret; // @pyseeapi NetUseAdd }
// @pymethod |win32net|NetUseDel|Ends connection to a shared resource. PyObject * PyNetUseDel(PyObject *self, PyObject *args) { // @pyparm string/<o PyUnicode>|server||The name of the server, or None. // @pyparm string/<o PyUnicode>|useName||The share name // @pyparm int|forceCond|0|Level of force to use. Can be USE_FORCE or USE_NOFORCE or USE_LOTS_OF_FORCE WCHAR *szServer = NULL; WCHAR *szName = NULL; PyObject *obUseName, *obServer; PyObject *ret = NULL; int forceCond = 0; DWORD err = 0; if (!PyArg_ParseTuple(args, "OO|i", &obServer, &obUseName, &forceCond)) return NULL; if (!PyWinObject_AsWCHAR(obServer, &szServer, TRUE)) goto done; if (!PyWinObject_AsWCHAR(obUseName, &szName, FALSE)) goto done; err = NetUseDel(szServer, szName, (DWORD)forceCond); if (err) { ReturnNetError("NetUseDel",err); goto done; } ret = Py_None; Py_INCREF(Py_None); done: PyWinObject_FreeWCHAR(szServer); PyWinObject_FreeWCHAR(szName); return ret; // @pyseeapi NetUseDel }
// @pymethod |win32wnet|WNetAddConnection2|Creates a connection to a network resource. The function can redirect // a local device to the network resource. // @comm This function also accepts backwards-compatible, positional-only // arguments of (dwType, lpLocalName, lpRemoteName[, lpProviderName, Username, Password, flags]) // @comm Accepts keyword arguments. // @pyseeapi WNetAddConnection2 static PyObject *PyWNetAddConnection2 (PyObject *self, PyObject *args, PyObject *kwargs) { LPTSTR Username = NULL; LPTSTR Password = NULL; // values used for b/w compat args. DWORD Type; PyObject *obLocalName, *obRemoteName, *obProviderName = Py_None; PyObject *obnr, *obPassword=Py_None, *obUsername=Py_None, *ret=NULL; DWORD ErrorNo; // holds the returned error number, if any DWORD flags = 0; NETRESOURCE *pNetResource; NETRESOURCE tempNetResource; memset(&tempNetResource, 0, sizeof(tempNetResource)); if (PyArg_ParseTuple(args,"iOO|OOOi",&Type,&obLocalName,&obRemoteName,&obProviderName,&obUsername,&obPassword, &flags)) { // the b/w compat args have been used - build the NETRESOURCE structure memset((void *)&tempNetResource, '\0', sizeof(NETRESOURCE)); tempNetResource.dwType = Type; if (!PyWinObject_AsTCHAR(obLocalName, &tempNetResource.lpLocalName, TRUE) || !PyWinObject_AsTCHAR(obRemoteName, &tempNetResource.lpRemoteName, FALSE) || !PyWinObject_AsTCHAR(obProviderName, &tempNetResource.lpProvider, TRUE)) goto done; pNetResource = &tempNetResource; } else { PyErr_Clear(); static char *keywords[] = {"NetResource","Password","UserName","Flags", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOk", keywords, &obnr, // @pyparm <o PyNETRESOURCE>|NetResource||Describes the network resource for the connection. &obPassword, // @pyparm str|Password|None|The password to use. Use None for default credentials. &obUsername, // @pyparm str|UserName|None|The user name to connect as. Use None for default credentials. &flags)) // @pyparm int|Flags|0|Combination win32netcon.CONNECT_* flags return NULL; if (!PyWinObject_AsNETRESOURCE(obnr, &pNetResource, FALSE)) return NULL; } if (!PyWinObject_AsTCHAR(obPassword, &Password, TRUE) || !PyWinObject_AsTCHAR(obUsername, &Username, TRUE)) goto done; Py_BEGIN_ALLOW_THREADS #ifdef _WIN32_WCE_ // Windows CE only has the #3 version...use NULL for HWND to simulate #2 ErrorNo = WNetAddConnection3(NULL, pNetResource, Password, Username, flags); #else ErrorNo = WNetAddConnection2(pNetResource, Password, Username, flags); #endif Py_END_ALLOW_THREADS if (ErrorNo != NO_ERROR) ReturnNetError("WNetAddConnection2", ErrorNo); else{ Py_INCREF(Py_None); ret = Py_None; } done: PyWinObject_FreeTCHAR(Password); PyWinObject_FreeTCHAR(Username); PyWinObject_FreeTCHAR(tempNetResource.lpLocalName); PyWinObject_FreeTCHAR(tempNetResource.lpRemoteName); PyWinObject_FreeTCHAR(tempNetResource.lpProvider); return ret; };
// @pymethod string/tuple|win32wnet|WNetGetUniversalName|Takes a drive-based path for a network resource and returns an information structure that contains a more universal form of the name. static PyObject * PyWNetGetUniversalName(PyObject *self, PyObject *args) { int level = UNIVERSAL_NAME_INFO_LEVEL; TCHAR *szLocalPath = NULL; PyObject *obLocalPath; void *buf = NULL; DWORD length = 0; PyObject *ret = NULL; DWORD errcode; if (!PyArg_ParseTuple(args, "O|i:WNetGetUniversalName", &obLocalPath, &level)) return NULL; if (!PyWinObject_AsTCHAR(obLocalPath, &szLocalPath, FALSE)) return NULL; // @pyparm string|localPath||A string that is a drive-based path for a network resource. // <nl>For example, if drive H has been mapped to a network drive share, and the network // resource of interest is a file named SAMPLE.DOC in the directory \WIN32\EXAMPLES on // that share, the drive-based path is H:\WIN32\EXAMPLES\SAMPLE.DOC. // @pyparm int|infoLevel|UNIVERSAL_NAME_INFO_LEVEL|Specifies the type of structure that the function stores in the buffer pointed to by the lpBuffer parameter. // This parameter can be one of the following values. // @flagh Value|Meaning // @flag UNIVERSAL_NAME_INFO_LEVEL (=1)|The function returns a simple string with the UNC name. // @flag REMOTE_NAME_INFO_LEVEL (=2)|The function returns a tuple based in the Win32 REMOTE_NAME_INFO data structure. // @rdesc If the infoLevel parameter is REMOTE_NAME_INFO_LEVEL, the result is a tuple of 3 strings: (UNCName, connectionName, remainingPath) // First get the buffer size. { Py_BEGIN_ALLOW_THREADS char temp_buf[] = ""; // doesnt appear to like NULL!! errcode = WNetGetUniversalName( szLocalPath, level, &temp_buf, &length); Py_END_ALLOW_THREADS } if (errcode != ERROR_MORE_DATA || length == 0) { ReturnNetError("WNetGetUniversalName (for buffer size)", errcode); goto done; } buf = malloc(length); if (buf==NULL) goto done; errcode = WNetGetUniversalName( szLocalPath, level, buf, &length); if (errcode != 0) { ReturnNetError("WNetGetUniversalName", errcode); goto done; } switch (level) { case UNIVERSAL_NAME_INFO_LEVEL: ret = PyWinObject_FromTCHAR( ((UNIVERSAL_NAME_INFO *)buf)->lpUniversalName ); break; case REMOTE_NAME_INFO_LEVEL: { REMOTE_NAME_INFO *r = (REMOTE_NAME_INFO *)buf; ret = PyTuple_New(3); if (ret==NULL) goto done; PyTuple_SET_ITEM(ret, 0, PyWinObject_FromTCHAR( r->lpUniversalName) ); PyTuple_SET_ITEM(ret, 1, PyWinObject_FromTCHAR( r->lpConnectionName) ); PyTuple_SET_ITEM(ret, 2, PyWinObject_FromTCHAR( r->lpRemainingPath) ); break; } default: PyErr_SetString(PyExc_TypeError, "Unsupported infoLevel"); } done: PyWinObject_FreeTCHAR(szLocalPath); if (buf) free(buf); return ret; }
// @pymethod [(groupName, attribute), ...]|win32net|NetUserGetGroups|Returns a list of groups,attributes for all groups for the user. // @todo This needs to be extended to support the new model, while // not breaking existing code. A default arg would be perfect. PyObject * PyNetUserGetGroups( PyObject *self, PyObject *args) { DWORD dwBuffsize = MAX_PREFERRED_LENGTH; PyWin_AutoFreeBstr wzServerName; // storage for incoming servername string pointer PyWin_AutoFreeBstr wzUserName; // incoming username PyObject * obServerName; PyObject * obUserName; if (!PyArg_ParseTuple(args, "OO:NetUserGetGroups", &obServerName, // @pyparm string|serverName||The name of the remote server on which the function is to execute. None or an empty string specifies the server program running on the local computer. &obUserName)) // @pyparm string|userName||The name of the user to search for in each group account. return NULL; if (!PyWinObject_AsAutoFreeBstr(obServerName, &wzServerName, TRUE)) return NULL; if (!PyWinObject_AsAutoFreeBstr(obUserName, &wzUserName, FALSE)) return NULL; DWORD dwMaxCount, dwCount; // see the win32api call for how these are used. GROUP_USERS_INFO_1 *lpBuffer; NET_API_STATUS Errno; dwMaxCount = dwCount = 0; PyObject * pRetlist = PyList_New(0); //create a return list of 0 size if (pRetlist==NULL) return NULL; // did we err? Py_BEGIN_ALLOW_THREADS Errno = NetUserGetGroups((BSTR)wzServerName, (BSTR)wzUserName, 1, (LPBYTE *)&lpBuffer, dwBuffsize, &dwCount, &dwMaxCount); Py_END_ALLOW_THREADS if (Errno == NERR_Success) // if no error, then build the list { GROUP_USERS_INFO_1 *p_nr = lpBuffer; // Enum Resource returns a buffer of successive structs if (dwCount > 0) // we actually got something { do { PyObject *obName = PyWinObject_FromWCHAR(p_nr->grui1_name); PyObject *t_ob = Py_BuildValue("(Oi)",obName, p_nr->grui1_attributes); Py_XDECREF(obName); int listerr = PyList_Append(pRetlist,t_ob); // append our obj...Append does an INCREF! Py_DECREF(t_ob); if (listerr) // or bail { Py_DECREF(pRetlist); // free the Python List NetApiBufferFree((LPVOID)lpBuffer); return NULL; } p_nr++; // next object (its a ++ because it is a typed pointer!) dwCount--; } while (dwCount); }; // if (dwCount > 0) } else { // ERROR Occurred Py_DECREF(pRetlist); return ReturnNetError("NetUserGetGroups", Errno); } // @rdesc Always makes the level 1 call and returns all data. // Data return format is a Python List. Each "Item" // is a tuple of (groupname, attributes). "(s,i)" respectively. In NT 4 the attributes seem to be hardcoded to 7. // Earlier version of NT have not been tested. NetApiBufferFree((LPVOID)lpBuffer); return pRetlist; }
// @pymethod [groupName, ...]|win32net|NetUserGetLocalGroups|Retrieves a list of local groups to which a specified user belongs. // @todo This needs to be extended to support the new model, while // not breaking existing code. A default arg would be perfect. PyObject * PyNetUserGetLocalGroups( PyObject *self, PyObject *args) { DWORD dwFlags = LG_INCLUDE_INDIRECT; DWORD dwBuffsize = 0xFFFFFFFF; // request it all baby! PyWin_AutoFreeBstr wzServerName; // storage for incoming domain string pointer PyWin_AutoFreeBstr wzUserName; // incoming username PyObject *obServerName; PyObject *obUserName; if (!PyArg_ParseTuple(args, "OO|i:NetUserGetLocalGroups", &obServerName, // @pyparm string|serverName||The name of the remote server on which the function is to execute. None or an empty string specifies the server program running on the local computer. &obUserName, // @pyparm string|userName||The name of the user to search for in each group account. This parameter can be of the form \<UserName\>, in which case the username is expected to be found on servername. The user name can also be of the form \<DomainName\>\\\<UserName\> in which case \<DomainName\> is associated with servername and \<UserName\> is expected to be to be found on that domain. &dwFlags)) // @pyparm int|flags|LG_INCLUDE_INDIRECT|Flags for the call. return NULL; if (!PyWinObject_AsAutoFreeBstr(obServerName, &wzServerName, TRUE)) return NULL; if (!PyWinObject_AsAutoFreeBstr(obUserName, &wzUserName, FALSE)) return NULL; DWORD dwMaxCount, dwCount; // see the win32api call for how these are used. LOCALGROUP_USERS_INFO_0 *lpBuffer; NET_API_STATUS Errno; dwMaxCount = dwCount = 0; PyObject * pRetlist = PyList_New(0); //create a return list of 0 size if (pRetlist==NULL) return NULL; // did we err? Py_BEGIN_ALLOW_THREADS Errno = NetUserGetLocalGroups(wzServerName, wzUserName, 0, dwFlags, (LPBYTE *)&lpBuffer, dwBuffsize, &dwCount, &dwMaxCount); // do the enumeration Py_END_ALLOW_THREADS if (Errno == NERR_Success) // if no error, then build the list { LOCALGROUP_USERS_INFO_0 *p_nr = lpBuffer; // Enum Resource returns a buffer of successive structs if (dwCount > 0) // we actually got something { do { PyObject *t_ob = PyWinObject_FromWCHAR(p_nr->lgrui0_name); int listerr = PyList_Append(pRetlist,t_ob); // append our obj...Append does an INCREF! Py_DECREF(t_ob); if (listerr) // or bail { Py_DECREF(pRetlist); // free the Python List NetApiBufferFree((LPVOID)lpBuffer); return NULL; } p_nr++; // next object (its a ++ because it is a typed pointer!) dwCount--; } while (dwCount); }; // if (dwCount > 0) } else // ERROR Occurred { Py_DECREF(pRetlist); return ReturnNetError("NetUserGetLocalGroups", Errno); } NetApiBufferFree((LPVOID)lpBuffer); return pRetlist; }