可能该函数一开始在Win98中有bug,不能按老的MSDN文档或Delphi在线文档
中的说明做。微软的最新文档用的是 SHARE_INFO_50 结构,在 win2000中
仍用 SHARE_INFO_2。以下是微软的 C声明和示例:
98x
typedef struct _share_info_50
{
char shi50_netname[LM20_NNLEN+1];
unsigned char shi50_type;
unsigned short shi50_flags;
char FAR* shi50_remark;
char FAR* shi50_path;
char shi50_rw_password[SHPWLEN+1];
char shi50_ro_password[SHPWLEN+1];
} _share_info_50;
#include <stdio.h>
#include <windows.h>
#include <svrapi.h>
int main(int argc, char FAR * argv[])
{
char FAR * pszServerName = NULL;
short nLevel = 50;
struct share_info_50* pBuf = NULL;
unsigned short cbBuffer;
NET_API_STATUS nStatus;
//
// ServerName can be NULL to indicate the local computer.
//
if ((argc < 3) || (argc > 4))
{
printf("Usage: %s [////ServerName] ShareName SharePath/n", argv[0]);
exit(1);
}
if (argc == 4)
pszServerName = argv[1];
//
// Allocate the memory required to specify a
// share_info_50 structure.
//
cbBuffer = sizeof(struct share_info_50);
pBuf = malloc(cbBuffer);
if (pBuf == NULL)
printf("No memory/n");
//
// Assign values to the share_info_50 structure.
//
strcpy(pBuf->shi50_netname, argv[argc-2]);
pBuf->shi50_type = STYPE_DISKTREE;
pBuf->shi50_flags = SHI50F_FULL;
pBuf->shi50_remark = NULL;
pBuf->shi50_path = argv[argc-1];
pBuf->shi50_rw_password[0] = '/0';
// No password
pBuf->shi50_ro_password[0] = '/0';
// No password
//
// Call the NetShareAdd function
// specifying information level 50.
//
nStatus = NetShareAdd(pszServerName,
nLevel,
(char FAR *)pBuf,
cbBuffer);
//
// Display the result of the function call.
//
if (nStatus == NERR_Success)
printf("Share added successfully/n");
else
fprintf(stderr, "A system error has occurred: %d/n", nStatus);
//
// Free the allocated memory.
//
if (pBuf != NULL)
free(pBuf);
return 0;
}
2K
#define UNICODE
#include <windows.h>
#include <stdio.h>
#include <lm.h>
void wmain( int argc, TCHAR *argv[ ])
{
NET_API_STATUS res;
SHARE_INFO_2 p;
DWORD parm_err = 0;
if(argc<2)
printf("Usage: NetShareAdd server/n");
else
{
//
// Fill in the SHARE_INFO_2 structure.
//
p.shi2_netname = TEXT("TESTSHARE");
p.shi2_type = STYPE_DISKTREE;
// disk drive
p.shi2_remark = TEXT("TESTSHARE to test NetShareAdd");
p.shi2_permissions = 0;
p.shi2_max_uses = 4;
p.shi2_current_uses = 0;
p.shi2_path = TEXT("C://");
p.shi2_passwd = NULL;
// no password
//
// Call the NetShareAdd function,
// specifying level 2.
//
res=NetShareAdd(argv[1], 2, (LPBYTE) &p, &parm_err);
//
// If the call succeeds, inform the user.
//
if(res==0)
printf("Share created./n");
// Otherwise, print an error,
// and identify the parameter in error.
//
else
printf("Error: %u/tparmerr=%u/n", res, parm_err);
}
return;
}
在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
Char;
path
Char;
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
Dword) : 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
Word;
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.