◆◆◆100分求:如何编程实现将某个硬盘分区设置为共享????

onlydelphiandi 2003-08-22 03:44:31
要实现的功能:
1、可以将某个硬盘分区设置为共享。
2、可以随其他指定的程序一起运行(既其它程序启动时它也跟着启动)。

我想写这个程序的原因是,我在网吧上网,有时下载了一些东西,可是下次上网时机器却被别人占了。很烦。

请给出原代码,我很急!!

另外,有没有可以在局域网中直接访问其他电脑中未被设置共享的分区的软件?
...全文
191 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
firstshine 2003-08-27
  • 打赏
  • 举报
回复
既然这样,不一定非要采用共享的方法;这样吧,做一个服务程序,监听某个端口,收到访问后就可以访问你制定的资源了;然后再做一个客户端程序来访问他,你想干什么都可以
onlydelphiandi 2003-08-26
  • 打赏
  • 举报
回复
codez快帮我解决问题啊!
codez 2003-08-25
  • 打赏
  • 举报
回复
反正不叫 codez.

“网上无缘”- 这是当年泡mm的名字,懒的改了。
:)

我不是加了你吗?

onlydelphiandi 2003-08-24
  • 打赏
  • 举报
回复
codez(codez) 来了啊。你qq里叫什么名?
codez 2003-08-24
  • 打赏
  • 举报
回复
补充一下:

start->run->\\XXX.XXX.XXX.XXX\\c$
输入那台机器的密码,一般就可以打开他的机器了。
当然,这要求 nt 系统,以及用户使用缺省安全设置。
codez 2003-08-24
  • 打赏
  • 举报
回复
说实话,你要的东西可以针对某个网吧,或者某部分网吧可以。
因为一般网吧都装设保护系统。

想想怎么搞定他们,进入 ring0?利用他们的漏洞?

如果想达到你的目的,我认为最快的方法就是木马了~不是吗?
ipc$ 相关的函数,楼上几位给得差不多了。

小弟只能出点旁门左道了。呵呵!
OYHL 2003-08-24
  • 打赏
  • 举报
回复
#include
#include
#include

#define RTN_OK 0
#define RTN_USAGE 1
#define RTN_ERROR 13

//
// Note: UNICODE entry point and argv. This way, we don't need to bother
// with converting commandline args to Unicode
//

int
__cdecl
wmain(
int argc,
wchar_t *argv[]
)
{
LPWSTR DirectoryToShare;
LPWSTR Sharename;
LPWSTR Username;
LPWSTR Server;

PSID pSid = NULL;
DWORD cbSid;

WCHAR RefDomain[DNLEN + 1];
DWORD cchDomain = DNLEN + 1;
SID_NAME_USE peUse;

SECURITY_DESCRIPTOR sd;
PACL pDacl = NULL;
DWORD dwAclSize;

SHARE_INFO_502 si502;
NET_API_STATUS nas;

BOOL bSuccess = FALSE; // assume this function fails

if(argc < 4) {
printf("Usage: %ls [\\\\Server]\n", argv[0]);
printf(" directory is fullpath of directory to share\n");
printf(" sharename is name of share on server\n");
printf(" user/group is an WinNT user/groupname (REDMOND\\sfield, Administrators, etc)\n");
printf(" optional Server is the name of the computer to create the share on\n");
printf("\nExample: %ls c:\\public public Everyone\n", argv[0]);
printf("c:\\public shared as public granting Everyone full access\n");
printf("\nExample: %ls c:\\private cool$ REDMOND\\sfield \\\\WINBASE\n", argv[0]);
printf("c:\\private on \\\\WINBASE shared as cool$ (hidden) granting REDMOND\\sfield access\n");

return RTN_USAGE;
}

//
// since the commandline was Unicode, just provide pointers to
// the relevant items
//

DirectoryToShare = argv[1];
Sharename = argv[2];
Username = argv[3];

if( argc > 4 ) {
Server = argv[4];
} else {
Server = NULL; // local machine
}

//
// initial allocation attempt for Sid
//
#define SID_SIZE 96
cbSid = SID_SIZE;

pSid = (PSID)HeapAlloc(GetProcessHeap(), 0, cbSid);
if(pSid == NULL) {
printf("HeapAlloc error!\n");
return RTN_ERROR;
}

//
// get the Sid associated with the supplied user/group name
// force Unicode API since we always pass Unicode string
//

if(!LookupAccountNameW(
NULL, // default lookup logic
Username, // user/group of interest from commandline
pSid, // Sid buffer
&cbSid, // size of Sid
RefDomain, // Domain account found on (unused)
&cchDomain, // size of domain in chars
&peUse
)) {

//
// if the buffer wasn't large enough, try again
//

if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {

pSid = (PSID)HeapReAlloc(GetProcessHeap(), 0, pSid, cbSid);

if(pSid == NULL) {
printf("HeapReAlloc error!\n");
goto cleanup;
}

cchDomain = DNLEN + 1;

if(!LookupAccountNameW(
NULL, // default lookup logic
Username, // user/group of interest from commandline
pSid, // Sid buffer
&cbSid, // size of Sid
RefDomain, // Domain account found on (unused)
&cchDomain, // size of domain in chars
&peUse
)) {
printf("LookupAccountName error! (rc=%lu)\n", GetLastError());
goto cleanup;
}

} else {
printf("LookupAccountName error! (rc=%lu)\n", GetLastError());
goto cleanup;
}
}

//
// compute size of new acl
//

dwAclSize = sizeof(ACL) +
1 * ( sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) ) +
GetLengthSid(pSid) ;

