473,395 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

calling NetShareEnum win32api with ctypes

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.
Aug 28 '08 #1
4 3114
On Aug 28, 12:01*am, taghi <eghli...@gmail.comwrote:
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.
Aug 28 '08 #2
taghi wrote:
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
Aug 28 '08 #3
taghi wrote:
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
Aug 28 '08 #4
En Thu, 28 Aug 2008 02:01:23 -0300, taghi <eg******@gmail.comescribi�:
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

--
Gabriel Genellina

Aug 28 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Georgy Pruss | last post by:
Hi all, How can I call a function which is in a Windows DLL? For example, import win32api lib_hnd = win32api.LoadLibrary( "user32.dll" ) if lib_hnd: fn_addr = win32api.GetProcAddress(...
5
by: Frank Borell | last post by:
I'm having a problem retrieving the ShortName from a file that was written by a MAC and only from a Windows 2003 server. I realize the file has a space after it, but I can retrieve the short...
0
by: siddharth_jain_1 | last post by:
Hello I am using NetShareEnum to enumerate all the shared folders of a server. If there is password set on the server, I get system error code 5 (access denied) as the return value of...
1
by: Siddharth Jain | last post by:
hello I am trying to enumerate the shared folders on a server using the NetShareEnum function. Now, when the server has a password set to access the shared folders, the function returns system...
0
by: Siddharth Jain | last post by:
hello I am trying to enumerate the shared folders on a server using the NetShareEnum function. Now, when the server has a password set to access the shared folders, the function returns system...
1
by: Siddharth Jain | last post by:
hello I am trying to enumerate the shared folders on a server using the NetShareEnum function. Now, when the server has a password set to access the shared folders, the function returns system...
1
by: Gerald Klix | last post by:
I read the whol email thread carefully and could not find any sentence by Guido, which states that he does not accept ctypes for the standard library. He just declined to rewrite winreg. Did I miss...
1
by: moreati | last post by:
Recently I discovered the re module doesn't support POSIX character classes: Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42) on linux2 Type "help", "copyright", "credits" or "license" for...
1
by: taghi | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.