calling NetShareEnum win32api with ctypes

T

taghi

I want to call NetShareEnum, a function from netapi32.dll
NetShareEnum has this definition:

NET_API_STATUS NetShareEnum(
__in LPWSTR servername,
__in DWORD level,
__out LPBYTE *bufptr,
__in DWORD prefmaxlen,
__out LPDWORD entriesread,
__out LPDWORD totalentries,
__inout LPDWORD resume_handle
);

I wrote this code in python 2.5:

from ctypes import *

cname=c_wchar_p('a-computer-name')
level=c_int(1)
bufptr=create_string_buffer('\00', 10000)
prefmaxlen=c_int(9999)
entriesread=c_long(0)
totalentries=c_long(0)
resume=c_long(0)

netapi32=cdll.LoadLibrary('netapi32.dll')
netapi32.NetShareEnum(cname, level, byref(bufptr), prefmaxlen,
byref(entriesread), byref(totalentries), byref(resume))

but I get this error:
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
s.NetShareEnum(name, level, byref(bufptr), prefmaxlen,
entriesread, totalentries, resume)
ValueError: Procedure called with not enough arguments (28 bytes
missing) or wrong calling convention

entriesread and totalentries has valid values after call but bufptr is
not.
I pass bufptr to function instead of byref(bufptr) but the error was
same.
 
C

castironpi

I want to call NetShareEnum, a function from netapi32.dll
NetShareEnum has this definition:

NET_API_STATUS NetShareEnum(
  __in     LPWSTR servername,
  __in     DWORD level,
  __out    LPBYTE *bufptr,
  __in     DWORD prefmaxlen,
  __out    LPDWORD entriesread,
  __out    LPDWORD totalentries,
  __inout  LPDWORD resume_handle
);

I wrote this code in python 2.5:

from ctypes import *

cname=c_wchar_p('a-computer-name')
level=c_int(1)
bufptr=create_string_buffer('\00', 10000)
prefmaxlen=c_int(9999)
entriesread=c_long(0)
totalentries=c_long(0)
resume=c_long(0)

netapi32=cdll.LoadLibrary('netapi32.dll')
netapi32.NetShareEnum(cname, level, byref(bufptr), prefmaxlen,
byref(entriesread), byref(totalentries), byref(resume))

but I get this error:
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    s.NetShareEnum(name, level, byref(bufptr), prefmaxlen,
entriesread, totalentries, resume)
ValueError: Procedure called with not enough arguments (28 bytes
missing) or wrong calling convention

entriesread and totalentries has valid values after call but bufptr is
not.
I pass bufptr to function instead of byref(bufptr) but the error was
same.

Taghi,

I just learned how to do this myself. I tested this revision on WinXP
Python 2.5.

from ctypes import *
netapi32= cdll.LoadLibrary( 'netapi32.dll' )

cname=c_wchar_p('[mycompname]')
level=c_int(1)
#bufptr=create_string_buffer('\00', 10000)
bufptr=c_void_p(0)
prefmaxlen=c_int(9999)
entriesread=c_long(0)
totalentries=c_long(0)
resume=c_long(0)

prototype= WINFUNCTYPE( c_int,
c_wchar_p,
c_int,
POINTER( c_void_p ),
c_int,
POINTER( c_long ),
POINTER( c_long ),
POINTER( c_long )
)
NetShareEnum= prototype( ( "NetShareEnum", netapi32 ) )

result= NetShareEnum(cname,
level,
pointer(bufptr),
prefmaxlen,
byref(entriesread),
byref(totalentries),
byref(resume)
)

print result
print cname, level, bufptr, prefmaxlen, entriesread, totalentries,
resume


The 'bufptr' parameter receives a pointer to a buffer on return, which
you then have to free using NetApiBufferFree. Here is my output:

0
c_wchar_p(u'[mycompname]') c_long(1) c_void_p(2382504) c_long(9999)
c_long(2) c_l
ong(2) c_long(0)

'bufptr' now points to an array of SHARE_INFO_1 structures, which you
will have to define too. It copied 2/2 entries, or 'entriesread' /
'totalentries'. Return 0 indicates success.
 
T

Tim Golden

taghi said:
I wrote this code in python 2.5:

from ctypes import *


.... snip ...

netapi32=cdll.LoadLibrary('netapi32.dll')


This is your problem: netapi32 is a windows
DLL and needs to be attached as such:

netapi32 = windll.LoadLibrary ("netapi32.dll")

or, more simply:

netapi32 = windll.net32api

But see my other post re this function in pywin32

TJG
 
T

Tim Golden

taghi said:
I wrote this code in python 2.5:

from ctypes import *


.... snip ...

netapi32=cdll.LoadLibrary('netapi32.dll')


This is your problem: netapi32 is a windows
DLL and needs to be attached as such:

netapi32 = windll.LoadLibrary ("netapi32.dll")

or, more simply:

netapi32 = windll.netapi32

But see my other post re this function in pywin32

TJG
 
G

Gabriel Genellina

I want to call NetShareEnum, a function from netapi32.dll
NetShareEnum has this definition:
[...]
netapi32=cdll.LoadLibrary('netapi32.dll')
netapi32.NetShareEnum(cname, level, byref(bufptr), prefmaxlen,
byref(entriesread), byref(totalentries), byref(resume))

but I get this error:
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
s.NetShareEnum(name, level, byref(bufptr), prefmaxlen,
entriesread, totalentries, resume)
ValueError: Procedure called with not enough arguments (28 bytes
missing) or wrong calling convention

Try with netapi32=windll.netapi32 instead
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top