StackOverFlow Exception with large input. Please help me analyse.  
Author Message
Jessica Alba





PostPosted: Visual C# General, StackOverFlow Exception with large input. Please help me analyse. Top

I need to send a long string to print through printer driver. below is the code. If the string is short, it is OK. But if the string is long (e.g., 1433 characters), the program will crash with StackOverflow Exception). Not sure if it got something to do with the IntPtr.

It crashed at SendBytesToPrinter(szPrinterName, docname, pBytes, encodedBytes.Length);

By putting Console.Writeline() to check, I found it didn't even enter the method above but the following code is completed and I could print out the value of encodedBytes.Length.

Marshal.Copy(encodedBytes, 0, pBytes, encodedBytes.Length);

Please share me your insight and experience. I will reward you with the good answer. Please help.

public static bool SendStringToPrinter(string szPrinterName,

string docname,

string szString)

{

IntPtr pBytes;

Int32 dwCount;

try

{

// How many characters are in the string

dwCount = szString.Length;

// Assume that the printer is expecting ANSI text, and then convert

// the string to ANSI text.

pBytes = Marshal.StringToCoTaskMemAnsi(szString);

Byte[] encodedBytes = Encoding.UTF8.GetBytes(szString);

Marshal.Copy(encodedBytes, 0, pBytes, encodedBytes.Length);

// Send the converted ANSI string to the printer.

SendBytesToPrinter(szPrinterName, docname, pBytes, encodedBytes.Length);

Marshal.FreeCoTaskMem(pBytes);

}

catch (Exception ex)

{

log.Error("Printing Exception : " + ex);

}

return true;

}

// SendBytesToPrinter()

// When the function is given a printer name and an unmanaged array

// of bytes, the function sends those bytes to the print queue.

// Returns true on success, false on failure.

public static bool SendBytesToPrinter(string szPrinterName,

string docname, IntPtr pBytes, Int32 dwCount)

{

Int32 dwError = 0, dwWritten = 0;

IntPtr hPrinter = new IntPtr(0);

DOCINFOA di = new DOCINFOA();

bool bSuccess = false; // Assume failure unless you specifically succeed.

di.pDocName = docname;

di.pDataType = "RAW";

// Open the printer.

if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))

{

// Start a document.

if (StartDocPrinter(hPrinter, 1, di))

{

// Start a page.

if (StartPagePrinter(hPrinter))

{

// Write your bytes.

bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);

EndPagePrinter(hPrinter);

}

EndDocPrinter(hPrinter);

}

ClosePrinter(hPrinter);

}

// If you did not succeed, GetLastError may give more information

// about why not.

if (bSuccess == false)

{

dwError = Marshal.GetLastWin32Error();

}

return bSuccess;

}



Visual C#13  
 
 
Nimrand





PostPosted: Visual C# General, StackOverFlow Exception with large input. Please help me analyse. Top

Have you tried using StringToHGlobal instead   Also, why do you even copy the bytes into the encodedBytes array when you don't appear to use it
 
 
cl408e





PostPosted: Visual C# General, StackOverFlow Exception with large input. Please help me analyse. Top

Hi,

Thanks for reply.

I am a newbie in C#. The code pasted actually is mostly copied from internet. I think I have some modification. The string I need to print sometimes contain Unicode characters.

Can you please tell me where should I should StringToGlobal And how to use it

How should I modifiy the code to make it more propoer

Please enlighten me. I will grade you, promise.