1 |
douglas |
527 |
// Douglas Thrift |
2 |
|
|
// |
3 |
|
|
// Share |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
using System; |
8 |
|
|
using System.Runtime.InteropServices; |
9 |
|
|
|
10 |
|
|
public class Share |
11 |
|
|
{ |
12 |
|
|
public string path { get { return "//" + Environment.MachineName + "/" + share_; } } |
13 |
|
|
|
14 |
douglas |
528 |
public bool shared |
15 |
|
|
{ |
16 |
|
|
get |
17 |
|
|
{ |
18 |
|
|
IntPtr info = new IntPtr(); |
19 |
|
|
uint code; |
20 |
|
|
|
21 |
|
|
switch (code = NetShareGetInfo(null, share_, 2, ref info)) |
22 |
|
|
{ |
23 |
|
|
case (uint)NERR.NetNameNotFound: |
24 |
|
|
return false; |
25 |
|
|
case (uint)ERROR.MORE_DATA: |
26 |
|
|
case (uint)NERR.Success: |
27 |
|
|
NetApiBufferFree(info); |
28 |
|
|
|
29 |
|
|
return true; |
30 |
|
|
default: |
31 |
|
|
throw new Exception(String.Format("NetShareGetInfo() {0} {1}", (ERROR)code, (NERR)code)); |
32 |
|
|
} |
33 |
|
|
} |
34 |
|
|
} |
35 |
|
|
|
36 |
douglas |
527 |
public Share(string folder) |
37 |
|
|
{ |
38 |
|
|
folder_ = folder; |
39 |
|
|
share_ = String.Format("DecentralizedMedia0x{0:X}$", folder.GetHashCode()); |
40 |
|
|
} |
41 |
|
|
|
42 |
douglas |
528 |
/*~Share() |
43 |
|
|
{ |
44 |
|
|
Remove(); |
45 |
|
|
}*/ |
46 |
|
|
|
47 |
douglas |
527 |
public void Add() |
48 |
|
|
{ |
49 |
douglas |
528 |
if (!shared) |
50 |
douglas |
527 |
{ |
51 |
|
|
SHARE_INFO_2 info = new SHARE_INFO_2(); |
52 |
|
|
|
53 |
|
|
info.shi2_netname = share_; |
54 |
douglas |
528 |
info.shi2_type = STYPE.TEMPORARY; |
55 |
douglas |
527 |
info.shi2_remark = "Decentralized Media Share"; |
56 |
|
|
info.shi2_permissions = ACCESS.READ | ACCESS.EXEC; |
57 |
|
|
info.shi2_max_uses = -1; |
58 |
|
|
info.shi2_path = folder_; |
59 |
|
|
info.shi2_passwd = null; |
60 |
|
|
|
61 |
douglas |
528 |
NetShareAdd(null, 2, info, null); |
62 |
douglas |
527 |
} |
63 |
|
|
} |
64 |
|
|
|
65 |
douglas |
528 |
public void Remove() |
66 |
douglas |
527 |
{ |
67 |
douglas |
528 |
if (shared) |
68 |
|
|
NetShareDel(null, share_, 0); |
69 |
douglas |
527 |
} |
70 |
|
|
|
71 |
douglas |
528 |
enum ERROR : uint { ACCESS_DENIED = 5, NOT_ENOUGH_MEMORY = 8, INVALID_PARAMETER = 87, INVALID_NAME = 123, INVALID_LEVEL = 124, MORE_DATA = 234 } |
72 |
|
|
enum NERR : uint { Success = 0, BASE = 2100, UnknownDevDir = BASE + 16, RedirectedPath = BASE + 17, DuplicateShare = BASE + 18, BufTooSmall = BASE + 23, NetNameNotFound = BASE + 210, DeviceNotShared = BASE + 211 } |
73 |
|
|
enum STYPE : uint { DISKTREE = 0, PRINTQ = 1, DEVICE = 2, IPC = 3, TEMPORARY = 0x40000000, SPECIAL = 0x80000000 } |
74 |
douglas |
527 |
[Flags] |
75 |
douglas |
528 |
enum ACCESS : uint { READ = 0x1, WRITE = 0x2, CREATE = 0x4, EXEC = 0x8, DELETE = 0x10, ATRIB = 0x20, PERM = 0x40, ALL = READ | WRITE | CREATE | EXEC | DELETE | ATRIB | PERM } |
76 |
douglas |
527 |
|
77 |
|
|
[StructLayout(LayoutKind.Sequential)] |
78 |
douglas |
528 |
class SHARE_INFO_2 |
79 |
douglas |
527 |
{ |
80 |
|
|
[MarshalAs(UnmanagedType.LPWStr)] |
81 |
|
|
public string shi2_netname; |
82 |
|
|
public STYPE shi2_type; |
83 |
|
|
[MarshalAs(UnmanagedType.LPWStr)] |
84 |
|
|
public string shi2_remark; |
85 |
|
|
public ACCESS shi2_permissions; |
86 |
douglas |
528 |
public int shi2_max_uses; |
87 |
|
|
public uint shi2_current_uses; |
88 |
douglas |
527 |
[MarshalAs(UnmanagedType.LPWStr)] |
89 |
|
|
public string shi2_path; |
90 |
|
|
[MarshalAs(UnmanagedType.LPWStr)] |
91 |
|
|
public string shi2_passwd; |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
private string folder_, share_; |
95 |
|
|
|
96 |
douglas |
528 |
[DllImport("netapi32.dll", EntryPoint = "NetShareAdd")] |
97 |
|
|
private static extern uint NetShareAdd([In, MarshalAs(UnmanagedType.LPWStr)] string servername, uint level, [In] SHARE_INFO_2 buf, ref uint parm_err); |
98 |
douglas |
527 |
|
99 |
douglas |
528 |
[DllImport("netapi32.dll", EntryPoint = "NetShareAdd")] |
100 |
|
|
private static extern uint NetShareAdd([In, MarshalAs(UnmanagedType.LPWStr)] string servername, uint level, [In] SHARE_INFO_2 buf, [Out] object parm_err); |
101 |
douglas |
527 |
|
102 |
douglas |
528 |
[DllImport("netapi32.dll", EntryPoint = "NetShareCheck")] |
103 |
|
|
private static extern uint NetShareCheck([In, MarshalAs(UnmanagedType.LPWStr)] string servername, [In, MarshalAs(UnmanagedType.LPWStr)] string device, ref STYPE type); |
104 |
|
|
|
105 |
|
|
[DllImport("netapi32.dll", EntryPoint = "NetShareDel")] |
106 |
|
|
private static extern uint NetShareDel([In, MarshalAs(UnmanagedType.LPWStr)] string servername, [In, MarshalAs(UnmanagedType.LPWStr)] string netname, uint reserved); |
107 |
|
|
|
108 |
|
|
[DllImport("netapi32.dll"/*@"C:\DouglasThrift.net\TestDll\Debug\TestDll.dll"*/, EntryPoint = "NetShareGetInfo")] |
109 |
|
|
private static extern uint NetShareGetInfo([In, MarshalAs(UnmanagedType.LPWStr)] string servername, [In, MarshalAs(UnmanagedType.LPWStr)] string netname, uint level, ref IntPtr bufptr); |
110 |
|
|
|
111 |
|
|
[DllImport("netapi32.dll"/*@"C:\DouglasThrift.net\TestDll\Debug\TestDll.dll"*/, EntryPoint = "NetApiBufferFree")] |
112 |
|
|
private static extern uint NetApiBufferFree([In] IntPtr buffer); |
113 |
douglas |
527 |
} |