//
// allocate storage for Acl
//

pDacl = (PACL)HeapAlloc(GetProcessHeap(), 0, dwAclSize);
if(pDacl == NULL) goto cleanup;

if(!InitializeAcl(pDacl, dwAclSize, ACL_REVISION))
goto cleanup;

//
// grant GENERIC_ALL (Full Control) access
//

if(!AddAccessAllowedAce(
pDacl,
ACL_REVISION,
GENERIC_ALL,
pSid
)) goto cleanup;

if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
goto cleanup;

if(!SetSecurityDescriptorDacl(&sd, TRUE, pDacl, FALSE)) {
fprintf(stderr, "SetSecurityDescriptorDacl error! (rc=%lu)\n",
GetLastError());
goto cleanup;
}

//
// setup share info structure
//

si502.shi502_netname = (LPTSTR) Sharename;
si502.shi502_type = STYPE_DISKTREE;
si502.shi502_remark = NULL;
si502.shi502_permissions = 0;
si502.shi502_max_uses = SHI_USES_UNLIMITED;
si502.shi502_current_uses = 0;
si502.shi502_path = (LPTSTR) DirectoryToShare;
si502.shi502_passwd = NULL;
si502.shi502_reserved = 0;
si502.shi502_security_descriptor = &sd;

nas = NetShareAdd(
(LPTSTR) Server, // share is on local machine
502, // info-level
(LPBYTE)&si502, // info-buffer
NULL // don't bother with parm
);

if(nas != NO_ERROR) {
printf("NetShareAdd error! (rc=%lu)\n", nas);
goto cleanup;
}

bSuccess = TRUE; // indicate success

cleanup:

//
// free allocated resources
//
if(pDacl != NULL)
HeapFree(GetProcessHeap(), 0, pDacl);

if(pSid != NULL)
HeapFree(GetProcessHeap(), 0, pSid);

if(!bSuccess) {
return RTN_ERROR;
}

return RTN_OK;
onlydelphiandi 2003-08-24
  • 打赏
  • 举报
回复
顶也谢谢了。
myadvice 2003-08-24
  • 打赏
  • 举报
回复
呵呵帮你顶。
onlydelphiandi 2003-08-24
  • 打赏
  • 举报
回复
◆◆◆我是楼主◆◆◆
这个程序要在网吧中运行,网吧中的任何机器默认是没有设置任何共享的。硬盘分为C D E三个分区,而且安装了还原精灵(只还原C分区),游戏安装在E盘。还安装有万象网管软件(但是允许下载、安装软件什么的)、诺顿杀毒软件等等。
我希望的效果是,当起动某些游戏时,这个程序跟着启动并悄悄的将C,D盘设置为完全共享。以方便我使用其他机器来访问这台机器。

修改注册表的方法我试验了,遗憾的是,只有在重启或注销后共享才生效,但是这样会使人起疑。见下面:

NetShareAdd这个函数我使用了,可是没有成功,不知道错在哪?见下面:
-------------------------------------------------------------------
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const NETNAME_LEN = 13; PASSWORD_LEN = 9; SHI50F_RDONLY = $0001;
var
Form1: TForm1;
function NetShareAdd(servername: PChar; level: SmallInt; buf: Pointer;
buf_len: SmallInt): SmallInt; far; stdcall; external 'svrapi.dll';

implementation

{$R *.dfm}
type
SHARE_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;

procedure TForm1.Button1Click(Sender: TObject); //在nt内核下才好使,我要的是在win98下的方法。
begin
WinExec('net.exe share MyShare=c:', SW_HIDE)
end;

procedure TForm1.Button2Click(Sender: TObject); //采用NetShareAdd函数来启用共享,没有成功,错在哪呢?请解决!
var buf: pointer;
k: ^SHARE_INFO_50;
begin
new(k);
k^.netname := 'C:';
k^.path := 'C:';
k^.flags := 258;
k^.remark := '';
buf := k;
NetShareAdd('', 50, buf, 0);
end;

procedure TForm1.Button3Click(Sender: TObject); //采用修改注册表的方法来启用共享,但是只有重启或注销后共享才生效,这不是我想要的。为什么在属性中修改共享属性后会立刻生效呢?妈的?晕!
var
MyReg: TRegistry;
j: pointer;
//以下为注册表修改
begin
j := nil;
MyReg := TRegistry.Create;
MyReg.RootKey := HKEY_LOCAL_MACHINE;
MyReg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Network\LanMan\C', True);
MyReg.WriteInteger('Flags', 258); //完全共享
myreg.WriteBinaryData('Parm1enc', j, 0);
myreg.WriteBinaryData('Parm2enc', j, 0);
MyReg.WriteInteger('Type', 0);
MyReg.WriteString('Path', 'C\:'); //共享
MyReg.WriteString('Remark', '');
MyReg.CloseKey;
end;

