Solved

CDC invalid handle  0xC0000008

Posted on 2007-04-04
10
582 Views
Last Modified: 2009-07-29
Hi Experts,
I just added some new code to a program that I've been using a CDC object with for printing... I didn't modify any code that modifies the CDC object but when printing I'm now getting a "first-chance exception in MyProgram.exe (NTDLL.DLL):  0xC0000008: Invalid Handle."    This happens inside a CDC.DrawText call... The internal m_hDC looks like it's pointing at a valid location...  I'm not sure how to find the source of this bug...

Any comments are very appreciated - I'm quite stuck...

Thank you!
Mike
0
Comment
Question by:thready
10 Comments
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 18853727
Can you show the snippet?
0
 
LVL 1

Author Comment

by:thready
ID: 18853799
Hi DeepuAbrahamK - thanks for helping - please note that I think this is probably a stray pointer - I haven't touched any of my printing code in over a year!!  (the code is literally not touched)..  I think I'd be wasting your time by showing you code that has worked.. I just don't know how to find out which handle is invalid... Since the call stack ends up somewhere in NTDLL before it reports the error, I really have no idea exactly what is invalid... It must be something in the CDC object....  Do you still think code will help?   (it's a big momma mammoth class)....

Thanks,
Mike
0
 
LVL 11

Expert Comment

by:cup
ID: 18853888
The last time I got something like that it was to do with Fonts.  Are some of the fonts set to "MS Shell Dlg"?  This is one of those locale dependent fonts that lives in the registry.  If it doesn't exist, it will cause one of these invalid handle things.  This happens on the later versions of visual studio.  Didn't happen on Visual 6.  That is just a guess: it may or may not be the reason for your first chance exception.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:thready
ID: 18853933
Funny thing is I was thinking it does have something to do with fonts.... I will attach the fonts first....  I'll let you know - and thanks!

Mike
0
 
LVL 1

Author Comment

by:thready
ID: 18853935
i meant attack - not attach.....

:-P
0
 
LVL 1

Author Comment

by:thready
ID: 18854209
Nope - doesn't look like it's the fonts... It's now also happening before I even try to use the dc... Here's a snippet:

      CDC dc;
      CPrintDialog printDlg(FALSE);
      if(printDlg.DoModal() == IDCANCEL)
            return FALSE;
      dc.Attach(printDlg.GetPrinterDC());
      dc.m_bPrinting = TRUE;

the dc that I attach to is the one I use later..  But this is failing in ***DoModal*** (intermittently) now....
0
 
LVL 1

Author Comment

by:thready
ID: 18854427
ok this is weird - it **IS** font related (I'm using vis studio 6)... I changed all myfonts to Arial and it works...  Here's a code snippet:

m_FieldInfoFont.CreateFont(FIELD_INFO_SIZE*nMapFactor, 0, 0, 0, FW_BOLD, FALSE, bUnderLineFieldInfo, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_TT_ALWAYS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial");  //"Times New Roman");

In the snippet above, the font was "Times New Roman"... changing it to Arial gets rid of the invalid handle when I do a DrawText....   Any ideas?

Thanks again!
Mike
0
 
LVL 1

Author Comment

by:thready
ID: 18854434
Again - the above snippet is using Times new roman in the version of the exe that I haven't changed for a long time.... this is only happening in an updated version of the program which made me thing this was a stray pointer in new code....  Do you think it's a stray pointer?  Could it be something else font-related??
0
 
LVL 11

Accepted Solution

by:
DeepuAbrahamK earned 500 total points
ID: 18855757
Can you try this?
void CMyView::OnDraw(CDC* pDC)
{
    CFont font;

    font.CreateFont(46, 28, 215, 0,
                 FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
              OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
              DEFAULT_PITCH | FF_ROMAN, "Times New Roman");

    CFont *pFont = pDC->SelectObject(&font);
    pDC->TextOut(20, 128, "This is a test", 12);

    pDC->SelectObject(pFont);
    font.DeleteObject();
}

Hope you are doing delete aswell. Can you try this and let us know whats the behaviour?

Best Regards,
DeepuAbrahamK
0
 
LVL 4

Expert Comment

by:MacroLand
ID: 18856182
Hi,

This page may give you an idea... http://www.codeproject.com/printing/QPrint.asp

http://www.codeproject.com/printing/ contains lots of example codes of how to use MFC for printing.

Regards,
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question