Solved

HDD mapping in a LAN at runtime.

Posted on 1998-11-29
6
253 Views
Last Modified: 2010-08-05
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
Comment
Question by:forns
  • 3
  • 3
6 Comments
 
LVL 7

Accepted Solution

by:
BlackMan earned 50 total points
ID: 1348560
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
 

Author Comment

by:forns
ID: 1348561
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
 

Author Comment

by:forns
ID: 1348562
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:BlackMan
ID: 1348563
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
 
LVL 7

Expert Comment

by:BlackMan
ID: 1348564
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
 

Author Comment

by:forns
ID: 1348565
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
code issue 8 179
creating threads in delphi 1 157
JAudiorecorder record freezing the app 29 82
scroll down TListBox component in Delphi 1 30
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question