Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

HDD mapping in a LAN at runtime.

I need to map a hard disk from a PC into mine at runtime, example:
\\Server\c    ==> D: (in my macine)
Hou could I do it?
0
forns
Asked:
forns
  • 3
  • 3
1 Solution
 
BlackManCommented:
Here's a snip from one of my units... For more detailed info, you should check the Win32 help for WNetAddConnection2

unit LBSNet;

interface

Uses
  Winsock, Messages,
  Windows, WinSpool, Classes, SysUtils;

Var
  NetRes : PNetResource;


Function TNetConnector.ConnectDisk(LocalName, RemoteName : String) : Integer;
Begin
  With NetRes^ Do
  Begin
    dwType := RESOURCETYPE_DISK;
    lpLocalName := PChar(LocalName);
    lpRemoteName := PChar(RemoteName);
    lpProvider := '';
  End; (* With *)
  Result := WNetAddConnection2(FNetRes^, nil, nil, 0);
End; (* Procedure Connect *)


Begin
  ConnectDisk('D:', '\\server\c');
End.
0
 
fornsAuthor Commented:
hey, blackman! i tried that code in my lan with some modifications, but i have some problems:
i made a form with 2 textboxes, one for the remote machine "Server\c" and another one for the now local unit "D:". i put 2 buttons: one to connect and another one to disconnect.
the answer to OnClick is "ConnectDisk(txtLocal.Text, txtRemot.Text), so it calls your function. i modified your function declaring the PNetResource variable in it.

the problem:
ok, it works, but when i exit the program it launches an exception:
Exception EAccessViolation in module PROJECT1.EXE at 0000AEFE.
Acces violation at adress 0040AEFE in module 'PROJECT1.EXE'.
Read of adress 5C33312D.

i tried to declare the PNetResource variable as a global variable, but it launches:
Acces violation at adress 0042E505 in module 'PROJECT1.EXE'.
Read of adress FFFFFFFF.

could you plz tell me what's the problem?
thanx
0
 
fornsAuthor Commented:
hey, blackman! i tried that code in my lan with some modifications, but i have some problems:
i made a form with 2 textboxes, one for the remote machine "Server\c" and another one for the now local unit "D:". i put 2 buttons: one to connect and another one to disconnect.
the answer to OnClick is "ConnectDisk(txtLocal.Text, txtRemot.Text), so it calls your function. i modified your function declaring the PNetResource variable in it.

the problem:
ok, it works, but when i exit the program it launches an exception:
Exception EAccessViolation in module PROJECT1.EXE at 0000AEFE.
Acces violation at adress 0040AEFE in module 'PROJECT1.EXE'.
Read of adress 5C33312D.

i tried to declare the PNetResource variable as a global variable, but it launches:
Acces violation at adress 0042E505 in module 'PROJECT1.EXE'.
Read of adress FFFFFFFF.

could you plz tell me what's the problem?
thanx
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
BlackManCommented:
Hmm, I can see that, seems like I cut to hard from my unit :-)
I'll be home in about 3 hours, then I'll post the rest for you.. Sorry!
0
 
BlackManCommented:
I just found the mising link.. Just add a New(NetRes) to your code. Here's my test unit:
unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

Var
  NetRes : PNetResource;

Function ConnectDisk(LocalName, RemoteName : String) : Integer;
Begin
  With NetRes^ Do
  Begin
    dwType := RESOURCETYPE_DISK;
    lpLocalName := PChar(LocalName);
    lpRemoteName := PChar(RemoteName);
    lpProvider := '';
  End; (* With *)
  Result := WNetAddConnection2(NetRes^, nil, nil, 0);
End; (* Procedure Connect *)


procedure TForm1.Button1Click(Sender: TObject);
begin
  New(NetRes);
  ConnectDisk('y:', '\\netnews\spfile');
end;

end.
0
 
fornsAuthor Commented:
That's the way, man! Just what I was looking for!

Thanks a lot, BlackMan, it looks like that New() works as malloc() in C, doesn't it? And Dispose() as Free(). Cool.

It's been very usefull. See you!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now