使用 ironpython 共享文件夹

share a folder using ironpython

我写了一个(铁)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

给出异常:System.AccessViolationException 来自铁 python 代码中的某处。 我感觉这与结构的结构 declaration/memory 布局有关,但不确定。

有人知道这里出了什么问题吗?

找到解决方案!

必须将 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