Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#ifdef DEBUG_PRINTER
//---------------------------------------------------------------------------

void __fastcall LogDebugTxt(AnsiString Txt)
{
if (!FileExists("D:\AGBdebug.txt"))
DebugHandle = FileCreate("D:\AGBdebug.txt");
else
DebugHandle = FileOpen("D:\AGBdebug.txt",fmOpenReadWrite);
FileSeek(DebugHandle,0,2);
FileWrite(DebugHandle,Txt.c_str(),Txt.Length());
// if (Txt.Po != '\n')
FileWrite(DebugHandle,"\r\n",2);
FileClose(DebugHandle);
}
#endif
//---------------------------------------------------------------------------

AnsiString __fastcall GetPrinterName()
{
int pi = Printer()->PrinterIndex;
#ifdef DEBUG_PRINTER
LogDebugTxt(NowtStr);
LogDebugTxt("Debug test start");
LogDebugTxt("----------------");
TStrings* prns = Printer()->Printers;
LogDebugTxt("PrinterCount = " + AnsiString(prns->Count));
LogDebugTxt("PrinterIndex = " + IntToStr(pi));
LogDebugTxt(NowtStr);
LogDebugTxt("Get the printer string array");
s_Bfr = Printer()->Printers[0].GetText();
LogDebugTxt(NowtStr);
LogDebugTxt("List the array content");
LogDebugTxt("--- list start ---");
LogDebugTxt(s_Bfr);
LogDebugTxt("--- list end ---");
LogDebugTxt(NowtStr);
LogDebugTxt("Isolate the printer name");

while (pi > 0)
{ // Remove entries before the indexed target
s_Bfr = s_Bfr.SubString(s_Bfr.Pos("\r") + 1,s_Bfr.Length());
pi--;
}
pi = s_Bfr.Pos("\r");
if (pi) // remove all that follow it
s_Bfr.SetLength(pi - 1);
s_Bfr = s_Bfr.Trim();
LogDebugTxt(s_Bfr);
LogDebugTxt("--------------");
LogDebugTxt("Debug test end");
#else // non-debug version code
s_Bfr = Printer()->Printers[0].GetText();
while (pi > 0)
{
s_Bfr = s_Bfr.SubString(s_Bfr.Pos("\r") + 1,s_Bfr.Length());
pi--;
}
pi = s_Bfr.Pos("\r");
if (pi)
s_Bfr.SetLength(pi - 1);
#endif
return s_Bfr.Trim();
}
//---------------------------------------------------------------------------

What I have tried:

THE PROBLEM IS FIXED NOW, with many thanks to those who provided input. The displayed code listing is the debug code used. All works fine on all target platforms.

Note that the LogDebugTxt function keeps the log file normally closed so that in the event of a fatal crash the log data will not be lost.

Again, thank you for your help on this one. AGB
Posted
Updated 4-Jun-20 17:35pm
v4
Comments
Richard MacCutchan 3-Jun-20 3:57am    
Oh, and we are supposed to guess what question you are referring to?
ZurdoDev 3-Jun-20 7:58am    
After 13 guesses I feel like I almost had it. Just missed though.
[no name] 3-Jun-20 5:47am    
Check the size of Printers.

TStrings* prns= Printer()->Printers;
LogDebugTxt("Count= " + AnsiString(prns->Count));


And check also whter Printer()->PrinterIndex is in a valid range. Most probably no DefaultPrinter selected and the value of PrinterIndex == -1
ZurdoDev 3-Jun-20 7:58am    
What exactly is your question?
jeron1 3-Jun-20 10:29am    
You should update your original question with any changes that you have made and/or new information you may have. That thread will be pushed to the top when you do that, hence no need for "Question.....(continued)" threads.

1 solution

This is more a guess than an answer, because too much is not clear. Another thing is, I do answer it from a perspective of c++ Builder 6, because on a first glance (you mentioned AnsiString and also Printer()->Printers) it looks to be very similar.

Take into acount
Printer()->PrinterIndex
can be '-1' which means in Builder it is the default Printer. And in this case you can't get the printer name by Printer()->Printers[p].GetText(); because this will end in index out of range or usually with borland in an access violation.

The next problem I face usually in Builder environement: In case no default printer is assigned (which is on the other hand very seldom under W10) you can get in problems.

Finally, accessing printer information needs to be done carefully. An example:
C++
TStrings* prns= Printer()->Printers;
if (prns == NULL)
{
  // This should never happen
  throw (*new Exception("Printer()->Printer returned NULL"));
}

LogDebugTxt("Available printers, no of printers is: " + AnsiString(prns->Count));
for (int ix= 0; ix < prns->Count; ix++)
{
  LogDebugTxt(AnsiString(ix) + prns[ix]);
}

LogDebugTxt("Current Printer Index= " + AnsiString(Printer()->PrinterIndex));


I hope it helps and that I'm not completely wrong. Feel free to ask back.
 
Share this answer
 
v2
Comments
Maciej Los 3-Jun-20 14:42pm    
5ed!
[no name] 3-Jun-20 14:44pm    
Thank you Maciej to give me five for a guess ;)
Maciej Los 3-Jun-20 14:49pm    
;)
[no name] 3-Jun-20 15:38pm    
At least I'm involved for support on some legacy applications implemented with borland stuff :)
Btw. Do you know, what happend with Sergei (SAK), I think to remember he also worked with this delphi/builder stuff, but he seems to offline for a very long period now.
Maciej Los 4-Jun-20 2:01am    
Sorry for late reply... Well, i haven't seen Sergey long time. I hope He is OK. I was thinking about Him several times, but never decided to write Him few words.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900