1. How do MAKEINTRESOURCE in Euphoria?
- Posted by ArthurCrump May 16, 2013
- 2137 views
I am trying to wrap an API which wants me to use MAKEINTRESOURCE.
I thought I knew enough C to know what the C code for this meant, but I seem to have forgotten it all.
Can somebody tell me the Euphoria equivalent of the MAKEINTRESOURCE macro? It seems to be defined as:
#define MAKEINTRESOURCEW (x) ((LPWSTR) (U_LONG_PTR) (WORD) (x))
2. Re: How do MAKEINTRESOURCE in Euphoria?
- Posted by mattlewis (admin) May 16, 2013
- 2117 views
I am trying to wrap an API which wants me to use MAKEINTRESOURCE.
I thought I knew enough C to know what the C code for this meant, but I seem to have forgotten it all.
Can somebody tell me the Euphoria equivalent of the MAKEINTRESOURCE macro? It seems to be defined as:
#define MAKEINTRESOURCEW (x) ((LPWSTR) (U_LONG_PTR) (WORD) (x))
This appears to be casting to a WORD (16-bit unsigned) and then up into a pointer, so you should be able to do:
function MAKEINTRESOURCE( atom x ) return and_bits( 0xffff, x ) end function
Matt
3. Re: How do MAKEINTRESOURCE in Euphoria?
- Posted by ArthurCrump May 16, 2013
- 2052 views
#define MAKEINTRESOURCEW (x) ((LPWSTR) (U_LONG_PTR) (WORD) (x))
This appears to be casting to a WORD (16-bit unsigned) and then up into a pointer, so you should be able to do:
function MAKEINTRESOURCE( atom x ) return and_bits( 0xffff, x ) end function
Matt
Thank you. That gives the results I was hoping for, although I don't understand how it can deduce the original atom.
Arthur
4. Re: How do MAKEINTRESOURCE in Euphoria?
- Posted by CoJaBo2 May 16, 2013
- 2072 views
This appears to be casting to a WORD (16-bit unsigned) and then up into a pointer, so you should be able to do:
function MAKEINTRESOURCE( atom x ) return and_bits( 0xffff, x ) end function
This isn't correct; the primary purpose of that is to do the typecast, so that Windows can store a 16-bit integer ID in a type defined as "pointer" for convoluted reasons; Eu doesn't have types, so in Eu this macro would be a complete no-op.
If you want to be safe, you can do an assert/check to make sure the ID is less than 0xffff, as something is wrong if it isn't. If you just mask it to 16 bits, like the code above, you'll silently corrupt the incorrect value into another incorrect value.
5. Re: How do MAKEINTRESOURCE in Euphoria?
- Posted by jimcbrown (admin) May 16, 2013
- 2074 views
This isn't correct; the primary purpose of that is to do the typecast, so that Windows can store a 16-bit integer ID in a type defined as "pointer" for convoluted reasons; Eu doesn't have types, so in Eu this macro would be a complete no-op.
My understanding was that with C, when you cast to a smaller integer type, then you lose bits. Casting back to a larger integer type does not restore the lost bits. In that case, mattlewis's code properly emulates the C behavior.
If you want to be safe, you can do an assert/check to make sure the ID is less than 0xffff, as something is wrong if it isn't. If you just mask it to 16 bits, like the code above, you'll silently corrupt the incorrect value into another incorrect value.
Agreed. Better to assert and fail early, than to do it the C way and fail bizarrely.
6. Re: How do MAKEINTRESOURCE in Euphoria?
- Posted by andi49 May 16, 2013
- 2077 views
I am trying to wrap an API which wants me to use MAKEINTRESOURCE.
I thought I knew enough C to know what the C code for this meant, but I seem to have forgotten it all.
Can somebody tell me the Euphoria equivalent of the MAKEINTRESOURCE macro? It seems to be defined as:
#define MAKEINTRESOURCEW (x) ((LPWSTR) (U_LONG_PTR) (WORD) (x))
Hallo
Matt's solution is pretty okay in my opinion.
MakeIntResource is a hack in the Windowsworld.
Windows just uses this to distinguish a handle or a pointer from an index to a resource.
If the parameter is bigger than #ffff it's an Pointer or a Handle else it is an index to a resource.
On old Windowsversions this may end in a bluescreen if someone passes accidently a parameter bigger than #ffff.
MakeIntResource is the airbag in this case.
But i think it is easier to check the value yourself before you pass it to the Api Function.
Maybe you can check out this for more information : http://stackoverflow.com/questions/9806100/makeintresource-returning-bad-pointers-for-resource-ids
Andreas
7. Re: How do MAKEINTRESOURCE in Euphoria?
- Posted by ArthurCrump May 17, 2013
- 2129 views
Matt's solution is pretty okay in my opinion.
MakeIntResource is a hack in the Windowsworld.
Windows just uses this to distinguish a handle or a pointer from an index to a resource.
If the parameter is bigger than #ffff it's an Pointer or a Handle else it is an index to a resource.
On old Windowsversions this may end in a bluescreen if someone passes accidently a parameter bigger than #ffff.
MakeIntResource is the airbag in this case.
But i think it is easier to check the value yourself before you pass it to the Api Function.
Maybe you can check out this for more information : http://stackoverflow.com/questions/9806100/makeintresource-returning-bad-pointers-for-resource-ids
Andreas
I wouldn't dream of using MAKEINTRESOURCE if I was writing new C code, which I cannot do, having forgotten what little I knew about C. However, I am wrapping an API whose constants are defined using this hack. Now that I know what the values are, they will go straight into the wrapper and MAKEINTRESOURCE doesn't need to be mentioned, although I may be a bit sarcastic about Microsoft using an ancient hack in a modern routine.
Arthur
8. Re: How do MAKEINTRESOURCE in Euphoria?
- Posted by mattlewis (admin) May 17, 2013
- 2013 views
This appears to be casting to a WORD (16-bit unsigned) and then up into a pointer, so you should be able to do:
function MAKEINTRESOURCE( atom x ) return and_bits( 0xffff, x ) end function
This isn't correct; the primary purpose of that is to do the typecast, so that Windows can store a 16-bit integer ID in a type defined as "pointer" for convoluted reasons; Eu doesn't have types, so in Eu this macro would be a complete no-op.
Euphoria's and_bits() also returns an unsigned value, so it's not necessarily a no-op, though I agree that it is if you're dealing with something that's already a valid value. In my code, I usually ignore this sort of macro for the purposes of euphoria.
Matt