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:
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.
GetCurrentDirectory() arguments are: the buffer pointer, then the buffer length. Not the buffer length, then the buffer pointer.
And, currDir variable was declarated as "array[0..50] of Char" which means that it has a storage of 51 bytes plus one byte alignment padding (a total
of 52 bytes). The "sizeof(currDir)+1" expression would translate to "51+1" which is 52 bytes. You're lucky you've declared currDir variable as an odd length Char array. If you've declared it as an array with a length of multiple of 4, there would be no padding following its storage. The data following its storage might be used by other variable. So, if you use "sizeof(currDir)+1" you may overwrite one byte which follows the actual
array - a byte outside of the array.
But I would still like to learn more about strings and PChar.
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);
But I would still like to learn more about strings and PChar.
"JJ" wrote in message news:1rco3b1rvcu6.1k5cho7qohwwc$.dlg@40tude.net...
GetCurrentDirectory() arguments are: the buffer pointer, then the buffer
length. Not the buffer length, then the buffer pointer.
From the Delphi help:
DWORD GetCurrentDirectory(
DWORD nBufferLength, // size, in characters, of directory buffer
LPTSTR lpBuffer // address of buffer for current directory
);
Also, perhaps I could use the return value of the function with a buffer
size of zero. It would return the size needed, and then I could use alloc() to create the memory space to use. I just don't know what is the "best" way to handle functions like this, and I may have been using the PChar() cast improperly.
String...
String type is a combination between ShortString and PChar (null terminated string). The String storage layout is like ShortString except that the
string length is 16-bit instead of 8-bit. The data for "ABC" would be like below.
03,00,41,42,43,00
i.e.
(*
StringStorage = record
StringLength : Word;
StringData : Array [1..n] of Char;
end;
Where n is variable. i.e. variable field size
*)
"JJ" wrote in message news:1rco3b1rvcu6.1k5cho7qohwwc$.dlg@40tude.net...
[snip]
GetCurrentDirectory() arguments are: the buffer pointer, then the
buffer length. Not the buffer length, then the buffer pointer.
From the Delphi help:
DWORD GetCurrentDirectory(
DWORD nBufferLength, // size, in characters, of directory buffer
LPTSTR lpBuffer // address of buffer for current directory
);
Also, perhaps I could use the return value of the function with a buffer
size of zero. It would return the size needed, and then I could use
alloc() to create the memory space to use. I just don't know what is the "best" way to handle functions like this, and I may have been using the PChar() cast improperly.
P E Schoen schrieb:
From the Delphi help:
DWORD GetCurrentDirectory(
DWORD nBufferLength, // size, in characters, of directory buffer
LPTSTR lpBuffer // address of buffer for current directory
According to MSDN I'd think that the length comes first.
Most API functions, dealing with variable amount of data, return the
required size when called with a Nil pointer. MSDN also claims that the *required* size is returned by GetCurrentDirectory, if the string doesn't
fit into the buffer, but I'd not rely on that. At least the call succeeded
if the returned (actual) size is lower than the allocated size.
All non-empty dynamic Strings are automatically terminated by an invisible and not counted zero char. I.e. you can/should SetLength(str, charcount)
to extend or shrink a dynamic String to the required length, with a zero
byte at its end as expected by API (C) funtions.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 67:07:38 |
Calls: | 6,654 |
Files: | 12,200 |
Messages: | 5,331,951 |