Author Topic: GetStockObject not working right?  (Read 9297 times)

Offline Knyght

  • Single posting newcomer
  • *
  • Posts: 4
GetStockObject not working right?
« on: February 06, 2006, 09:17:25 pm »
Not sure if thise is the right forum, or if I should be asking at all, but I don't know where else to ask, so here goes.

from wingdi.h, included by windows.h.

hFont = GetStockObject(SYSTEM_FONT);

This should work. msdn says the function should be HGDIOBJ GetStockObject(int fnObject); which looks right for what I'm doing.

I looked in wingdi.h and the function declaration is the same.

However, when I run it in codeblocks I get "error: invalid conversion from 'void*' to 'HFONT__*'. This seems odd.

Am I doing something wrong, or is it a problem elsewhere?

Thanks.

sethjackson

  • Guest
Re: GetStockObject not working right?
« Reply #1 on: February 06, 2006, 09:35:41 pm »
This should work.  :D

Code: cpp
hFont = static_cast<HFONT>(GetStockObject(SYSTEM_FONT));


Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GetStockObject not working right?
« Reply #2 on: February 06, 2006, 09:36:05 pm »
Code
typedef void *HGDIOBJ;
[...]
WINGDIAPI HGDIOBJ WINAPI GetStockObject(int);
[...]
HFONT hFont = GetStockObject(SYSTEM_FONT);

No problem, that's perfectly right. The compiler says exactly what you do.
Try casting to HFONT as Seth said.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

sethjackson

  • Guest
Re: GetStockObject not working right?
« Reply #3 on: February 06, 2006, 09:37:42 pm »
BTW see here for why.

http://www.winprog.org/tutorial/errors.html#C2440

DO NOT cast the way they suggested.  :P

static_cast, and it's friends are sooooo much better. :)

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GetStockObject not working right?
« Reply #4 on: February 06, 2006, 09:43:30 pm »
DO NOT cast the way they suggested.  :P
static_cast, and it's friends are sooooo much better. :)
Casting is a hack anyway, so why make such a fuzz about it :P
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

sethjackson

  • Guest
Re: GetStockObject not working right?
« Reply #5 on: February 06, 2006, 09:48:55 pm »
DO NOT cast the way they suggested.  :P
static_cast, and it's friends are sooooo much better. :)
Casting is a hack anyway, so why make such a fuzz about it :P

True, but C-style casting is hard to spot (most of the time), and not as good.

http://public.research.att.com/~bs/bs_faq2.html#static-cast

Anyways avoid casting at all costs:D

Offline Knyght

  • Single posting newcomer
  • *
  • Posts: 4
Re: GetStockObject not working right?
« Reply #6 on: February 06, 2006, 09:55:56 pm »
I love you guys.

sethjackson

  • Guest
Re: GetStockObject not working right?
« Reply #7 on: February 06, 2006, 09:56:56 pm »
Well I guess that means it works now.  :lol:

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GetStockObject not working right?
« Reply #8 on: February 06, 2006, 10:02:41 pm »
True, but C-style casting is hard to spot (most of the time), and not as good.
What makes you think that it is "not as good"? It is really the same thing, except that you can do things like
Code
Foo *foo = new Foo;
Bar *bar = (Bar*) foo;
int a = (int) foo;
printf("%s", (const char*) a);
delete bar; // this one is especially good... :)
int b = 5;
wxWindow *c = (wxWindow*) b;
c->Show();
with old-style C casts, which static_cast will of course not allow you to do.

However, static_cast's "friends" let you do such stuff, so if you want to shoot your foot, you can do it either way, and if you don't know what you're doing, you'll go to hell either way, too. :lol:
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

sethjackson

  • Guest
Re: GetStockObject not working right?
« Reply #9 on: February 06, 2006, 10:07:45 pm »
:shock: That code sample.  :lol: :lol:

Anyways you are right it is the same thing. The C++ style casts are easier to spot however.  :)

