关于目录共享,请高手指教! ( 积分: 100 )

  • 主题发起人 主题发起人 zjzwh
  • 开始时间 开始时间
Z

zjzwh

Unregistered / Unconfirmed
GUEST, unregistred user!
在WIN98/ME环境下共享目录,在论坛中通过全文检索,知道可以直接调用标准的Win32API函数 NetShareAdd()和NetShareDel()。
建立共享目录函数:Function NetShareAdd(servername:PChar; level:SmallInt;buf:Pointer; buf_len:SmallInt):SmallInt;far;stdcall;external 'svrapi.dll';
撤销共享目录函数:Function NetShareDel(servername:PChar;buf:Pointer; reserved:SmallInt):SmallInt;far;stdcall;external 'svrapi.dll';
但不知对应Delphi的参数应怎么设置,请高手指教!最好详细一点,一般设置的资料我手头已经不少,就是看不懂。
 
在WIN98/ME环境下共享目录,在论坛中通过全文检索,知道可以直接调用标准的Win32API函数 NetShareAdd()和NetShareDel()。
建立共享目录函数:Function NetShareAdd(servername:PChar; level:SmallInt;buf:Pointer; buf_len:SmallInt):SmallInt;far;stdcall;external 'svrapi.dll';
撤销共享目录函数:Function NetShareDel(servername:PChar;buf:Pointer; reserved:SmallInt):SmallInt;far;stdcall;external 'svrapi.dll';
但不知对应Delphi的参数应怎么设置,请高手指教!最好详细一点,一般设置的资料我手头已经不少,就是看不懂。
 
mark 下下
 
QSmile
分不够可以再加。
 
在98平台,试试以下的Delphi实现:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

const NETNAME_LEN = 13;
PASSWORD_LEN = 9;

SHI50F_RDONLY = $0001;
SHI50F_FULL = $0002;
SHI50F_DEPENDSON = $0003;
SHI50F_ACCESSMASK = $0003;
SHI50F_PERSIST = $0100;
SHI50F_SYSTEM = $0200;

STYPE_DISKTREE = 0;
STYPE_PRINTQ = 1;
STYPE_DEVICE = 2;
STYPE_IPC = 3;

NERR_Success = 0;
NERR_BASE = 2100; //常量说明
NERR_UnKnownDevDir=(NERR_BASE+16);
NERR_UnknownServer=(NERR_BASE+3);
NERR_ServerNotStarted=(NERR_BASE+14);
NERR_RedirectedPath=(NERR_BASE+17);
NERR_DuplicateShare=(NERR_BASE+18);
NERR_BufTooSmalll=(NERR_BASE+23);
type
TSHARE_INFO_50=Record
netname:array [0..NETNAME_LEN-1] of Char;
sharetype:ShortInt;
flags:SmallInt;
remark:PChar;
path:PChar;
rw_password:array [0..PASSWORD_LEN-1] of Char;
ro_password:array [0..PASSWORD_LEN-1] of Char;
End; //以上为类型定义

function NetShareAdd(ServerName : PWideString;Level : Dword;
Buffer : pByte;ParamError :pDword) : SmallInt;

var
Form1: TForm1;

implementation

{$R *.DFM}
function NetShareAdd; external 'svrapi.DLL' name 'NetShareAdd';

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
si:TSHARE_INFO_50;
parm_err :DWord;
begin
si.netname:='save';
si.shareType :=STYPE_DISKTREE;
si.flags := SHI50F_FULL;
si.remark :='Abcde';
si.path :='D:/temp';
si.rw_password :='';
si.ro_password :='';
i:=NetShareAdd(nil,50,pbyte(@si),@parm_err);
if (i<>NERR_Success) then
showmessage('Error:'+inttostr(i));
end;
end.
 
Wfisher你好,先谢谢了。你给出的代码可以运行,结果提示:Erorr:124 。函数NetShareAdd(nil,50,pbyte(@si),@parm_err);中的参数有些不明白,而且目录没有共享,还请指教!

这问题贴出好几天了,请其他的高手也帮帮忙,运行通过立即结贴,分不够再加。
 
{老兄给你一段代码,保证可以通过,要共享子目录可修改相应的代码。这不是我写的但我程序里用这段代码修改后实现了我的功能(共享子目录),见前面我的帖子。有空请帮我看看,看能否解决我的问题。}
unit FileShare;

