我正在編寫的應用程序有一個奇怪的問題。我很確定我最近沒有在下面的代碼中改變任何東西,但不知何故它停止了工作。 重點。 我使用:從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