我写了一个(铁)python脚本,它在远程服务器上共享一个文件夹。
import getopt
import ctypes as C
from ctypes.wintypes import (LPCWSTR)
class SHARE_INFO_2(C.Structure):
_fields_ = [('shi2_netname', LPCWSTR),
('shi2_type', C.c_int),
('shi2_remark', LPCWSTR),
('shi2_permissions', C.c_int),
('shi2_max_uses', C.c_int),
('shi2_current_uses', C.c_int),
('shi2_path', LPCWSTR),
('shi2_passwd', LPCWSTR)]
def Share(server, shareName, dir):
i = C.c_int()
info = SHARE_INFO_2()
STYPE_DISKTREE = 0
info.shi2_netname = shareName
info.shi2_path = dir
info.shi2_type = STYPE_DISKTREE
info.shi2_remark = "Shared from script"
info.shi2_max_uses = -1
info.shi2_passwd = ""
return C.windll.Netapi32.NetShareAdd(server, 2, info, C.byref(i)) == 0
server = "rs01"
dir = "c:\\temp"
Share(server, "Temp", dir)
它是在Visual Studio 2015中创建的。当从Visual Studio中运行此脚本时,它运行正常。该文件夹是共享的。但是从命令行运行脚本:
ipy C:\ PythonConsole \ SysTasks.py
给出了异常:来自iron python代码中某处的System.AccessViolationException。 我觉得这与结构的结构声明/内存布局有关,但不确定。
任何人都知道这里有什么问题吗?
答案 0 :(得分:2)
找到了解决方案!
SHARE_INFO_2结构必须转换为具有缓冲区功能的字节数组,并且该数组必须传递给NetShareAdd。
def Share(server, shareName, dir):
info = SHARE_INFO_2()
STYPE_DISKTREE = 0
info.shi2_netname = shareName
info.shi2_path = dir
info.shi2_type = STYPE_DISKTREE
info.shi2_remark = "Shared from script"
info.shi2_max_uses = -1
info.shi2_passwd = ""
info.shi2_current_uses = 0
info.shi2_permissions = 0xFFFFFFFF
i = c_int()
bytearray = buffer(info)[:] # SERIALIZE INTO BYTES
return windll.Netapi32.NetShareAdd(server, 2, bytearray, C.byref(i)) == 0