473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

CreateDC throws error on 203rd loop.

L
Hi guys,

I am writing some interop code to print graphics to an eps file
and I use adobe printer driver. The piece of code I have written is
working fine. but if I loop for more than 200 times, it is throwing an
error. Can anybody tell me why it throws an error or am I having memory
problems? I debuged through it and I see that createDC is returning 0
on 203rd loop and throws an exception. Why is createDC returning a 0?
This is the piece of code that does the interop.

using System;
using System.Drawing;
using System.IO;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Drawing.Text;

namespace Printer
{
public class CPrinter
{
private Graphics pDC;
private IntPtr dc1;
private IntPtr hdevmode;

[StructLayout( LayoutKind.Sequential)]
public struct DOCINFO
{
[MarshalAs(UnmanagedType.LPWStr)]public string pDocName;
[MarshalAs(UnmanagedType.LPWStr)]public string pOutputFile;
[MarshalAs(UnmanagedType.LPWStr)]public string pDataType;
}

[DllImport(
"winspool.drv",CharSet=CharSet.Unicode,ExactSpelli ng=false,
CallingConvention=CallingConvention.StdCall )]
public static extern long StartDocPrinter(IntPtr hPrinter, int Level,
ref DOCINFO pDocInfo);

[DllImport("gdi32.dll", EntryPoint="CreateDCA")]
public static extern IntPtr CreateDC(string lpDriverName, string
lpDeviceName,
string lpOutput, IntPtr pDevMode);

[DllImport("gdi32.dll")]
static extern bool DeleteDC(IntPtr hdc);

[DllImport("gdi32", EntryPoint="StartDocA")]
public static extern int StartDoc(IntPtr dc1, ref DOCINFO di);

[DllImport("gdi32")] public static extern int StartPage(IntPtr hDC);
[DllImport("gdi32")] public static extern int EndDoc(IntPtr hDC);

[DllImport("gdi32")] public static extern int EndPage(IntPtr hDC);

public CPrinter()
{
}
public void StartPrinter(string outputFile)
{
try
{
DOCINFO di = new DOCINFO();
di.pOutputFile = outputFile;
PrintDocument pd = new PrintDocument();
hdevmode = pd.PrinterSettings.GetHdevmode();
dc1 = CreateDC("","Acrobat Distiller 3x2",outputFile,hdevmode);
int result = StartDoc(dc1, ref di);
result = StartPage(dc1);
pDC = Graphics.FromHdc(dc1);
pDC.PageUnit = GraphicsUnit.Pixel;
}
catch(Exception ex)
{
throw(ex);
}
}

public void EndPrinter()
{
int result = EndPage(dc1);
result = EndDoc(dc1);
DeleteDC(dc1);
}
}
}

This is the piece of code where I loop :

using System;
using Printer;

namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int i = 0;
try
{
while (true)
{
CPrinter AdobePrinter = new CPrinter();
AdobePrinter.StartPrinter("temp.wrk");
AdobePrinter.EndPrinter();
i++;
Console.WriteLine(i);
}
}
catch (Exception ex)
{
Console.WriteLine( i + " : " + ex);
}
}
}
}

Thanks,
Lalasa.

Nov 16 '05 #1
9 4964
"L" <la********@investors.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi guys,

I am writing some interop code to print graphics to an eps file
and I use adobe printer driver. The piece of code I have written is
working fine. but if I loop for more than 200 times, it is throwing an
error. Can anybody tell me why it throws an error or am I having memory
problems? I debuged through it and I see that createDC is returning 0
on 203rd loop and throws an exception. Why is createDC returning a 0?

You are using a Disposable object (Graphics) without disposing it.


This is the piece of code that does the interop.

using System;
using System.Drawing;
using System.IO;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Drawing.Text;

namespace Printer
{
public class CPrinter
{
private Graphics pDC;
private IntPtr dc1;
private IntPtr hdevmode;

[StructLayout( LayoutKind.Sequential)]
public struct DOCINFO
{
[MarshalAs(UnmanagedType.LPWStr)]public string pDocName;
[MarshalAs(UnmanagedType.LPWStr)]public string pOutputFile;
[MarshalAs(UnmanagedType.LPWStr)]public string pDataType;
}

[DllImport(
"winspool.drv",CharSet=CharSet.Unicode,ExactSpelli ng=false,
CallingConvention=CallingConvention.StdCall )]
public static extern long StartDocPrinter(IntPtr hPrinter, int Level,
ref DOCINFO pDocInfo);

[DllImport("gdi32.dll", EntryPoint="CreateDCA")]
public static extern IntPtr CreateDC(string lpDriverName, string
lpDeviceName,
string lpOutput, IntPtr pDevMode);

[DllImport("gdi32.dll")]
static extern bool DeleteDC(IntPtr hdc);

[DllImport("gdi32", EntryPoint="StartDocA")]
public static extern int StartDoc(IntPtr dc1, ref DOCINFO di);

[DllImport("gdi32")] public static extern int StartPage(IntPtr hDC);
[DllImport("gdi32")] public static extern int EndDoc(IntPtr hDC);

[DllImport("gdi32")] public static extern int EndPage(IntPtr hDC);

public CPrinter()
{
}
public void StartPrinter(string outputFile)
{
try
{
DOCINFO di = new DOCINFO();
di.pOutputFile = outputFile;
PrintDocument pd = new PrintDocument();
hdevmode = pd.PrinterSettings.GetHdevmode();
dc1 = CreateDC("","Acrobat Distiller 3x2",outputFile,hdevmode);
int result = StartDoc(dc1, ref di);
result = StartPage(dc1);
pDC = Graphics.FromHdc(dc1);
pDC.PageUnit = GraphicsUnit.Pixel;
}
catch(Exception ex)
{
throw(ex);
}
}

public void EndPrinter()
{
int result = EndPage(dc1);
result = EndDoc(dc1);
DeleteDC(dc1);
}
}
}

