我正在编写的应用程序有一个奇怪的问题。我很确定我最近没有在下面的代码中改变任何东西,但不知何故它停止了工作。 重点。 我使用:从c#调用非托管代码。使用IntPtr获取数据
[DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern uint NetShareGetInfo(
string servername,
string netname,
int level,
out IntPtr ppBuf
);
要导入的netapi32.dll 。我需要物理路径。 WMI和WinRM不能使用。 然后,我有枚举和struct如下:根据VS代码分析器
public enum ShType
{
DiskTree = 0,
PrintQ = 1,
Device = 2,
IPC = 3,
Temporary = 0x40000000,
Special = unchecked((int)0x80000000)
}
public struct SHARE_INFO_502
{
public string Name;
public ShType Type;
public string Remark;
public int Permissions;
public int MaxUses;
public int CurrentUses;
public string Path;
public string PassWd;
public int reserved;
public IntPtr SecDescriptor;
}
所有在
[SuppressUnmanagedCodeSecurity]
public static class SafeNativeMethods
类。 我把它叫做:
IntPtr ptrGet;
var resGetInfo = SafeNativeMethods.NetShareGetInfo("server_name", "share_name", 502, out ptrGet);
SafeNativeMethods.SHARE_INFO_502 siGet = (SafeNativeMethods.SHARE_INFO_502)Marshal.PtrToStructure(ptrGet, typeof(SafeNativeMethods.SHARE_INFO_502));
上面的代码运行我没有收到任何错误或异常后,然而,siGet结构有每个属性值只有第一个字母。 哪里可能是问题?
请参阅SHARE_INFO_502的声明:http://www.pinvoke.net/default.aspx/Structures.SHARE_INFO_502 – PaulF