ctypes WNetGetUniversalNameW

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Sep 2 23:24:22 EDT 2009


En Wed, 02 Sep 2009 14:24:10 -0300, Gustavo <gustavotabares at gmail.com>  
escribió:

> I'm trying to call WNetGetUniversalNameW via the ctypes module but I'm
> only causing the interpreter to crash. Unfortunately I don't have much
> experience with the ctypes module and I'm still trying to figure it
> out. I've searched for a solution to no avail. My confusion is
> centered around the LPVOID buffer parameter and how this should be
> created/used. Any help is appreciated.
>
> # DWORD WNetGetUniversalName(
> #  __in     LPCTSTR lpLocalPath,
> #  __in     DWORD dwInfoLevel,
> #  __out    LPVOID lpBuffer,
> #  __inout  LPDWORD lpBufferSize
> # );

import ctypes, win32netcon
 from ctypes import wintypes

LPDWORD = ctypes.POINTER(wintypes.DWORD)
class UniversalNameInfo(ctypes.Structure):
     # note the SINGLE underscores!
     _fields_ = [("lpUniversalName", wintypes.LPWSTR)]
PUniversalNameInfo = ctypes.POINTER(UniversalNameInfo)

WNetGetUniversalNameW = ctypes.windll.mpr.WNetGetUniversalNameW
WNetGetUniversalNameW.argtypes = [
   wintypes.LPCWSTR,
   wintypes.DWORD,
   wintypes.LPVOID,
   LPDWORD]

path = 'Y:'
level = win32netcon.UNIVERSAL_NAME_INFO_LEVEL
# you must suply your own buffer:
buffer = ctypes.create_unicode_buffer(1024)
buffersize = wintypes.DWORD(ctypes.sizeof(buffer))

ret = WNetGetUniversalNameW(path, level,
           ctypes.byref(buffer), ctypes.byref(buffersize))
print ret
# casting to pointer to UniversalNameInfo struct
puni = ctypes.cast(buffer, PUniversalNameInfo)
# dereference the pointer and access its (only) field
print puni.contents.lpUniversalName

-- 
Gabriel Genellina




More information about the Python-list mailing list