CDC invalid handle 0xC0000008

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!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Deepu AbrahamR & D Engineering ManagerCommented:
Can you show the snippet?
threadyAuthor Commented:
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)....

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.
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

threadyAuthor Commented:
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!

threadyAuthor Commented:
i meant attack - not attach.....

threadyAuthor Commented:
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.m_bPrinting = TRUE;

the dc that I attach to is the one I use later..  But this is failing in ***DoModal*** (intermittently) now....
threadyAuthor Commented:
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!
threadyAuthor Commented:
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??
Deepu AbrahamR & D Engineering ManagerCommented:
Can you try this?
void CMyView::OnDraw(CDC* pDC)
    CFont font;

    font.CreateFont(46, 28, 215, 0,
              DEFAULT_PITCH | FF_ROMAN, "Times New Roman");

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


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

Best Regards,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

This page may give you an idea... contains lots of example codes of how to use MFC for printing.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.