1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
[ StructLayout( LayoutKind.Sequential )]
public struct SHARE_INFO_502
{
[MarshalAs(UnmanagedType.LPWStr)]
public string shi502_netname;
public uint shi502_type;
[MarshalAs(UnmanagedType.LPWStr)]
public string shi502_remark;
public Int32 shi502_permissions;
public Int32 shi502_max_uses;
public Int32 shi502_current_uses;
[MarshalAs(UnmanagedType.LPWStr)]
public string shi502_path;
[MarshalAs(UnmanagedType.LPWStr)]
public string shi502_passwd;
public Int32 shi502_reserved;
public IntPtr shi502_security_descriptor;
}
[DllImport("Netapi32", CharSet = CharSet.Auto)]
static extern int NetApiBufferFree(IntPtr Buffer);
[DllImport("Netapi32", CharSet = CharSet.Auto)]
static extern int NetShareGetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername,
[MarshalAs(UnmanagedType.LPWStr)] string netname, int level, ref IntPtr bufptr);
static string NetShareGetPerm(string serverName, string netName)
{
string path = null;
IntPtr ptr = IntPtr.Zero;
int errCode = NetShareGetInfo(serverName, netName,0, ref ptr);
if (errCode == NERR_Success)
{
SHARE_INFO_502 shareInfo = (SHARE_INFO_502)
Marshal.PtrToStructure(ptr, typeof(SHARE_INFO_502));
path = shareInfo.shi502_permissions.ToString();
NetApiBufferFree(ptr);
}
else
Console.WriteLine(FormatMessage(errCode));
return path;
}
[STAThread]
static int Main(string[] args)
{
Console.WriteLine("Permissions=" + NetShareGetPerm("\\\\127.0.0.1", "partage$")+ "\r\n");
Console.ReadLine();
return 0;
}
const int ERROR_ACCESS_DENIED = 5;
const int ERROR_INVALID_LEVEL = 124; // unimplemented level for info
const int ERROR_INVALID_PARAMETER = 87;
const int ERROR_MORE_DATA = 234;
const int ERROR_NOT_ENOUGH_MEMORY = 8;
const int NERR_BufTooSmall = 2123; // The API return buffer is too small.
const int NERR_NetNameNotFound = 2310; // This shared resource does not exist.
const int NERR_Success = 0; |
Partager