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

  • 主题发起人 主题发起人 philipliu
  • 开始时间 开始时间
P

philipliu

Unregistered / Unconfirmed
GUEST, unregistred user!
如何共享文件夹?请写上详细代码,谢啦。
 
给我发邮件, tseug@263.net
 
&nbsp;function NetShareAdd(servername:Widestring; level: DWORD; buf: PBYTE; VAR &nbsp;parm_err: LPDWORD ): DWORD; stdcall<br><br>&nbsp;var<br>function NetShareAdd; external 'netapi32.DLL' name 'NetShareAdd';<br><br>function get_ip():string;<br>var<br>&nbsp; wVersionRequested : WORD;<br>&nbsp; wsaData : TWSAData;<br>&nbsp; p : PHostEnt;<br>&nbsp; s : array[0..128] of char;<br>&nbsp; p2 : pchar;<br>&nbsp; OutPut:array[0..100] of char;<br>begin<br>{Start up WinSock}<br>&nbsp; &nbsp; &nbsp;wVersionRequested := MAKEWORD(1, 1);<br>&nbsp; &nbsp; &nbsp;WSAStartup(wVersionRequested, wsaData);<br><br>{Get the computer name}<br>&nbsp; &nbsp; &nbsp;GetHostName(@s, 128);<br>&nbsp; &nbsp; &nbsp;p := GetHostByName(@s);<br><br>{Get the IpAddress}<br>&nbsp; &nbsp; &nbsp;p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);<br>&nbsp; &nbsp; &nbsp;StrPCopy(OutPut,'Hostname: '+Format('%s', [p^.h_Name])+#10#13+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'IPaddress: '+Format('%s',[p2])<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;);<br>&nbsp; &nbsp; &nbsp;WSACleanup;<br>&nbsp; &nbsp; &nbsp;get_ip:=Format('%s',[p2]);<br>end;<br><br>function addshare(pathstr:string):string; &nbsp; &nbsp; &nbsp;// &nbsp; &nbsp;创建计算机共享目录的函数<br>var<br>&nbsp; ServerName:widestring;<br>&nbsp; si: TSHARE_INFO_502;<br>&nbsp; r: DWORD;<br>&nbsp; parm_err:LPDword;<br>&nbsp; chararry:array [0..150] of WideChar;<br>begin<br>&nbsp; si.shi502_netname := 'kdedata'; // &nbsp;(共享名)<br>&nbsp; si.shi502_type := 0; &nbsp;//STYPE_DISKTREE<br>&nbsp; si.shi502_remark := nil;<br>&nbsp; si.shi502_max_uses := $FFFFFFFF;<br>&nbsp; si.shi502_current_uses := 10;<br>&nbsp; si.shi502_path :=StringToWideChar(pathstr,chararry,150); // &nbsp;(原路径)<br>&nbsp; si.shi502_passwd := nil;<br>&nbsp; si.shi502_reserved := 0;<br>&nbsp; si.shi502_security_descriptor := nil;<br>&nbsp; si.shi502_permissions:=0;<br>&nbsp; ServerName:=get_ip();<br>// &nbsp;strpcopy(ServerName,'vodserver'); // &nbsp;('vodserver'是机器名)<br>&nbsp; try<br>&nbsp; &nbsp; r := NetShareAdd(ServerName, 502, @si, parm_err );<br>&nbsp; Finally<br>// &nbsp; &nbsp;strdispose(ServerName);<br>&nbsp; end;<br>&nbsp; addshare := Format( '%d', [r] );<br><br>// &nbsp;Edit2.Text:=inttostr(parm_err);<br>end;<br>
 
to agangr:<br>&nbsp; TSHARE_INFO_502是什么东东?
 
到MSDN 或微软网站找,或搜索本BBS
 
或直接用net share命令吧,怎么用看帮助
 
谁有详细点的例子?
 
win 9x下的 c 代码:<br><br>typedef struct _share_info_50 <br>{<br>&nbsp; &nbsp;char shi50_netname[LM20_NNLEN+1];<br>&nbsp; &nbsp;unsigned char shi50_type;<br>&nbsp; &nbsp;unsigned short shi50_flags;<br>&nbsp; &nbsp;char FAR* shi50_remark;<br>&nbsp; &nbsp;char FAR* shi50_path;<br>&nbsp; &nbsp;char shi50_rw_password[SHPWLEN+1];<br>&nbsp; &nbsp;char shi50_ro_password[SHPWLEN+1];<br>} _share_info_50;<br><br><br>#include &lt;stdio.h&gt;<br>#include &lt;windows.h&gt; <br>#include &lt;svrapi.h&gt;<br><br>int main(int argc, char FAR * argv[])<br>{<br>&nbsp; &nbsp;char FAR * pszServerName = NULL;<br>&nbsp; &nbsp;short nLevel = 50;<br>&nbsp; &nbsp;struct share_info_50* pBuf = NULL;<br>&nbsp; &nbsp;unsigned short cbBuffer;<br>&nbsp; &nbsp;NET_API_STATUS nStatus;<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;// ServerName can be NULL to indicate the local computer.<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;if ((argc &lt; 3) || (argc &gt; 4))<br>&nbsp; &nbsp;{<br>&nbsp; &nbsp; &nbsp; printf("Usage: %s [////ServerName] ShareName SharePath/n", argv[0]);<br>&nbsp; &nbsp; &nbsp; exit(1);<br>&nbsp; &nbsp;}<br><br>&nbsp; &nbsp;if (argc == 4)<br>&nbsp; &nbsp; &nbsp; pszServerName = argv[1];<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;// Allocate the memory required to specify a <br>&nbsp; &nbsp;// &nbsp; share_info_50 structure.<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;cbBuffer = sizeof(struct share_info_50);<br>&nbsp; &nbsp;pBuf = malloc(cbBuffer);<br><br>&nbsp; &nbsp;if (pBuf == NULL)<br>&nbsp; &nbsp; &nbsp; printf("No memory/n");<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;// Assign values to the share_info_50 structure.<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;strcpy(pBuf-&gt;shi50_netname, argv[argc-2]);<br>&nbsp; &nbsp;pBuf-&gt;shi50_type = STYPE_DISKTREE;<br>&nbsp; &nbsp;pBuf-&gt;shi50_flags = SHI50F_FULL;<br>&nbsp; &nbsp;pBuf-&gt;shi50_remark = NULL;<br>&nbsp; &nbsp;pBuf-&gt;shi50_path = argv[argc-1];<br>&nbsp; &nbsp;pBuf-&gt;shi50_rw_password[0] = '/0'; // No password<br>&nbsp; &nbsp;pBuf-&gt;shi50_ro_password[0] = '/0'; // No password<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;// Call the NetShareAdd function<br>&nbsp; &nbsp;// &nbsp;specifying information level 50.<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;nStatus = NetShareAdd(pszServerName,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nLevel,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(char FAR *)pBuf,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cbBuffer);<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;// Display the result of the function call.<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;if (nStatus == NERR_Success)<br>&nbsp; &nbsp; &nbsp; printf("Share added successfully/n");<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; fprintf(stderr, "A system error has occurred: %d/n", nStatus);<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;// Free the allocated memory.<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;if (pBuf != NULL)<br>&nbsp; &nbsp; &nbsp; free(pBuf);<br><br>&nbsp; &nbsp;return 0;<br>}<br><br>win 2K 下的C代码:<br><br>#define UNICODE<br>#include &lt;windows.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;lm.h&gt;<br><br>void wmain( int argc, TCHAR *argv[ ])<br>{<br>&nbsp; &nbsp;NET_API_STATUS res;<br>&nbsp; &nbsp;SHARE_INFO_2 p;<br>&nbsp; &nbsp;DWORD parm_err = 0;<br><br>&nbsp; &nbsp;if(argc&lt;2)<br>&nbsp; &nbsp; &nbsp; printf("Usage: NetShareAdd server/n");<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp;{<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; // Fill in the SHARE_INFO_2 structure.<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; p.shi2_netname = TEXT("TESTSHARE"); &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; p.shi2_type = STYPE_DISKTREE; // disk drive<br>&nbsp; &nbsp; &nbsp; p.shi2_remark = TEXT("TESTSHARE to test NetShareAdd");<br>&nbsp; &nbsp; &nbsp; p.shi2_permissions = 0; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; p.shi2_max_uses = 4;<br>&nbsp; &nbsp; &nbsp; p.shi2_current_uses = 0; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; p.shi2_path = TEXT("C://");<br>&nbsp; &nbsp; &nbsp; p.shi2_passwd = NULL; // no password<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; // Call the NetShareAdd function,<br>&nbsp; &nbsp; &nbsp; // &nbsp;specifying level 2.<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; res=NetShareAdd(argv[1], 2, (LPBYTE) &amp;p, &amp;parm_err);<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; // If the call succeeds, inform the user.<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; if(res==0)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("Share created./n");<br>&nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; // Otherwise, print an error,<br>&nbsp; &nbsp; &nbsp; // &nbsp;and identify the parameter in error.<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("Error: %u/tparmerr=%u/n", res, parm_err);<br>&nbsp; &nbsp;}<br>&nbsp; &nbsp;return;<br>}<br><br><br><br>delphi 下的参考:<br><br>type<br>TShare_INFO_2 = record<br>&nbsp; &nbsp; shi2_netname :PWideString;<br>&nbsp; &nbsp; shi2_type :DWord;<br>&nbsp; &nbsp; shi2_remark :PWideString;<br>&nbsp; &nbsp; shi2_permissions :DWord;<br>&nbsp; &nbsp; shi2_max_uses :DWord;<br>&nbsp; &nbsp; shi2_current_uses :DWord;<br>&nbsp; &nbsp; shi2_path :PWideString;<br>&nbsp; &nbsp; shi2_passwd :PWideString;<br>end;<br><br>function NetShareAdd(ServerName : PWideChar;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Level : Longword;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Buffer : Pointer;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var ParamError : Longword) : Longword;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br>function NetShareAdd; external 'netapi32.DLL' name 'NetShareAdd';<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; shareAttrresult,shareAttrtemp:string;<br>&nbsp; i :integer;<br>&nbsp; si :TSHARE_INFO_2;<br>&nbsp; parm_err :pDWord;<br>&nbsp; severname :pchar;<br>&nbsp; servername:OSVERSIONINFO;<br>&nbsp; PwString:PwideChar;<br>&nbsp; wstring:string;<br><br>&nbsp; Net_Name,Remark,thePath,Password:widestring;<br>//////////////////////////<br>begin<br>wstring:='savedfdf';<br>&nbsp; &nbsp; Net_Name:='save';<br>&nbsp; &nbsp; si.shi2_netname:=@Net_Name;<br><br>&nbsp; &nbsp; si.shi2_type :=STYPE_DISKTREE;<br><br>&nbsp; &nbsp; ReMark:='abcde';<br>&nbsp; &nbsp; si.shi2_remark :=@Remark;<br><br>&nbsp; &nbsp; si.shi2_permissions :=ACCESS_READ;<br> <br>&nbsp; &nbsp; si.shi2_max_uses :=-1;<br>&nbsp; &nbsp; si.shi2_current_uses :=10;<br><br>&nbsp; &nbsp; thePath:='f:/save';<br> si.shi2_path :=@thePath;<br><br> Password:='123456';<br>&nbsp; &nbsp; si.shi2_passwd :=@Password;<br> &nbsp; &nbsp;//共享级安全才有用。用户级安全忽略<br><br>&nbsp; &nbsp; NetShareAdd(nil ,2,@si,@parm_err);<br> <br>
 
还是不行啊,程序试过没?
 
参照 C 代码去改,微软对这个函数已经修正过了。<br>或搜索一下 NetShareAdd ,我以前回答过。
 
<br>&nbsp; TSHARE_INFO_502 = record<br>&nbsp; &nbsp; shi502_netname: PWideChar;<br>&nbsp; &nbsp; shi502_type: DWORD;<br>&nbsp; &nbsp; shi502_remark: PWideChar;<br>&nbsp; &nbsp; shi502_permissions: DWORD;<br>&nbsp; &nbsp; shi502_max_uses: DWORD;<br>&nbsp; &nbsp; shi502_current_uses: DWORD;<br>&nbsp; &nbsp; shi502_path: PWideChar;<br>&nbsp; &nbsp; shi502_passwd: PWideChar;<br>&nbsp; &nbsp; shi502_reserved: DWORD;<br>&nbsp; &nbsp; shi502_security_descriptor: PSECURITY_DESCRIPTOR;<br>&nbsp; end;<br><br>
 
后退
顶部