end.
-------------------------------------------------------------------
在使用NetShareAdd函数的时候感觉很困扰:函数TMD要先声明,要改为Delphi格式,SHARE_INFO_50结构体类型要按照C++描述的改为Delphi格式的再声明一次,妈的,怎么这么麻烦呢!要是没有超级猛料我还真不知道怎么个改法,比如说那些常量,我他妈的上哪查去啊!
onlydelphiandi 2003-08-22
  • 打赏
  • 举报
回复
◆◆◆我是楼主:
网吧里安装有还原精灵和万象网管什么的,可能会禁止修改注册表!!
firstshine 2003-08-22
  • 打赏
  • 举报
回复
转贴一篇文章,看看对你有用吗?

uses Registry
var

MyReg:TRegistry;

//以下为注册表修改

MyReg:=TRegistry.Create ;

MyReg.RootKey :=HKEY_LOCAL_MACHINE;MyReg.OpenKey ('',True)

begin

MyReg.WriteInteger('Flags',258); //共享为完全共享

MyReg.WriteInteger('Type',0);

MyReg.WriteString('Path','C:);//共享

MyReg.WriteString('Remark','');

MyReg.CloseKey ;

end;

见笑了

***************

DELPHI 关于WIN9X下共享文件夹问题

作者:房客(Jason)

经常看到有人问起如何在程序中增加和删除共享文件夹,于是就尽可能把相关信息写出来,更深入答案还请大虾共同讨论。

共享/删除共享可以直接调用标准的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';

这里再提供两个关于连接共享目录/撤销共享目录函数WnetAddConnection和WnetCancelConnection。

关于映射网络驱动器的方法如下:

WNetAddConnection(%共享目录名%,%口令%,'X:'); //映射X盘

WNetCancelConnection('X:',True); //撤销X盘映射

关于建立/撤消共享的函数返回变量说明如下:

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); //NetShareAdd返回错误

NERR_NetNotStarted = (NERR_BASE+2);

NERR_ServerNotStarted = (NERR_BASE+14);

NERR_NetNameNotFound = (NERR_BASE+210);

NERR_ShareNotFound = (NERR_BASE+292); //NetShareDel返回错误

SHARE_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; //以上为类型定义

再有就是关于Window9X共享目录口令问题(其实屏保口令也是同样保存的),口令(原始为十六进制字符)在注册表位置:HKEY_LOCAL_MACHINE_version\u20849共享文件夹名enc和Parm2enc两位置,Parm1enc为对应的是完全共享密码,Parm2enc对应的是只读共享密码。字符与数列(前八个数是35,9A,4D,A6,53,A9,D4,6A)作异或运算即得密码的二进制ASCII码,转换后可得到密码。

另外提供一个比较幼稚但可行的方案(在注册表做动作):

var reg : TRegistry; name : String; //s是在网上邻居里的文件夹名

begin

name := 'DirName';//建立一个只读,无密码共享目录

reg := TRegistry.Create;

reg.RootKey := HKEY_LOCAL_MACHINE;

reg.OpenKey('Software+name,true );

reg.WriteInteger( 'Flags', 401 ) ; //权限:401代表为访问,258为完全访问,259为密码访问

reg.WriteInteger( 'Parm1enc', 0 ) ; //参数2,放置完全访问密码,已加密

reg.WriteInteger( 'Parm2enc', 0 ) ; //参数2,放置只读访问密码,已加密

reg.WriteString( 'Path', 'C:' ) ; //放置要共享的目录的绝对路径

reg.WriteString( 'Remark', '' ) ;

reg.WriteInteger( 'Type', 0 ) ;

end;

这样的缺点就是要重启系统,可以ExitWindowsEx( EWX_FORCE and EWX_SHUTDOWN , 0 )来重启。

此外你还可以使用控件File Sharing 95/98,该控件包含了几个方便的function:ShareResource、DeleteShare、GetShareInfo、SetShareInfo、GetNetErrorString等。

附:关于NetUserAdd等相关API

WNetCloseEnum 结束网络资源列表

WNetConnectionDialog 开始网络连接对话框

WNetDisconnectDialog 断开网络对话框

WNetEnumResource 继续列表网络资源

WNetGetConnection 获取网络资源名

WNetGetLastError 返回网络函数最近错误

WNetGetUser 获取当前网络用户名

WNetOpenEnum 列出网络资源

 

lxpbuaa 2003-08-22
  • 打赏
  • 举报
回复
http://www.csdn.net/develop/article/15/15822.shtm

————————————————————————————————————
宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
————————————————————————————————————

5,379

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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