如何将文件夹共享(100分)

  • 主题发起人 主题发起人 Cruiser
  • 开始时间 开始时间
C

Cruiser

Unregistered / Unconfirmed
GUEST, unregistred user!
在DELPHI中好象无法用NetShareAdd,而且该函数的帮助也不太对。<br>最好给个例子,用VC实现也可。
 
两位大侠的意思是?
 
看你是个新来的,不想让你对论坛失望,<br>如果你不是急着要的话,我有空时到是可以给你写一个。<br>否则你去找控件,肯定有这样的控件,我看见过。
 
我从别处抄一个例子给你:<br>/*++ <br>&nbsp;<br>Copyright (c) 1995, 1996 &nbsp;Microsoft Corporation <br>&nbsp;<br>Module Name: <br>&nbsp;<br>&nbsp; &nbsp; netshare.c <br>&nbsp;<br>Abstract: <br>&nbsp;<br>&nbsp; &nbsp; This module illustrates how to use the Windows NT Lan Manager API <br>&nbsp; &nbsp; in conjunction with the Win32 security API to create a new share <br>&nbsp; &nbsp; on an arbitrary machine with permissions that grant an arbitrary <br>&nbsp; &nbsp; user/group Full Access to the share. <br>&nbsp;<br>Author: <br>&nbsp;<br>&nbsp; &nbsp; Scott Field (sfield) &nbsp; &nbsp;01-Oct-95 <br>&nbsp;<br>--*/ <br>&nbsp;<br>#include &lt;windows.h&gt; <br>#include &lt;lm.h&gt; <br>#include &lt;stdio.h&gt; <br>&nbsp;<br>#define RTN_OK 0 <br>#define RTN_USAGE 1 <br>#define RTN_ERROR 13 <br>&nbsp;<br>// <br>// Note: UNICODE entry point and argv. &nbsp;This way, we don't need to bother <br>// with converting commandline args to Unicode <br>// <br>&nbsp;<br>int <br>__cdecl <br>wmain( <br>&nbsp; &nbsp; int argc, <br>&nbsp; &nbsp; wchar_t *argv[] <br>&nbsp; &nbsp; ) <br>{ <br>&nbsp; &nbsp; LPWSTR DirectoryToShare; <br>&nbsp; &nbsp; LPWSTR Sharename; <br>&nbsp; &nbsp; LPWSTR Username; <br>&nbsp; &nbsp; LPWSTR Server; <br>&nbsp;<br>&nbsp; &nbsp; PSID pSid = NULL; <br>&nbsp; &nbsp; DWORD cbSid; <br>&nbsp;<br>&nbsp; &nbsp; WCHAR RefDomain[DNLEN + 1]; <br>&nbsp; &nbsp; DWORD cchDomain = DNLEN + 1; <br>&nbsp; &nbsp; SID_NAME_USE peUse; <br>&nbsp;<br>&nbsp; &nbsp; SECURITY_DESCRIPTOR sd; <br>&nbsp; &nbsp; PACL pDacl = NULL; <br>&nbsp; &nbsp; DWORD dwAclSize; <br>&nbsp;<br>&nbsp; &nbsp; SHARE_INFO_502 si502; <br>&nbsp; &nbsp; NET_API_STATUS nas; <br>&nbsp;<br>&nbsp; &nbsp; BOOL bSuccess = FALSE; // assume this function fails <br>&nbsp;<br>&nbsp; &nbsp; if(argc &lt; 4) { <br>&nbsp; &nbsp; &nbsp; &nbsp; printf("Usage: %ls &lt;directory&gt; &lt;sharename&gt; &lt;user/group&gt; [////Server]/n", argv[0]); <br>&nbsp; &nbsp; &nbsp; &nbsp; printf(" directory is fullpath of directory to share/n"); <br>&nbsp; &nbsp; &nbsp; &nbsp; printf(" sharename is name of share on server/n"); <br>&nbsp; &nbsp; &nbsp; &nbsp; printf(" user/group is an WinNT user/groupname (REDMOND//sfield, Administrators, etc)/n"); <br>&nbsp; &nbsp; &nbsp; &nbsp; printf(" optional Server is the name of the computer to create the share on/n"); <br>&nbsp; &nbsp; &nbsp; &nbsp; printf("/nExample: %ls c://public public Everyone/n", argv[0]); <br>&nbsp; &nbsp; &nbsp; &nbsp; printf("c://public shared as public granting Everyone full access/n"); <br>&nbsp; &nbsp; &nbsp; &nbsp; printf("/nExample: %ls c://private cool$ REDMOND//sfield ////WINBASE/n", argv[0]); <br>&nbsp; &nbsp; &nbsp; &nbsp; printf("c://private on ////WINBASE shared as cool$ (hidden) granting REDMOND//sfield access/n"); <br>&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; return RTN_USAGE; <br>&nbsp; &nbsp; } <br>&nbsp;<br>&nbsp; &nbsp; // <br>&nbsp; &nbsp; // since the commandline was Unicode, just provide pointers to <br>&nbsp; &nbsp; // the relevant items <br>&nbsp; &nbsp; // <br>&nbsp;<br>&nbsp; &nbsp; DirectoryToShare = argv[1]; <br>&nbsp; &nbsp; Sharename = argv[2]; <br>&nbsp; &nbsp; Username = argv[3]; <br>&nbsp;<br>&nbsp; &nbsp; if( argc &gt; 4 ) { <br>&nbsp; &nbsp; &nbsp; &nbsp; Server = argv[4]; <br>&nbsp; &nbsp; } else { <br>&nbsp; &nbsp; &nbsp; &nbsp; Server = NULL; // local machine <br>&nbsp; &nbsp; } <br>&nbsp;<br>&nbsp; &nbsp; // <br>&nbsp; &nbsp; // initial allocation attempt for Sid <br>&nbsp; &nbsp; // <br>#define SID_SIZE 96 <br>&nbsp; &nbsp; cbSid = SID_SIZE; <br>&nbsp;<br>&nbsp; &nbsp; pSid = (PSID)HeapAlloc(GetProcessHeap(), 0, cbSid); <br>&nbsp; &nbsp; if(pSid == NULL) { <br>&nbsp; &nbsp; &nbsp; &nbsp; printf("HeapAlloc error!/n"); <br>&nbsp; &nbsp; &nbsp; &nbsp; return RTN_ERROR; <br>&nbsp; &nbsp; } <br>&nbsp;<br>&nbsp; &nbsp; // <br>&nbsp; &nbsp; // get the Sid associated with the supplied user/group name <br>&nbsp; &nbsp; // force Unicode API since we always pass Unicode string <br>&nbsp; &nbsp; // <br>&nbsp;<br>&nbsp; &nbsp; if(!LookupAccountNameW( <br>&nbsp; &nbsp; &nbsp; &nbsp; NULL, &nbsp; &nbsp; &nbsp; // default lookup logic <br>&nbsp; &nbsp; &nbsp; &nbsp; Username, &nbsp; // user/group of interest from commandline <br>&nbsp; &nbsp; &nbsp; &nbsp; pSid, &nbsp; &nbsp; &nbsp; // Sid buffer <br>&nbsp; &nbsp; &nbsp; &nbsp; &amp;cbSid, &nbsp; &nbsp; // size of Sid <br>&nbsp; &nbsp; &nbsp; &nbsp; RefDomain, &nbsp;// Domain account found on (unused) <br>&nbsp; &nbsp; &nbsp; &nbsp; &amp;cchDomain, // size of domain in chars <br>&nbsp; &nbsp; &nbsp; &nbsp; &amp;peUse <br>&nbsp; &nbsp; &nbsp; &nbsp; )) { <br>&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; // <br>&nbsp; &nbsp; &nbsp; &nbsp; // if the buffer wasn't large enough, try again <br>&nbsp; &nbsp; &nbsp; &nbsp; // <br>&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) { <br>&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pSid = (PSID)HeapReAlloc(GetProcessHeap(), 0, pSid, cbSid); <br>&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(pSid == NULL) { <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("HeapReAlloc error!/n"); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goto cleanup; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } <br>&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cchDomain = DNLEN + 1; <br>&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!LookupAccountNameW( <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NULL, &nbsp; &nbsp; &nbsp; // default lookup logic <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Username, &nbsp; // user/group of interest from commandline <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pSid, &nbsp; &nbsp; &nbsp; // Sid buffer <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;cbSid, &nbsp; &nbsp; // size of Sid <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RefDomain, &nbsp;// Domain account found on (unused) <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;cchDomain, // size of domain in chars <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;peUse <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )) { <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("LookupAccountName error! (rc=%lu)/n", GetLastError()); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goto cleanup; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } <br>&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; } else { <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("LookupAccountName error! (rc=%lu)/n", GetLastError()); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goto cleanup; <br>&nbsp; &nbsp; &nbsp; &nbsp; } <br>&nbsp; &nbsp; } <br>&nbsp;<br>&nbsp; &nbsp; // <br>&nbsp; &nbsp; // compute size of new acl <br>&nbsp; &nbsp; // <br>&nbsp;<br>&nbsp; &nbsp; dwAclSize = sizeof(ACL) + <br>&nbsp; &nbsp; &nbsp; &nbsp; 1 * ( sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) ) + <br>&nbsp; &nbsp; &nbsp; &nbsp; GetLengthSid(pSid) ; <br>&nbsp;<br>&nbsp; &nbsp; // <br>&nbsp; &nbsp; // allocate storage for Acl <br>&nbsp; &nbsp; // <br>&nbsp;<br>&nbsp; &nbsp; pDacl = (PACL)HeapAlloc(GetProcessHeap(), 0, dwAclSize); <br>&nbsp; &nbsp; if(pDacl == NULL) goto cleanup; <br>&nbsp;<br>&nbsp; &nbsp; if(!InitializeAcl(pDacl, dwAclSize, ACL_REVISION)) <br>&nbsp; &nbsp; &nbsp; &nbsp; goto cleanup; <br>&nbsp;<br>&nbsp; &nbsp; // <br>&nbsp; &nbsp; // grant GENERIC_ALL (Full Control) access <br>&nbsp; &nbsp; // <br>&nbsp;<br>&nbsp; &nbsp; if(!AddAccessAllowedAce( <br>&nbsp; &nbsp; &nbsp; &nbsp; pDacl, <br>&nbsp; &nbsp; &nbsp; &nbsp; ACL_REVISION, <br>&nbsp; &nbsp; &nbsp; &nbsp; GENERIC_ALL, <br>&nbsp; &nbsp; &nbsp; &nbsp; pSid <br>&nbsp; &nbsp; &nbsp; &nbsp; )) goto cleanup; <br>&nbsp;<br>&nbsp; &nbsp; if(!InitializeSecurityDescriptor(&amp;sd, SECURITY_DESCRIPTOR_REVISION)) <br>&nbsp; &nbsp; &nbsp; &nbsp; goto cleanup; <br>&nbsp;<br>&nbsp; &nbsp; if(!SetSecurityDescriptorDacl(&amp;sd, TRUE, pDacl, FALSE)) { <br>&nbsp; &nbsp; &nbsp; &nbsp; fprintf(stderr, "SetSecurityDescriptorDacl error! (rc=%lu)/n", <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetLastError()); <br>&nbsp; &nbsp; &nbsp; &nbsp; goto cleanup; <br>&nbsp; &nbsp; } <br>&nbsp;<br>&nbsp; &nbsp; // <br>&nbsp; &nbsp; // setup share info structure <br>&nbsp; &nbsp; // <br>&nbsp;<br>&nbsp; &nbsp; si502.shi502_netname = (LPTSTR) Sharename; <br>&nbsp; &nbsp; si502.shi502_type = STYPE_DISKTREE; <br>&nbsp; &nbsp; si502.shi502_remark = NULL; <br>&nbsp; &nbsp; si502.shi502_permissions = 0; <br>&nbsp; &nbsp; si502.shi502_max_uses = SHI_USES_UNLIMITED; <br>&nbsp; &nbsp; si502.shi502_current_uses = 0; <br>&nbsp; &nbsp; si502.shi502_path = (LPTSTR) DirectoryToShare; <br>&nbsp; &nbsp; si502.shi502_passwd = NULL; <br>&nbsp; &nbsp; si502.shi502_reserved = 0; <br>&nbsp; &nbsp; si502.shi502_security_descriptor = &amp;sd; <br>&nbsp;<br>&nbsp; &nbsp; nas = NetShareAdd( <br>&nbsp; &nbsp; &nbsp; &nbsp; (LPTSTR) Server, &nbsp; &nbsp; &nbsp; &nbsp; // share is on local machine <br>&nbsp; &nbsp; &nbsp; &nbsp; 502, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// info-level <br>&nbsp; &nbsp; &nbsp; &nbsp; (LPBYTE)&amp;si502, // info-buffer <br>&nbsp; &nbsp; &nbsp; &nbsp; NULL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// don't bother with parm <br>&nbsp; &nbsp; &nbsp; &nbsp; ); <br>&nbsp;<br>&nbsp; &nbsp; if(nas != NO_ERROR) { <br>&nbsp; &nbsp; &nbsp; &nbsp; printf("NetShareAdd error! (rc=%lu)/n", nas); <br>&nbsp; &nbsp; &nbsp; &nbsp; goto cleanup; <br>&nbsp; &nbsp; } <br>&nbsp;<br>&nbsp; &nbsp; bSuccess = TRUE; // indicate success <br>&nbsp;<br>cleanup: <br>&nbsp;<br>&nbsp; &nbsp; // <br>&nbsp; &nbsp; // free allocated resources <br>&nbsp; &nbsp; // <br>&nbsp; &nbsp; if(pDacl != NULL) <br>&nbsp; &nbsp; &nbsp; &nbsp; HeapFree(GetProcessHeap(), 0, pDacl); <br>&nbsp;<br>&nbsp; &nbsp; if(pSid != NULL) <br>&nbsp; &nbsp; &nbsp; &nbsp; HeapFree(GetProcessHeap(), 0, pSid); <br>&nbsp;<br>&nbsp; &nbsp; if(!bSuccess) { <br>&nbsp; &nbsp; &nbsp; &nbsp; return RTN_ERROR; <br>&nbsp; &nbsp; } <br>&nbsp;<br>&nbsp; &nbsp; return RTN_OK;
 
