P E Schoen
2016-08-27 09:39:10 UTC
This is probably a very basic question, but I may have been improperly
casting a string to PChar in some cases. For instance, I got the following
code to work as shown:
====================================================
procedure TfmOrtDatabase.btBackupClick(Sender: TObject);
var udfn: OrtData.TUserDataFile;
S, dbFileName, saveFileName, dirName: String;
// currDir: String;
currDir: array[0..50] of Char;
begin
if Application.MessageBox( 'Backup Database Files?',
'Backup', MB_YESNO ) = ID_YES then begin //1
// currDir := '01234567890123456789012345678901234567890123456789';
// SetLength(currDir,50);
GetCurrentDirectory(sizeof(currDir)+1, @currDir);
dirName := GetSystemFolder(CSIDL_PERSONAL);
dirName := dirName + '\Ortmaster';
SelectDirectory(dirName, [sdAllowCreate,sdPerformCreate], 0);
// SelectDirectory('Select Directory', dirName, dirName);
S := FormatDateTime('yyyymmdd', Now);
fmDBSaveDialog.InitialDir := dirName;
for udfn := udfCust to udfTypes do begin //2
dbFileName := ProgramDataFolder + '\' +
OrtData.UserFileName[udfn]+'.dbf';
fmDBSaveDialog.FileName := OrtData.UserFileName[udfn]+'.dbf';
if not (fmDBSaveDialog.Execute) then exit;
if (FileExists(dbFileName)) then begin //3
saveFileName := fmDBSaveDialog.FileName + '-' + S;
if not( CopyFile( pChar(dbFileName), PChar(saveFileName), FALSE ) )
then
MessageDlg('File Copy Error', mtInformation, [mbOK], 0);;
end; //-3
end; //-2
fmDBSaveDialog.InitialDir := dirName;
dbFileName := 'Ortmaster' + '.rcf';
fmDBSaveDialog.FileName := dbFileName;
dbFileName := ProgramDataFolder + '\Ortmaster' + '.rcf';
if not (fmDBSaveDialog.Execute) then exit;
if (FileExists(dbFileName)) then begin //2
saveFileName := fmDBSaveDialog.FileName + '-' + S;
if not( CopyFile( pChar(dbFileName), pChar(saveFileName), FALSE ) )
then
MessageDlg('File Copy Error', mtInformation, [mbOK], 0);;
end; //-2
SetCurrentDirectory(currDir);
end; //-1
end;
=========================================================
If I used an uninitialized string, the GetCurrentDirectory() function
failed, even when I initialized the string to a constant as shown. It's
rather late, and maybe my brain is fogged, but it seems like there should be
a better way to do this. It would be nice if there were a function like
GetSystemFolder() that returns a string.
BTW, the SelectDirectory() function seems to be what I was searching for in
a post from 7/20/15.
Also, I'm not sure why the current directory got changed by the function.
Thanks,
Paul
casting a string to PChar in some cases. For instance, I got the following
code to work as shown:
====================================================
procedure TfmOrtDatabase.btBackupClick(Sender: TObject);
var udfn: OrtData.TUserDataFile;
S, dbFileName, saveFileName, dirName: String;
// currDir: String;
currDir: array[0..50] of Char;
begin
if Application.MessageBox( 'Backup Database Files?',
'Backup', MB_YESNO ) = ID_YES then begin //1
// currDir := '01234567890123456789012345678901234567890123456789';
// SetLength(currDir,50);
GetCurrentDirectory(sizeof(currDir)+1, @currDir);
dirName := GetSystemFolder(CSIDL_PERSONAL);
dirName := dirName + '\Ortmaster';
SelectDirectory(dirName, [sdAllowCreate,sdPerformCreate], 0);
// SelectDirectory('Select Directory', dirName, dirName);
S := FormatDateTime('yyyymmdd', Now);
fmDBSaveDialog.InitialDir := dirName;
for udfn := udfCust to udfTypes do begin //2
dbFileName := ProgramDataFolder + '\' +
OrtData.UserFileName[udfn]+'.dbf';
fmDBSaveDialog.FileName := OrtData.UserFileName[udfn]+'.dbf';
if not (fmDBSaveDialog.Execute) then exit;
if (FileExists(dbFileName)) then begin //3
saveFileName := fmDBSaveDialog.FileName + '-' + S;
if not( CopyFile( pChar(dbFileName), PChar(saveFileName), FALSE ) )
then
MessageDlg('File Copy Error', mtInformation, [mbOK], 0);;
end; //-3
end; //-2
fmDBSaveDialog.InitialDir := dirName;
dbFileName := 'Ortmaster' + '.rcf';
fmDBSaveDialog.FileName := dbFileName;
dbFileName := ProgramDataFolder + '\Ortmaster' + '.rcf';
if not (fmDBSaveDialog.Execute) then exit;
if (FileExists(dbFileName)) then begin //2
saveFileName := fmDBSaveDialog.FileName + '-' + S;
if not( CopyFile( pChar(dbFileName), pChar(saveFileName), FALSE ) )
then
MessageDlg('File Copy Error', mtInformation, [mbOK], 0);;
end; //-2
SetCurrentDirectory(currDir);
end; //-1
end;
=========================================================
If I used an uninitialized string, the GetCurrentDirectory() function
failed, even when I initialized the string to a constant as shown. It's
rather late, and maybe my brain is fogged, but it seems like there should be
a better way to do this. It would be nice if there were a function like
GetSystemFolder() that returns a string.
BTW, the SelectDirectory() function seems to be what I was searching for in
a post from 7/20/15.
Also, I'm not sure why the current directory got changed by the function.
Thanks,
Paul