ctypes WNetGetUniversalNameW

G

Gustavo

Hello,

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

LPDWORD = ctypes.POINTER(DWORD)

class UniversalNameInfo(ctypes.Structure):
__fields__ = [("lpUniversalName", LPCWSTR)]

WNetGetUniversalNameW = ctypes.windll.mpr.WNetGetUniversalNameW

path = LPCWSTR('Y:')
level = DWORD(win32netcon.UNIVERSAL_NAME_INFO_LEVEL)
buffer_len = LPDWORD(DWORD(1024))
uni_struct = UniversalNameInfo()

ret = WNetGetUniversalNameW(path, level, uni_struct, buffer_len)
 
G

Gabriel Genellina

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top