I thought converting a mapped drive letter to a UNC path would be enough to be able to open a .GDB file, but alas:
function ConvertToUNCPath(AMappedDrive: string) : string; var lRemoteString : array[0..255] of char; lpRemote : PChar; lStringLen : Cardinal; begin lpRemote := @lRemoteString; lStringLen := 255; If WNetGetConnection(Pchar(ExtractFileDrive(AMappedDrive)) , lpRemote, lStringLen) = NO_ERROR Then Result := lRemoteString else Result := ''; // No mapping found end; function TDataModuleData.OpenGDBDatabase(AGDBName: string) : Boolean; var lDlgLogin: TFrmLogin; p : Integer; lUNC, lErrMsg : String; begin Result := False; with FDConnection do // TFDConnection begin Close; TxOptions.Isolation := xiDirtyRead; p := Pos(':',AGDBName); if p = 2 then begin lUNC := ConvertToUNCPath(Copy(AGDBName,1,2)); if lUNC '' then begin lUNC := Copy(lUNC,3); p := pos('\',lUNC); AGDBName := Copy(lUNC,p) + Copy(AGDBName,3); lUNC := copy(lUNC,1,p-1); end; end; DriverName := S_FD_IBId; Params.Database := AGDBName; if lUNC '' then Params.Add('Server=' + lUNC) else Params.Add('Server=localhost'); // Not strictly necessary Params.UserName := 'SYSDBA'; Params.Password := 'masterkey'; try Open; Result := Connected; except on E:Exception do begin lErrMsg := LowerCase(E.Message); end; end; end; end;
Depending on how I parse the ConvertToUNCPath
result I get different error messages:
[firedac][phys][ib]unavailable database
[firedac][phys][ib]i/o error during "createfile (open)" operation for file "persoonlijk\jan\klanten.gdb"'#$D#$A'error while trying to open file'#$D#$A'the system cannot find the path specified.
The part of the code using ConvertToUNCPath
succesfully converts e.g. P:\Jan\KLANTEN.GDB
to \\tt2012server\persoonlijk\Jan\KLANTEN.GDB
.
How can I open a GDB file when the path points to a mapped drive letter?
Added: I tried these hardcoded variations, they all fail:
// lUNC := '\\2012server'; // Unable to complete network request to host lUNC := 'tt2012server'; //AGDBName := '\\tt2012server\persoonlijk\jan\klanten.gdb'; //AGDBName := 'tt2012server\persoonlijk\jan\klanten.gdb'; //AGDBName := '\persoonlijk\jan\klanten.gdb'; //AGDBName := 'persoonlijk\jan\klanten.gdb'; //AGDBName := '\jan\klanten.gdb'; //AGDBName := 'jan\klanten.gdb'; //AGDBName := 'p:\jan\klanten.gdb'; (original input)
(P: maps to \\tt2012server\persoonlijk
)
Added:
Sorry, I was not clear in my initial text: this is not about connecting to a database on a remote server per se. I just want my local 'DB inspection' tool to be able to open a GDB file if someone places it in my network share for inspection (instead of having to copy it to local disk first).
To only intention of using WNetGetConnection was to resolve drive letter to UNC path (some I code I found on the web).