cant attach CDC?

Started by
4 comments, last by Code Is Your Friend 15 years, 3 months ago
I am not sure what I am doing wrong but it causes an exception. HDC dc = NULL; CDC* pdC = NULL; dc = GetDC(hWnd); pdC->Attach(dc); *Exception* I know hWnd is good, I use it in the same class to load an image before entering here, and the value is the same. thanks, Matt
Advertisement
Quote:Original post by Code Is Your Friend
I am not sure what I am doing wrong but it causes an exception.

HDC dc = NULL;
CDC* pdC = NULL;

dc = GetDC(hWnd);
pdC->Attach(dc); *Exception*

I know hWnd is good, I use it in the same class to load an image before entering here, and the value is the same.

thanks,
Matt
At no point in that code do you initialise pdC. You probably want to do:
CDC dc = GetDC(hWnd);
That works, but it creates a problem. It only works if the CDC isn't a pointer. And I need it to be a pointer for the next function called, so I either need to convert it to a CDC* somehow, or get the pointer to accept the dc.

Matt

[Edited by - Code Is Your Friend on January 15, 2009 9:35:00 AM]
Still having problems with this, I have tried everything I can think of to get the CDC* to take a dc.

Is there just no way to do this?
Matt
Quote:Original post by Code Is Your Friend
Still having problems with this, I have tried everything I can think of to get the CDC* to take a dc.

Is there just no way to do this?
Matt
No - a CDC* is a pointer to a CDC that already exists. You need to initialise the pointer before you use it.

Quote:Original post by Code Is Your Friend
That works, but it creates a problem. It only works if the CDC isn't a pointer. And I need it to be a pointer for the next function called, so I either need to convert it to a CDC* somehow, or get the pointer to accept the dc.
What function? Can't you just use &dc?:
CDC dc = GetDC(hWnd);SomeFuncThatTakesPointerToCDC(&dc);


Or:
CDC* pDC = new CDC;pDC->Attach(GetDC(hWnd));SomeFuncThatTakesPointerToCDC(pDC);// When you're done with pDC:delete pDC;

I doubt you need it to be a pointer though.
Oh well hell. I dont know why it didnt occur to me to use use &, I guess sometimes when your looking too deep its hard to see the obvious. Thank you very much, you have fixed the problem :)

Matt

This topic is closed to new replies.

Advertisement