兄弟们帮个忙,如何用程序实现文件夹的共享?(50分)

  • 主题发起人 主题发起人 3cs
  • 开始时间 开始时间
3

3cs

Unregistered / Unconfirmed
GUEST, unregistred user!
如何用程序实现文件夹的共享?
 
方案一、 写入注册表,至于位置我就不说了 (need restart)
方案二、api(not need restart)
 
2000和98需要使用不同的动态库函数,下面是我写的以动态加载方式使用API来共享和取消
共享文件。
DLLHandle 是一个全局变量。
//----------------------------------------------------------------------
function LoadNetAdd():integer;
var
sOS : string;
begin
Result := 0;
sOS := GetOsVersion();
if sOs = 'Windows 95' then
DLLHandle := LoadLibrary(PChar('SVRAPI.DLL'));{Try Loading the Win9x library}
if sOs = 'Windows NT' then
DLLHandle := LoadLibrary(PChar('NETAPI32.DLL'));
If (DLLHandle > 0) then
@NetShareAdd := GetProcAddress(DLLHandle, PChar('NetShareAdd'))
else
Result := GetLastError();
end;
//----------------------------------------------------------------------
function LoadNetDel():integer;
var
sOS : string;
begin
result := 0;
sOS := GetOsVersion();
if sOs = 'Windows 95' then
DLLHandle := LoadLibrary(PChar('SVRAPI.DLL'));{Try Loading the Win9x library}
if sOs = 'Windows NT' then
DLLHandle := LoadLibrary(PChar('NETAPI32.DLL'));
If (DLLHandle > 0) then
@NetShareDel := GetProcAddress(DLLHandle, PChar('NetShareDel'))
else
Result := GetLastError();
end;
//----------------------------------------------------------------------
function GetOsVersion():string;
var
OS: TOSVersionInfo;
begin
Result := '';
OS.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
if GetVersionEx(OS) = True then
begin
case OS.dwPlatformId of
VER_PLATFORM_WIN32s : Result := 'Win32';
VER_PLATFORM_WIN32_WINDOWS : Result := 'Windows 95';
VER_PLATFORM_WIN32_NT : Result := 'Windows NT';
end;
end;
end;
//-----------------------------------------------------
// 取消共享的文件夹
function CancelShareFolder(sShare:string):integer;
var
s : WideString;
sTemp : TStringList;
rlt : integer;
begin
Result := 0;
delete(sShare,1,2);
sTemp := SplitString(sShare,'/');
sShare := sTemp.Strings[1];
s := WideString(sShare);
rlt := LoadNetDel();
if rlt <> 0 then
begin
Result := rlt;
Exit;
end;
if NetShareDel('',pWideChar(s),0) <> 0 then
Result := GetLastError();
FreeLibrary(DllHandle);
end;
//-----------------------------------------------------------------
//取得计算机名
function GetMacName():string;
var
pComputerName:PChar;
ComputerNameLen:DWORD;
begin
ComputerNameLen:=255;
GetMem(pComputerName,ComputerNameLen);
try
if not GetComputerName(pComputerName,ComputerNameLen) then
pComputerName:='NoName';
Result:=String(PComputerName);
finally
FreeMem(pComputerName);
End;
End;
//-----------------------------------------------------------------
// 共享文件 sShare-共享名 TargetPath-要共享的本地文件夹
function ShareFolder(var sShare:string; TargetPath : string):integer;
var
si: TSHARE_INFO_502;
r: DWORD;
parm_err:lpDword;
sPathTemp,
sShareTemp : widestring;
rlt : integer;
begin
Result := 0;
sShareTemp := 'ShareForResLib';
sPathTemp := WideString(TargetPath);
si.shi502_netname := pwidechar(sShareTemp); //(共享名)
si.shi502_type := 0;
si.shi502_remark := nil;
si.shi502_max_uses := $FFFFFFFF;
si.shi502_permissions := TRUSTEE_ACCESS_ALL;
si.shi502_current_uses:= 10;
si.shi502_path := pWideChar(sPathTemp); //(原路径)
si.shi502_passwd := nil;
si.shi502_reserved := 0;
si.shi502_security_descriptor := nil;
rlt := LoadNetAdd();
if rlt <> 0 then
begin
Result := rlt;
Exit;
end;
r := NetShareAdd('', 502, @si, parm_err);
if r = 0 then
sShare := '//'+GetMacName()+'/'+sShareTemp+'/'
else
Result := GetLastError();
FreeLibrary(DllHandle);
end;
 
我试试,如果行我一定给分。
 
to:youngsun
我编译时出错,请你告诉我NetShareDel是什么数据类型?dllhandle是什么数据类型?
 
比较土、也是最简单的办法:
winexec('net share 共享名=d:/mypubs',sw_hide);
ok!
 
真是抱歉,国庆前我回家去了,下面是一些相关的声明,希望能有所帮助。[:I]
TSHARE_INFO_502 = record
shi502_netname: PWideChar;
shi502_type: DWORD;
shi502_remark: PWideChar;
shi502_permissions: DWORD;
shi502_max_uses: DWORD;
shi502_current_uses: DWORD;
shi502_path: PWideChar;
shi502_passwd: PWideChar;
shi502_reserved: DWORD;
shi502_security_descriptor: PSECURITY_DESCRIPTOR;
end;
PBYTE = ^BYTE;
PDWORD = ^DWORD;
lpNetShareAdd = function(ServerName : Widestring;
ShareLevel : DWORD;
Buffer : PBYTE;
var Parm_err : LPDWORD) : DWORD; StdCall;
lpNetShareDel = Function(servername,
NetName : WideString;
reserved : DWORD):DWORD;StdCall;

//-----------------------------------------------------------------
function GetFileLen(f:string):Integer;
function GetDirSize(Path: String): Integer;
function SplitString(const source,ch:string):TStringList;
function CancelShareFolder(sShare:string):integer;
function ShareFolder(var sShare:string; TargetPath : string):integer;
//function NetShareAdd(servername:Widestring; level: DWORD; buf: PBYTE;
// VAR parm_err: LPDWORD ): DWORD; stdcall;
//function NetShareDel(servername, NetName:Widestring;
// reserved: DWORD): DWORD; stdcall;
function UserInfo(User,Pwd:string; sQry:TQuery):boolean;
function GetAllUser(var AllUser : string):boolean;
function ExtractPath(sPath:string):string;
procedure MakeTree(Path : string; fTree : TTreeView; ParentNode : TTreeNode);
procedure AddSortLeaf(node : TTreeNode;Tree:TTreeView);
function MakeSortTree(Tree : TTreeView):Boolean;
function ConvertSize(Size : string):string;
function GetMacName():String;
function ModiResNote(sName,sNote:string):boolean;
function ModiSortNote(sName,sNote:string):boolean;
function GetSortPath(sSort:string):string;
function LoadNetAdd():integer;
function LoadNetDel():integer;
function GetOsVersion():string;
//-----------------------------------------------------------------
var
User_Name : string;
User_ID : integer;
sShareFolder : string;
NetShareAdd : lpNetShareAdd;
NetShareDel : lpNetShareDel;
DLLHandle : THandle;
sRootPath : string;
 
高手很多,洗耳恭听!
 
多人接受答案了。
 
后退
顶部