下边的程序,<br>封装 Windows95/98 SvrAPI 的单元,<br>提供开发人员轻松的让某个文件夹可以成为透过网络共享的状态 <br>请认真的阅读代码……其实与sonie的程序原理是一样的,<br><br>unit Sharing;<br>interface<br>uses Sysutils;<br><br>Type<br><br>&nbsp; Share_Info50 = Packed Record<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shi50_netname : Array[0..12] of Char; {13}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shi50_type &nbsp; &nbsp;: Byte;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shi50_flags &nbsp; : Word;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shi50_remark &nbsp;: PChar;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shi50_path &nbsp; &nbsp;: PChar;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shi50_rw_password : Array[0..8] of Char; {9}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shi50_ro_password : Array[0..8] of Char;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;End;<br><br>Const<br>{Use ShareErr for Error Constants &amp; Error functions}<br><br>{Resource Type Constants}<br>&nbsp; STYPE_DISKTREE = 0; {Directory Share}<br>&nbsp; STYPE_PRINTQ &nbsp; = 1; {Printer Share}<br><br>{Flag Constants}<br>&nbsp; SHI50F_RDONLY &nbsp;= 1; &nbsp;{ Share is Read Only}<br>&nbsp; SHI50F_FULL &nbsp; &nbsp;= 2; &nbsp;{ Share is Full Access}<br>&nbsp; SHI50F_DEPENDSON = (SHI50F_RDONLY or SHI50F_FULL); {Access depends upon password entered by user}<br><br>&nbsp; {OR the following with access constants to use them.<br>&nbsp; &nbsp;I.E.: flags := (SHI50F_RDONLY OR SHI50F_SYSTEM) }<br>&nbsp; SHI50F_PERSIST = 256; {The share is restored on system startup}<br>&nbsp; SHI50F_SYSTEM &nbsp;= 512; {The share is not normally visible}<br><br><br>{ ShareResource: Shares a resource on the specified machine.<br>&nbsp; Parameters:<br>&nbsp; &nbsp; ServerName= Name of server on which to share resource. Nil = Local Machine.<br>&nbsp; &nbsp; FilePath &nbsp;= Path to the resource to be shared. (This should be ALL upper-case);<br>&nbsp; &nbsp; NetName &nbsp; = Network Name to give the shared resource. Must be 12 characters or less.<br>&nbsp; &nbsp; Remark &nbsp; &nbsp;= Comment. Can be an empty string.<br>&nbsp; &nbsp; ShareType = Type of resource. See Constants declared above.<br>&nbsp; &nbsp; Flags &nbsp; &nbsp; = Sharing flags. See Constants declared above.<br>&nbsp; &nbsp; RWPass &nbsp; &nbsp;= Full Access Password - Must be 8 characters or less. Can be an empty string.<br>&nbsp; &nbsp; ROPass &nbsp; &nbsp;= Read Only Password - Must be 8 characters or less. Can be an empty string.<br><br>&nbsp; Example Call: ShareResource(Nil, 'C:/TEMP', 'TESTING', 'My Comment', STYPE_DISKTREE, SHI50F_RDONLY, '','MYPASS');<br>&nbsp; &nbsp; This Shares Disk Resource C:/TEMP as 'TESTING' with comment 'My Comment' on the local machine.<br>&nbsp; &nbsp; Access is Read Only, with Read Only password = 'MYPASS'. No Full Access Password specified.<br>&nbsp; &nbsp; (It would be ignored if specified) }<br>function ShareResource(ServerName : PChar; FilePath : PChar;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NetName : PChar; Remark : PChar;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShareType : Byte; Flags : Word;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RWPass : PChar; ROPass : PChar ) : Integer;<br><br><br><br>{ DeleteShare: Deletes a shared resource on the specified machine.<br>&nbsp; Parameters:<br>&nbsp; &nbsp; ServerName= Name of server on which to share resource. Nil = Local Machine.<br>&nbsp; &nbsp; NetName &nbsp; = Network Name of the shared resource.<br><br>&nbsp; Example Call: DeleteShare(Nil, 'TESTING');<br>&nbsp; &nbsp; This Deletes The network share named 'TESTING' on the local machine.}<br>function DeleteShare(ServerName : PChar; NetName : PChar) : Integer;<br><br><br>{ GetShareInfo: Gets information about a shared resource on the specified machine.<br>&nbsp; Parameters:<br>&nbsp; &nbsp; ServerName &nbsp;= Name of server where the shared resource resides. Nil = Local Machine.<br>&nbsp; &nbsp; NetName &nbsp; &nbsp; = Network Name of the shared resource. Must be 12 characters or less.<br>&nbsp; &nbsp; ShareStruct = Share_Info50. This structure will be filled with information on the<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; specified share if the function succeeds.<br><br>&nbsp; Example Call:<br>&nbsp; &nbsp; var MyShareStruct : Share_Info50;<br>&nbsp; &nbsp; GetShareInfo(Nil, 'TESTING', MyShareStruct);<br><br>&nbsp; &nbsp; This fills MyShareStruct with share information about 'TESTING' on the local machine.}<br>function GetShareInfo(ServerName : PChar; NetName : PChar; Var ShareStruct : Share_Info50) : Integer;<br><br>{ SetShareInfo: Sets information for a shared resource on the specified machine.<br>&nbsp; Parameters:<br>&nbsp; &nbsp; ServerName &nbsp;= Name of server where the shared resource resides. Nil = Local Machine.<br>&nbsp; &nbsp; NetName &nbsp; &nbsp; = Network Name of the shared resource. Must be 12 characters or less.<br>&nbsp; &nbsp; ShareStruct = Share_Info50. This structure contains the new information for the shared<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resource. It is easiest to fill this structure first with GetShareInfo and<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then change desired parameters; however you may fill it completely yourself.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; You may not change the path of a shared resource, if you change this<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parameter in the structure, it will be ignored.<br><br>&nbsp; Example Call: SetShareInfo(Nil, 'TESTING', MyShareStruct);<br>&nbsp; &nbsp; This changes the share information for 'TESTING' to reflect the data in MyShareStruct}<br>function SetShareInfo(ServerName : PChar; NetName : PChar; ShareStruct : Share_Info50) : Integer;<br><br>{These are the translated SVRAPI exports. I suggest you not use them yourself. Rather, use<br>&nbsp;the encapsulated versions I have written for you. If you wish to learn more about these<br>&nbsp;functions, browse the win32.hlp file.}<br>function NetShareAdd(ServerName : PChar; ShareLevel : SmallInt; Buffer : Pointer; Size : Word) : Integer; StdCall;<br>function NetShareDel(ServerName : PChar; NetName : PChar; Reserved : Word) : Integer; StdCall;<br>function NetShareGetInfo(ServerName : PChar; NetName : PChar; ShareLevel : SmallInt; Buffer : Pointer; Size : Word; Var Used : Word) : Integer; StdCall;<br>function NetShareSetInfo(ServerName : PChar; NetName : PChar; ShareLevel : SmallInt; Buffer : Pointer; Size : Word; Reserved : SmallInt) : Integer; StdCall;<br><br><br>implementation<br><br>function ShareResource(ServerName : PChar; FilePath : PChar;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NetName : PChar; Remark : PChar;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShareType : Byte; Flags : Word;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RWPass : PChar; ROPass : PChar ) : Integer;<br>&nbsp;var MyShare : Share_Info50;<br>&nbsp; &nbsp; &nbsp;PMyShare : ^Share_Info50;<br>begin<br>&nbsp; strLcopy(MyShare.shi50_netname,NetName,13);<br>&nbsp; MyShare.shi50_type := ShareType;<br>&nbsp; MyShare.shi50_flags := Flags;<br>&nbsp; MyShare.shi50_remark := Remark;<br>&nbsp; MyShare.shi50_path := FilePath;<br>&nbsp; strLcopy(MyShare.shi50_rw_password,RWPass,9);<br>&nbsp; strLcopy(MyShare.shi50_ro_password,ROPass,9);<br>&nbsp; PMyShare := @MyShare;<br>&nbsp; Result := NetShareAdd(ServerName,50,PMyShare,SizeOf(MyShare));<br>end;<br><br>function DeleteShare(ServerName : PChar; NetName : PChar) : Integer;<br>begin<br>&nbsp; Result := NetShareDel(ServerName,NetName,0);<br>end;<br><br>function GetShareInfo(ServerName : PChar; NetName : PChar; Var ShareStruct : Share_Info50) : Integer;<br>&nbsp;var PMyShare : ^Share_Info50;<br>&nbsp; &nbsp; &nbsp;AmountUsed : Word;<br>&nbsp; &nbsp; &nbsp;Error : Integer;<br>begin<br>&nbsp; PMyShare := AllocMem(255);<br>&nbsp; Error := NetShareGetInfo(ServerName,NetName,50,PMyShare,255,AmountUsed);<br>&nbsp; If Error = 0 Then<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; ShareStruct.shi50_netname := PMyShare.shi50_netname;<br>&nbsp; &nbsp; &nbsp; ShareStruct.shi50_type := PMyShare.shi50_type;<br>&nbsp; &nbsp; &nbsp; ShareStruct.shi50_flags := PMyShare.shi50_flags;<br>&nbsp; &nbsp; &nbsp; ShareStruct.shi50_remark := PMyShare.shi50_remark;<br>&nbsp; &nbsp; &nbsp; ShareStruct.shi50_path := PMyShare.shi50_path;<br>&nbsp; &nbsp; &nbsp; ShareStruct.shi50_rw_password := PMyShare.shi50_rw_password;<br>&nbsp; &nbsp; &nbsp; ShareStruct.shi50_ro_password := PMyShare.shi50_ro_password;<br>&nbsp; &nbsp; End;<br>&nbsp; FreeMem(PMyShare);<br>&nbsp; Result := Error;<br>end;<br><br>function SetShareInfo(ServerName : PChar; NetName : PChar; ShareStruct : Share_Info50) : Integer;<br>&nbsp;var PMyShare : ^Share_Info50;<br>begin<br>&nbsp; PMyShare := @ShareStruct;<br>&nbsp; Result := NetShareSetInfo(ServerName,NetName,50,PMyShare,SizeOf(ShareStruct),0);<br>end;<br><br>function NetShareAdd; &nbsp; &nbsp; external 'SVRAPI.DLL';<br>function NetShareDel; &nbsp; &nbsp; external 'SVRAPI.DLL';<br>function NetShareGetInfo; external 'SVRAPI.DLL';<br>function NetShareSetInfo; external 'SVRAPI.DLL';<br><br>end.<br><br><br><br>
 
