Solved

Problem changing from Strings to Array

Posted on 2000-04-28
6
153 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…

809 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