用NetShareAdd增加本机的网络共享问题???

saoren 2000-10-13 06:09:00
各位大虾:
小弟在用WinAPI:NetShareAdd,想用它来增加本机的一个共享资源,但是在Delphi中并不直接支持它的使用,必须由自己定义说明,它是在哪个DLL,由于它是C++原形,所以调用时不知所以。望各位大虾指点,多谢。
NetShareDel却是能用,就是它不好用。


...全文
463 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
black_fox 2001-08-28
  • 打赏
  • 举报
回复
98最好用Share_INFO_2
NT最好用SHARE_INFO_502
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;


procedure Button1Click(Sender: TObject);

end;

Share_INFO_2= record
shi2_netname:PWideChar;
shi2_type:DWORD;
shi2_remark:LPTSTR;
shi2_permissions:DWORD;
shi2_max_uses:DWORD;
shi2_current_uses:DWORD;
shi2_path:PWideChar;
shi2_passwd:LPTSTR;
end;
PShare_INFO_2 = ^Share_INFO_2;
LPShare_INFO_2 = ^Share_INFO_2;

SHARE_INFO_502 =record
shi502_netname: PCHAR;
shi502_type: DWORD;
shi502_remark: PCHAR;
shi502_permissions: DWORD;
shi502_max_uses: DWORD;
shi502_current_uses: DWORD;
shi502_path: PCHAR;
shi502_passwd: PCHAR;
shi502_reserved: DWORD;
shi502_security_descriptor: PSECURITY_DESCRIPTOR;
end;
PSHARE_INFO_502= ^SHARE_INFO_502;
LPSHARE_INFO_502=^SHARE_INFO_502;


const

STYPE_DISKTREE = $0001;
ACCESS_READ = $0001;

var
Form1: TForm1;
F:Cardinal;
function NetShareAdd(
Server : PwideChar;
level : cardinal;
Buf : Pointer;
var Parm_Err : DWORD):Cardinal;stdcall; external 'netapi32.dll' name 'NetShareAdd';

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
buf:Pointer;
UserInf:Share_INFO_2;
UserInf502:Share_INFO_502;
err:dWord;
begin

err:=0;
f:=0;
UserInf.shi2_netname:='test';
UserInf.shi2_type:=0;
UserInf.shi2_remark:='';
UserInf.shi2_permissions:=0;
UserInf.shi2_max_uses:= 0;
UserInf.shi2_current_uses:=0;
UserInf.shi2_path:='C:\';
UserInf.shi2_passwd:=nil;
GetMem(Buf ,sizeof(UserInf));
F:=NetSHAREAdd(nil,2,@UserInf,err);
FreeMem(Buf);


end;

end.
sunsetyang 2000-10-14
  • 打赏
  • 举报
回复
NetShareAdd
The NetShareAdd function shares a server resource.

Security Requirements
Only members of the Administrators or Account Operators local group or those with Communication, Print, or Server operator group membership can successfully execute the NetShareAdd function. The Print operator can add only Printer queues. The Communication operator can add only communication-device queues.

Windows NT/2000: The parameter order is as follows.

NET_API_STATUS NetShareAdd(
LPWSTR servername,
DWORD level,
LPBYTE buf,
LPDWORD parm_err
);
Windows 95/98: You must specify the size of the information buffer, in bytes, using the cbBuffer parameter. The Windows NT/Windows 2000 parm_err parameter is not available on this platform. Therefore, the parameter list is as follows.

extern API_FUNCTION
NetShareAdd(
const char FAR * pszServer,
short sLevel,
const char FAR * pbBuffer,
unsigned short cbBuffer
);
Parameters
servername
[in] Pointer to a Unicode (Windows NT/2000) or ANSI (Windows 95/98) string specifying the name of the remote server on which the function is to execute. The string must begin with \\. If this parameter is NULL, the local computer is used.
level
[in] Specifies the information level of the data. This parameter can be one of the following values.
Windows NT/2000: The following levels are valid. Value Meaning
2 Specifies information about the shared resource, including name of the resource, type and permissions, and number of connections. The buf parameter points to a SHARE_INFO_2 structure.
502 Specifies information about the shared resource, including name of the resource, type and permissions, number of connections, and other pertinent information. The buf parameter points to a SHARE_INFO_502 structure.



Windows 95/98: The following level is valid. Value Meaning
50 Specifies information about the shared resource, including the name and type of the resource, a comment associated with the resource, and passwords. The pbBuffer parameter points to a share_info_50 structure. Note that the string you specify in the shi50_path member must contain only uppercase characters. If the path contains lowercase characters, calls to NetShareAdd can fail with NERR_UnknownDevDir or ERROR_BAD_NET_NAME.



buf
[in] Pointer to the buffer that specifies the data. The format of this data depends on the value of the level parameter.
parm_err
[out] Pointer to a DWORD value that receives the index of the first member of the share information structure that causes the ERROR_INVALID_PARAMETER error. If this parameter is NULL, the index is not returned on error. For more information, see the NetShareSetInfo function.
Return Values
If the function succeeds, the return value is NERR_Success.

If the function fails, the return value can be one of the following error codes.

Value Meaning
ERROR_ACCESS_DENIED The user does not have access to the requested information.
ERROR_INVALID_LEVEL The value specified for the level parameter is invalid.
ERROR_INVALID_NAME The character or file system name is invalid.
ERROR_INVALID_PARAMETER The specified parameter is invalid.
NERR_DuplicateShare The share name is already in use on this server.
NERR_RedirectedPath The operation is invalid for a redirected resource. The specified device name is assigned to a shared resource.
NERR_UnknownDevDir The device or directory does not exist.


Remarks
Windows 95/98: See the NetShareAdd Sample (Windows 95/98) topic to view a code sample that demonstrates how to use the NetShareAdd function.

Windows NT/2000: The following code sample demonstrates how to share a network resource using a call to the NetShareAdd function. The code sample fills in the members of the SHARE_INFO_2 structure and calls NetShareAdd, specifying information level 2.

#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;
}

If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same functionality you can achieve by calling the network management share functions. For more information, see IADsFileShare.

Requirements
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Header: Declared in Lmshare.h (Windows NT/2000) or Svrapi.h (Windows 95/98); include Lm.h (Windows NT/2000).
Library: Use Netapi32.lib (Windows NT/2000) or Svrapi.lib (Windows 95/98).

Lionheart 2000-10-14
  • 打赏
  • 举报
回复
The set of ported LAN Manager functions supported by Microsoft?Windows?is based on the set of LAN Manager functions specified in the LAN Manager 2.x Programmer's Reference. They are not a part of the Microsoft?Win32?application programming interface (API).
The NetShareDel and NetShareAdd are Lan Manager functions.

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