Solved

Problem changing from Strings to Array

Posted on 2000-04-28
6
155 Views
Last Modified: 2010-04-04
I am getting  data from an Access database. Two Fields 'Servers' and 'Drives'.  I then want to Select the first Server and the FIRST drive Letter ( then I will go back and get the second drive Letter)  I want to take the Server Name and Drive letter to make a Network Connection.  The trouble I am having is when I try to add the strings to the array?  I then get the incompatable types sting and Pchar.  Any input on how to get the database information into the WNetAddConection?

Here is how I get the Information from the database
procedure TForm1.Button1Click(Sender: TObject);
var
   DriverArray : Array[0..8] of char;

begin
          if Table1.FieldByName ('drives').AsString<> '' then
          begin
          S := Table1.FieldByName ('ServerName').AsString+ ' ';
          D := Table1.FieldByName ('drives').AsString;
          DBServerList.Items.Add(S);
          {Move(D[1], DriverArray,Length(D));
          Memo1.Lines.Append(DriverArray[1]);}
          end;

//LOOP THRU the DRIVES

procedure TForm1.Button2Click(Sender: TObject);
var
   MyArray : array[0..10] of char;
   y : integer;
begin
     Move(D[1], MyArray, Length(D));
     y:= 0;
     WHile (MyArray[y] <> #0) do
           begin
               ListBox1.Items.Add(MyArray[y]);
               Memo1.Lines.Add(MyArray[y]);
               y:= y+1;
           end;

end;

procedure TForm1.Button3Click(Sender: TObject);
begin
     Table1.Next;
     Memo1.Lines.Clear;
end;


HERE IS WHERE THE PROBLEMS START!!!!

procedure TForm1.Button4Click(Sender: TObject);
var
   NR : TNetResource;
   NetArray : String;
   //NetArray : array [0..15] of char;
   x : integer;
begin
    // NetArray := '\\CEC-A\C$';
     NetArray := '\\' + S + '\' + D + '$';// NEEDS TO BE ENTERED LIKE THIS
     FillChar(NR, SizeOf(NR),0);
     NR.dwType := RESOURCETYPE_DISK;
     NR.lpLocalName := 'E:';
     NR.lpRemoteName := NetArray;
     WNetAddConnection2 (NR, nil, nil, 0);

end;


IF I assign the entire Servername and drive to the array the connections occur.  I am trying to get this so the connections and disconnects occur automaticly.  While the connections are made I get the Drive SIze and Free Space.
0
Comment
Question by:msmouse
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 10

Accepted Solution

by:
Lischke earned 50 total points
ID: 2758846
Hi,

you obviously have not yet understood the concept of String, PChar, array of char etc. so let me explain that a bit.

Usually Delhi stores its strings in the data type String which is nothing more than a piece of memory. A Delphi string is reference counted and always ends with a 0 byte to make it compatible to a PChar. So all you have to do to pass a PChar made from a String is to assign:

var
  ServerName: String;

NR.lpRemoteName := PChar(ServerName);

Using a string as intermediate storage frees you from taking care about memory allocation etc. Note: You can even mix variables and literals and still cast them to a PChar without any time penalty, like:

  NR.lpRemoteName := PChar(Server + '\D$');

Ciao, Mike
0
 
LVL 1

Author Comment

by:msmouse
ID: 2758918
You are correct with the my understanding of Pchar's.  This is my first program using Delphi so I have not yet figured out PChar's.  And I only have limited experence using strings and array's.  I will try your suggestion later, it's time for me to get out of here.

Thanks
0
 
LVL 1

Expert Comment

by:AttarSoftware
ID: 2759543
Or, you can use:

  StrPCopy( NetArray, '\\' + S + '\' + D + '$' ) ;

To do the same thing without casting...

Tim.
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 3

Expert Comment

by:bryan7
ID: 2763067
listenning
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2768600
msmouse, have you had success?
0
 
LVL 1

Author Comment

by:msmouse
ID: 2772519
StrPCopy also works, I'll have to decide which way will work the best.  Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

737 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