interface

uses windows;

type
NET_API_STATUS = type DWORD; //返回类型

const
LM20_NNLEN = 12; //网络名长度
SHPWLEN = 8; //密码长度

const
STYPE_DISKTREE = 0;
STYPE_PRINTQ = 1;
STYPE_DEVICE = 2;
STYPE_IPC = 3; //资源类型

const //共享权限
SHI50F_RDONLY = $0001;
SHI50F_FULL = $0002;
SHI50F_DEPENDSON = (SHI50F_RDONLY or SHI50F_FULL);
SHI50F_ACCESSMASK = (SHI50F_RDONLY or SHI50F_FULL);
SHI50F_PERSIST = $0100; //The share is restored on system startup
SHI50F_SYSTEM = $0200; //The share is not normally visible

const
NERR_Success = 0; //成功标志

type //共享信息结构
SHARE_INFO_50 = record
shi50_netname: array[0..LM20_NNLEN] of char;
shi50_type: byte;
shi50_flags: word;
shi50_remark: pchar;
shi50_path: pchar;
shi50_rw_password: array[0..SHPWLEN] of char;
shi50_ro_password: array[0..SHPWLEN] of char;
end;

type //调用函数类型
fnAdd = function(servername: Pchar; level: smallint;
buf: pchar; buflen: word): NET_API_STATUS; stdcall;
fnDel = function(servername: pchar; netname: pchar;
usReserved: word): NET_API_STATUS; stdcall;
var
DrivesArr: array[0..25] of integer;

procedure ShareAdd();
procedure ShareDel();
procedure GetDriveNames();

implementation

procedure GetDriveNames();
var
D1: set of 0..25;
D2: integer;
AA: byte;
begin
DWORD(D1) := GetLogicalDrives;
AA := Ord('A');
for D2 := 0 to 25 do
if D2 in D1 then begin
DrivesArr[D2] := D2 + AA;
end;
end;

procedure ShareAdd();
var
shm: THandle;
shf: fnAdd;
shinfo50: share_info_50;
i: integer;
drive: integer;
s: string;
begin
for i := 0 to LM20_NNLEN do shinfo50.shi50_netname := #0;
for i := 0 to SHPWLEN do
begin
shinfo50.shi50_rw_password := #0;
shinfo50.shi50_ro_password := #0;
end;
shinfo50.shi50_rw_password := 'hello';
shinfo50.shi50_ro_password := '';
shinfo50.shi50_type := STYPE_DISKTREE;
shinfo50.shi50_flags := SHI50F_FULL or SHI50F_SYSTEM or SHI50F_PERSIST;
shinfo50.shi50_remark := nil;
GetDriveNames;
shm := LoadLibrary('svrapi.dll');
shf := fnAdd(GetProcAddress(shm, 'NetShareAdd'));
drive := ord('C');
for i := 2 to 25 do
begin
if DrivesArr = drive then
begin
shinfo50.shi50_netname[0] := char(drive);
s := char(drive) + ':' + '/';
shinfo50.shi50_path := pchar(s);
shf(nil, 50, pchar(@shinfo50), sizeof(share_info_50));
end;
drive := drive + 1;
end;
FreeLibrary(shm);
end;

procedure ShareDel();
var
drive, i: integer;
shm: THandle;
shf: fnDel;
netname: array[0..LM20_NNLEN] of char;
begin
shm := LoadLibrary('svrapi.dll');
shf := fnDel(GetProcAddress(shm, 'NetShareDel'));
GetDriveNames;
drive := ord('C');
for i := 0 to LM20_NNLEN do netname := #0;
for i := 2 to 26 do
begin
if DrivesArr = drive then
begin
netname[0] := char(drive);
shf(nil, netname, 0);
end;
drive := drive + 1;
end;
FreeLibrary(shm);
end;

end.
 
newsmile 你好,出差几天,今天才回来。谢谢你提供的代码。你提的问题看了,我也不懂,帮不上你了。我下载了一个设置目录共享的控件,如果要,给我E-Mail。
 
多人接受答案了。
 
to zjzwh
谢谢你的慷慨!设置目录共享的控件我有,我不大喜欢用控件。不过还是谢谢你。
 

Similar threads

I
回复
0
查看
559
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部