Question regarding the Symbollink Unit created by Rlibby for the virtual drive manager

Hi

Im working with the virtual drive manager, for the most part created by and maintained by the symbollink unit, and I am trying to prevent the following situation, since this situation does not seem logical or of any use, but can be done

Someone can add a new drive, which points to a virtual drive, this does not seem to cause problems, but I cannot find any use for this, so Im trying to prevent this from being done, by comparing the new drive letter to the strings which represent the present and current virtual drives

procedure TMainForm.btnAddClick(Sender: TObject);
begin

  AddForm.Startup;
  if (AddForm.ShowModal = mrOK) and (AddForm.Drive > #0) then
  begin
   
>>>if pos(SymbolicLinks[AddForm.Drive], SymbolicLinks[cIndex].DriveLetter) = 0 then
{where the first is the new drive we are trying to add, and the second should be the list of the present virtual drives}

     SymbolicLinks[AddForm.Drive].DevicePath:=AddForm.txtPath.Text;
     SymbolicLinks[AddForm.Drive].Persist(True);
  end;
  LoadVirtualDrives;

end;

I have looked through the symbollink unit, but really cannot make much sense of it, at least not so much that i can perform the comparison, the above code fails and i know why, im comparing different types, plus i need to declare a variable [i think?] but since i even do not know if im comparing the right things i got stuck here

Frank
LVL 1
fdehellAsked:
Who is Participating?
 
Russell LibbyConnect With a Mentor Software Engineer, Advisory Commented:
Frank,
You can also update your version of the SymbolicLink unit to include the 2 following utility functions (should make your life easier).

---

// Insert after the TSymbolicLinks declaration

////////////////////////////////////////////////////////////////////////////////
//   Utility functions
////////////////////////////////////////////////////////////////////////////////
function   IsVirtualPath(PathName: String): Boolean;
function   GetPhysicalPath(PathName: String): String;

////////////////////////////////////////////////////////////////////////////////
//   Global variable
////////////////////////////////////////////////////////////////////////////////
var
  SymbolicLinks:    TSymbolicLinks;

implementation

function GetPhysicalPath(PathName: String): String;
var  szPath:        String;
     cDrive:        Char;
begin

  // Defaul result return
  result:=PathName;

  // Check virtual
  if (Length(PathName) > 0) and IsVirtualPath(PathName) then
  begin
     // Save path to local variable
     szPath:=PathName;
     // Recurse the mappings
     while (Length(szPath) > 0) and (szPath[1] in  ['A'..'Z', 'a'..'z']) do
     begin
        // Get drive letter
        cDrive:=szPath[1];
        // Check drive mapping
        if SymbolicLinks[cDrive].IsPath then
        begin
           // Get device path
           szPath:=SymbolicLinks[cDrive].DevicePath;
           // Delete the X: in the result with the actual path
           Delete(result, 1, 2);
           // Update result
           if (Length(result) = 0) then
              result:=ExcludeTrailingBackslash(szPath)
           else if (result[1] = '\') then
              result:=ExcludeTrailingBackslash(szPath)+result
           else
              result:=IncludeTrailingBackslash(szPath)+result;
        end
        else
           // No more paths to recurse
           SetLength(szPath, 0);
     end;
     // Update result so it looks correct
     result:=ExcludeTrailingBackslash(result);
  end;

end;

function IsVirtualPath(PathName: String): Boolean;
begin

  // Check file path
  if (Length(PathName) > 0) and ((GetFileAttributes(PChar(PathName)) and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY) then
  begin
     // Now extract the drive letter from the path
     if (PathName[1] in  ['A'..'Z', 'a'..'z']) then
        // Determine if there is a virtual path for the drive
        result:=SymbolicLinks[PathName[1]].IsPath
     else
        // Not a valid dive letter
        result:=False;
  end
  else
     // Not a valid path
     result:=False;

end;

The function IsVirtualPath takes a path and will tell you if it is a "virtual" path. The function GetPhysicalPath takes a path name and returns the physical path location for the passed virtual path. It is also able to iterate ALL mappings, take for example:

  // Create a bunch of links to "c:\windows" and below
  SymbolicLinks['p'].DevicePath:='c:\windows';
  SymbolicLinks['q'].DevicePath:='p:\system32';
  SymbolicLinks['r'].DevicePath:='q:\drivers';

  // Test that path to see if virtual
  if IsVirtualPath('r:\') then
  begin
     // Show message
     ShowMessage('Path is virtual');
     // Show real path
     ShowMessage(GetPhysicalPath('r:\'));
  end;

  // Get rid of mappings
  SymbolicLinks['p'].DevicePath:='';
  SymbolicLinks['q'].DevicePath:='';
  SymbolicLinks['r'].DevicePath:='';

This way, you can decide how you want to handle a case where a new virtual drive is being added, and the user is passing a path on another virtual drive.

eg

- Check IsVirtualPath and disallow the add
- Get the "real" path using GetPhysicalPath and allow the add (mapping it the real path)

-----

Hope this helps some,
Russell




0
 
Russell LibbySoftware Engineer, Advisory Commented:
Frank,

You just need to check the new path being added to get the drive letter, eg:

path = "X:\system32" // eg X was mapped to "c:\windows"

drive would be "X". And check to see if that drive maps to a path by using IsPath, eg:

if SymbolicLinks['X'].IsPath then
 // Dont allow a mapping to it

If IsPath is false, then its a real device or it hasn't been mapped. If you need an example let me know.

Regards,
Russell


 
0
 
fdehellAuthor Commented:
Hey Russel perfect, I wonder why Borland has not hired you yet for writing their helpfiles lol, they could use your help Im sure of that haha

Thanks...


Frank
0
 
fdehellAuthor Commented:
For completeness here is the procedure after I had altered the symbollink unit like Russel told me

procedure TMainForm.btnAddClick(Sender: TObject);
begin

  AddForm.Startup;
  if (AddForm.ShowModal = mrOK) and (AddForm.Drive > #0) then
    begin
      if IsVirtualPath(AddForm.txtPath.Text) then
      ShowMessage('There is little use pointing a virtual drive to a virtual drive') end
      else begin
    SymbolicLinks[AddForm.Drive].DevicePath:=AddForm.txtPath.Text;
     SymbolicLinks[AddForm.Drive].Persist(True);
     end;
 
  LoadVirtualDrives;

end;

again, thank you sooo much Russel
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.