//常量定义<br>const<br>&nbsp; &nbsp; &nbsp;NETNAME_LEN = 13;<br>&nbsp; &nbsp; &nbsp;PASSWORD_LEN = 9;<br><br>&nbsp; &nbsp; &nbsp;SHI50F_RDONLY = $0001;<br>&nbsp; &nbsp; &nbsp;SHI50F_FULL &nbsp; = $0002;<br>&nbsp; &nbsp; &nbsp;SHI50F_DEPENDSON &nbsp;= $0003;<br>&nbsp; &nbsp; &nbsp;SHI50F_ACCESSMASK = $0003;<br><br>&nbsp; &nbsp; &nbsp;SHI50F_PERSIST = $0100;<br>&nbsp; &nbsp; &nbsp;SHI50F_SYSTEM &nbsp;= $0200;<br><br>&nbsp; &nbsp; &nbsp;STYPE_DISKTREE = 0;<br>&nbsp; &nbsp; &nbsp;STYPE_PRINTQ = 1;<br>&nbsp; &nbsp; &nbsp;STYPE_DEVICE = 2;<br>&nbsp; &nbsp; &nbsp;STYPE_IPC = 3;<br>&nbsp; &nbsp; &nbsp;NERR_Success = 0;<br>&nbsp; &nbsp; &nbsp;NERR_BASE = 2100;<br><br>//NetShareAdd Return ERROR<br>&nbsp; &nbsp; &nbsp;NERR_UnKnownDevDir = (NERR_BASE+16);<br><br>&nbsp; &nbsp; &nbsp;NERR_UnknownServer = (NERR_BASE+3);<br>&nbsp; &nbsp; &nbsp;NERR_ServerNotStarted = (NERR_BASE+14);<br>&nbsp; &nbsp; &nbsp;NERR_RedirectedPath = (NERR_BASE+17);<br>&nbsp; &nbsp; &nbsp;NERR_DuplicateShare = (NERR_BASE+18);<br>&nbsp; &nbsp; &nbsp;NERR_BufTooSmalll = (NERR_BASE+23);<br><br>//NetShareDel Return ERROR<br>&nbsp; &nbsp; &nbsp;NERR_NetNotStarted = (NERR_BASE+2);<br>// &nbsp; &nbsp; NERR_ServerNotStarted = (NERR_BASE+14);<br>&nbsp; &nbsp; &nbsp;NERR_NetNameNotFound = (NERR_BASE+210);<br>&nbsp; &nbsp; &nbsp;NERR_ShareNotFound = (NERR_BASE+292);<br>//类型定义<br>&nbsp; SHARE_INFO_50=Record<br>&nbsp; &nbsp; netname:array [0..NETNAME_LEN-1] of Char;<br>&nbsp; &nbsp; sharetype:ShortInt;<br>&nbsp; &nbsp; flags:SmallInt;<br>&nbsp; &nbsp; remark:PChar;<br>&nbsp; &nbsp; path:PChar;<br>&nbsp; &nbsp; rw_password:array [0..PASSWORD_LEN-1] of Char;<br>&nbsp; &nbsp; ro_password:array [0..PASSWORD_LEN-1] of Char;<br>&nbsp; End;<br><br>//Win95/98中共享目录<br>Function NetShareAdd(servername:PChar; level:SmallInt;<br>&nbsp; buf:Pointer; buf_len:SmallInt):SmallInt;far;stdcall;external 'svrapi.dll';<br><br>//Win95/98中撤销共享目录<br>Function NetShareDel(servername:PChar;<br>&nbsp; buf:Pointer; reserved:SmallInt):SmallInt;far;stdcall;external 'svrapi.dll';<br><br>//使用方可以用如下函数,连接共享目录/撤销<br>WNetAddConnection<br>WNetCancelConnection<br><br>//------End----<br>
 
Netshareadd的头文件是:imshare.h.<br>输入库:netapi32.lib(winnt),<br>svrapi.lib(win95)<br><br>
 
多人接受答案了。
 
后退
顶部