Solved

Problem changing from Strings to Array

Posted on 2000-04-28
6
154 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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