In my case I'd rather not shoot myself at all.  :lol: :lol:

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: GetStockObject not working right?
« Reply #10 on: February 07, 2006, 10:35:07 am »
True, but C-style casting is hard to spot (most of the time), and not as good.
What makes you think that it is "not as good"? It is really the same thing, except that you can do things like
Code
Foo *foo = new Foo;
Bar *bar = (Bar*) foo;
int a = (int) foo;
printf("%s", (const char*) a);
delete bar; // this one is especially good... :)
int b = 5;
wxWindow *c = (wxWindow*) b;
c->Show();
with old-style C casts, which static_cast will of course not allow you to do.

However, static_cast's "friends" let you do such stuff, so if you want to shoot your foot, you can do it either way, and if you don't know what you're doing, you'll go to hell either way, too. :lol:

Cast is ugly and C-Style cast is uglier, even if easier to use :). C++ Cast Style is better, but this does not mean that it should be used everywhere and everytime :). There are some posts where we have discussed cast related problems. May be you might would like to have a look at here:


Best wishes,
Michael

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GetStockObject not working right?
« Reply #11 on: February 07, 2006, 04:15:12 pm »
These two lines taken from a recent patch (the anonymous author will hopefully forgive me posting them here) show that C++ casts are not better in any way:
Code
-    if(abs(compilerIdx)>=CompilerFactory::Compilers.GetCount())
+    if(static_cast<unsigned>(compilerIdx)>=CompilerFactory::Compilers.GetCount())
This patch's purpose is to remove "comparing signed vs. unsigned " compiler warning because abs declares as int abs(int).

The introduced C++ cast will remove the warning by making the compiler treat the number as unsigned, however, it did not only fool the compiler, but also the programmer!

static_cast<unsigned> looks so good and so innocent. Except that without abs(), it is not unsigned at all, you only treat it as such. Thus, a value of -1 now becomes 4294967295. By mere coincidence (because of the >= comparison) the above code might still work, but it is clearly wrong.

And this is my whole point: C++ casts are not better than C casts.
They are different, more explicit, but not better.

Casts are a means to break the rules of the language and change the semantics of data, not more, not less. Using casts can likewise shoot your foot or blow away your whole leg, whether you use C or C++.  8)
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5297
Re: GetStockObject not working right?
« Reply #12 on: February 07, 2006, 04:26:20 pm »
or as Meyers put it, the c++ ones are even uglier and harder to type, so you would think again and try to avoid the need for a cast all together. ;-)
and on the plus side : they are easier to find.

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: GetStockObject not working right?
« Reply #13 on: February 07, 2006, 05:44:24 pm »
These two lines taken from a recent patch (the anonymous author will hopefully forgive me posting them here) show that C++ casts are not better in any way:
Code
-    if(abs(compilerIdx)>=CompilerFactory::Compilers.GetCount())
+    if(static_cast<unsigned>(compilerIdx)>=CompilerFactory::Compilers.GetCount())
This patch's purpose is to remove "comparing signed vs. unsigned " compiler warning because abs declares as int abs(int).

The introduced C++ cast will remove the warning by making the compiler treat the number as unsigned, however, it did not only fool the compiler, but also the programmer!

static_cast<unsigned> looks so good and so innocent. Except that without abs(), it is not unsigned at all, you only treat it as such. Thus, a value of -1 now becomes 4294967295. By mere coincidence (because of the >= comparison) the above code might still work, but it is clearly wrong.

And this is my whole point: C++ casts are not better than C casts.
They are different, more explicit, but not better.

Casts are a means to break the rules of the language and change the semantics of data, not more, not less. Using casts can likewise shoot your foot or blow away your whole leg, whether you use C or C++.  8)

The anonymous author agrees with most of what you have said :D.

IMHO the compiler warning "comparing signed vs. unsigned" should be removed (soon or later :)), because mixing signed and unsigned int could lead to errors difficult to spot. The cast seems worste than the abs() function (because of negative numbers), but this function seems not the final solution too.

I have found some interesting information in these slides.

Michael

[EDIT]: I did last night some tests with cast, abs() and so on in order to understand better and effectively cast unsigned from signed is bad, especially when signed can have a negative value. If the signed will have only positive values, then there are no problems (probably :)), but if it can get negative values then this leads to problems and in some cases (if not in the most :D) to big problems. abs() seems to be here much better solution. The anonymous author has learnt something useful :D.
« Last Edit: February 08, 2006, 11:36:14 am by Michael »