This is the piece of code where I loop :

using System;
using Printer;

namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int i = 0;
try
{
while (true)
{
CPrinter AdobePrinter = new CPrinter();
AdobePrinter.StartPrinter("temp.wrk");
AdobePrinter.EndPrinter();
i++;
Console.WriteLine(i);
}
}
catch (Exception ex)
{
Console.WriteLine( i + " : " + ex);
}
}
}
}

Thanks,
Lalasa.

Nov 16 '05 #2
L

I added the pDC.Dispose() to my EndPrinter() function and still I am
having the problem.

public void EndPrinter()
{
int result = EndPage(dc1);
result = EndDoc(dc1);
pDC.Dispose();
DeleteDC(dc1);
}

Thanks,
L.

Nov 16 '05 #3
Can anybody tell me why it throws an error or am I having memory
problems?
You're net freeing the handle returned by GetHDevmode.

public static extern long StartDocPrinter(IntPtr hPrinter, int Level,

^^^^

The return type should be (u)int.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #4
L
Can you tell me the method I should use to release the handle returned
by GetHDevmode? I am not able to find one.

Thanks,
Lalasa.

Nov 16 '05 #5
Can you tell me the method I should use to release the handle returned
by GetHDevmode?


The GlobalFree Win32 API, just like thie documentation says.

http://msdn.microsoft.com/library/en...modeTopic1.asp

Marshal.FreeHGlobal probably works as well.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #6
L
Thankyou very much for helping out.

Nov 16 '05 #7
L
Hi,

Is there anything else I got to correct. Because I am having the
same error now after 1122 loops.
Here is my EndPrinter() function

public void EndPrinter()
{
int result = EndPage(dc1);
result = EndDoc(dc1);
pDC.Dispose();
Marshal.FreeHGlobal(hdevmode); // Frees the memory. You could also
free the memory by calling the native win32 globalfree method.
DeleteDC(dc1);
}

Thanks in advance
Lalasa.

Mattias Sjögren wrote:
Can you tell me the method I should use to release the handle returnedby GetHDevmode?
The GlobalFree Win32 API, just like thie documentation says.

http://msdn.microsoft.com/library/en...modeTopic1.asp
Marshal.FreeHGlobal probably works as well.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Nov 16 '05 #8
Is there anything else I got to correct. Because I am having the
same error now after 1122 loops.


Well your DOCINFO struct doesn't look correct compared to the one in
the MSDN documentation.

Then I recommend that you use Task Manager to monitor the number of
GDI handles used in your application to ensure that you're not leaking
any.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #9
L
Well, I figured it out. The memory leak was happening in my main code
where I was using interop calls for creating fonts. I fixed my dispose
function to delete all the fonts and it is working in an end less loop
now. Thanks for the help.

Lalasa.

Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: jerrygarciuh | last post by:
Hi all, I was just wondering what popular opinion is on PHP giving this warning: Warning: Invalid argument supplied for foreach() in /home/boogerpic/public_html/my.php on line 6 when...
0
by: Piotr Bieniek | last post by:
Hello, I have a problem with UDP sockets. It concerns UdpClient class as well. It throws strange exceptions on subsequent Send calls. Exception is SocketException with native error code 10049. I...
2
by: Piotr Bieniek | last post by:
Hello, I have a problem with UDP sockets. It concerns UdpClient class as well. It throws strange exceptions on subsequent Send calls. Exception is SocketException with native error code 10049. I...
1
by: Mike C# | last post by:
Hi all, I'm writing a little app that uses ODBC to call a SQL 2K stored procedure several times in a loop, with a different parameter each time. The SP can return no results in some instances. ...
1
by: Me | last post by:
Hello, The code below runs well on IE 6 and 7. On Firefox it loads the first time but throws the following error on reloads. Error: top is not defined Source File: top.myIFrameBody();...
5
by: kyosohma | last post by:
Hi, I am trying to create a post logon script which does various tasks, like setup a printer based on location. While most of it works very fast, I have a second Python script that I run that...
2
by: sbasavar | last post by:
Hi All, In my excel programming, I am using XML http object (MSXML2.XMLHTTP.3.0). Using this object when I try to send a request application throws an "Access is Denied" error. Strange thing...
4
by: wordone | last post by:
Sometimes it works, sometimes it doesn't but I can't find the bug: package Loop.Test; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class...
2
by: alireza6485 | last post by:
Hi, Could you please rewrite the program for me?I tried my best and the program still does not do what it has to do. I have to write a code that generates random speed and distance .it ask the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.