> From: Jason L. Coley [mailto:[EMAIL PROTECTED]]
> Well its pretty obvious that I don't know much about exception handling,
> can anyone suggest a good book on exceptions etc. I just try my best to
> write code that doesn't bomb out, I'd say handling the errors better
> would be a betteer way, and probably faster in the long run.
Exceptions aren't too complicated - you probably don't need a book on them.
The Delphi help goes into a fair bit of detail and there are examples of
their use in the demos.
> Anyway, could you look at the following and tell me what would be
> incorrect about the following?
Seems ok to me. Although GetMem can fail too (raising an EOutOfMemory
exception), so really you should go
GetMem(PaperNameArray, PaperNameCount * cchPaperName);
try
GetMem(PaperSizeArray, Needed * Sizeof(Word));
try
GetMem(PaperIDArray, Needed * SizeOf(TPoint));
try
...
finally
FreeMem(PaperIDArray);
end;
finally
FreeMem(PaperSizeArray);
end;
finally
FreeMem(PaperNameArray);
end;
(Just like I didn't do in my last message).
An alternative way to do this, perhaps not better, is to allocate one large
buffer for all three "array"s, and to point each PChar at a different part.
var Buffer: PChar;
GetMem(Buffer, PaperNameCount * cchPaperName + Needed * (SizeOf(Word) +
SizeOf(TPoint)));
try
PaperNameArray := Buffer;
PaperSizeArray := @PaperNameArray[PaperNameCount * cchPaperName];
PaperIDArray := @PaperSizeArray[PaperIDArray, Needed * SizeOf(Word)];
...
finally
FreeMem(Buffer);
end;
Either way it's a bit laboured, especially when combined with Move. If you
ever go to Delphi 5 dynamic arrays will make your life easier. Really you
don't want to make these variables PChar, you want PWord, PPoint etc., but
since Delphi's pointer types won't let you index into them (ie. MyWord :=
PaperSizeArray[Index] is not allowed for PaperSizeArray: PWord), it's not
worth it.
Cheers,
Carl
-----Original Message-----
From: Jason L. Coley [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, 10 October 2000 2:26 PM
To: Multiple recipients of list delphi
Subject: RE: RE: [DUG]: Creating PChars
Well its pretty obvious that I don't know much about exception handling,
can anyone suggest a good book on exceptions etc. I just try my best to
write code that doesn't bomb out, I'd say handling the errors better
would be a betteer way, and probably faster in the long run.
Here comes a complete rewrite!
Anyway, could you look at the following and tell me what would be
incorrect about the following?
Jason
procedure TSetup.SetPaperSizeText(Update : Boolean);
const
cchPaperName = 64;
var
PaperNameCount, Index, Needed: Integer;
PaperNameArray: PChar;
PaperIDArray: PChar;
PaperSizeArray: PChar;
PaperSize: TPoint;
PaperName: array[0..cchPaperName - 1] of Char;
PaperID: Word;
begin
//Get the number of paper name items
PaperNameCount := DeviceCapabilities(pDevice, pPort,
dc_Papernames, nil, nil);
if PaperNameCount = -1 then raise EDCFailed.Create('Can''t get
paper name count');
Needed := DeviceCapabilities(pDevice, pPort, dc_PaperSize, nil,
nil);
if Needed = -1 then raise EDCFailed.Create('Can''t get paper
size count');
GetMem(PaperNameArray, PaperNameCount * cchPaperName);
GetMem(PaperSizeArray, Needed * Sizeof(Word));
GetMem(PaperIDArray, Needed * SizeOf(TPoint));
try
if DeviceCapabilities(pDevice, pPort, dc_Papernames,
PaperNameArray, nil) = -1 then raise EDCFailed.Create('Can''t get the
Paper names');
if DeviceCapabilities(pDevice, pPort, dc_PaperSize,
PaperSizeArray, nil) = -1 then raise EDCFailed.Create('Can''t get the
Paper sizes');
if DeviceCapabilities(pDevice, pPort, dc_Papers,
PaperIDArray, nil) = -1 then raise EDCFailed.Create('Can''t get the
Paper IDs');
for Index := 0 to Needed - 1 do
begin
Move(PaperIDArray[SizeOf(Word) * Index],
PaperID, SizeOf(Word));
if MailPiecepDMode^.dmPaperSize = PaperID then
begin
//Get the paper name
if Update then
begin
Move(PaperNameArray[cchPaperName
* Index], PaperName[0], cchPaperName);
cboPaperSize.ItemIndex :=
cbopaperSize.Items.IndexOf(PaperName);
end;
//Get the paper size
Move(PaperSizeArray[SizeOf(TPoint) *
Index], PaperSize, SizeOf(TPoint));
//For some reason the print drivers do
not return the papersize info?
If (PaperSize.x < 1) or (PaperSize.y <
1) then
begin
If rdoPortrait.Checked then
begin
PaperSize.x :=
RoundUp(GetDeviceCaps(Printer.Handle, PHYSICALWIDTH) / ScaleX);
PaperSize.y :=
RoundUp(GetDeviceCaps(Printer.Handle, PHYSICALHEIGHT) / ScaleY);
end
else
begin
PaperSize.y :=
RoundUp(GetDeviceCaps(Printer.Handle, PHYSICALWIDTH) / ScaleX);
PaperSize.x :=
RoundUp(GetDeviceCaps(Printer.Handle, PHYSICALHEIGHT) / ScaleY);
end;
end;
end;
end;
finally;
FreeMem(PaperNameArray);
FreeMem(PaperSizeArray);
FreeMem(PaperIDArray);
end;
end;
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"