python - 使用ironpython共享文件夹

标签 python visual-studio 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

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

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

最佳答案

找到解决方案!

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

关于python - 使用ironpython共享文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34223335/

相关文章:

python - master/worker 是否可扩展?

visual-studio - 在 T4 模板中引用 64 位 DLL

c++ - 如何使用visual studio c++绘制图表

c# - 扩展 C# .NET 应用程序 - 是否构建自定义脚本语言?

c# - 将参数从 C# 传递到 Python

python - 使用适用于 Python 2.7 的 MacPorts 安装 Shogun 工具箱

python - 每次运行时协方差都会发生变化

.net - 步骤失败后,是否可以在 Visual Studio Team Services(以前是 VS Online)中重试 VS Test 构建步骤?

ironpython - 字符串为空或空

python - 如何从 dataFrame 单元格中获取值以在另一个位置填充值?