|
-
Oct 26th, 2001, 07:59 PM
#1
Thread Starter
Addicted Member
convert from on to another *again
ok I think MS over did it with the typedef deal its annoying HWND is a long int is it not...?
But I can't assign a HWND to a long...? and i can't make a conversion from HWND to char at least i haven't found away to yet... so how does one convert a HWND to a Char so that one might TextOut the HWND to a HDC yet another long ......
is there a place where i can find these answers with ease not MSDN it isn't there in any form i can find..... I'm headed to planet-source-code now maybe they have something....
Thanks
Magiaus
Visual Basic 6.0 SP5
Visual C++ 6.0 SP5
The only sovereign you can allow to rule you is reason.
-
Oct 26th, 2001, 08:10 PM
#2
PowerPoster
Can you do something like this:
(char)HWND
-
Oct 26th, 2001, 08:32 PM
#3
Thread Starter
Addicted Member
ok me newbie
i've seen ()TYPE allot but am clueless as to what it does or how it works can you explain and tell me if it is base C++ or brought in by a header
thanks
Magiaus
Visual Basic 6.0 SP5
Visual C++ 6.0 SP5
The only sovereign you can allow to rule you is reason.
-
Oct 27th, 2001, 05:54 AM
#4
Type casting is basic C/C++.
You can nearly always write (TYPE)variable, except in situations where the two types have got nothing to do with each other. Like when trying to convert a custom class to an int.
In C++, there are the cast operators:
static_cast, const_cast, dynamic_cast and reinterpret_cast.
Use static_cast<TYPE>(variable) for simple casts, like casting a float to an int. Use const_cast<TYPE> (const TYPE variable) to cast away the const. This is not recommended, because const variables are not supposed to be changed. Use dynamic_cast to cast pointers to classes of a class hirarchy, like dynamic_cast<baseclass*>(childclass*) (downcast), dynamic_cast<childclass*>(baseclass*) (upcast) or dynamic_cast<baseclass1*>(baseclass2*) (crosscast).
Use reinterpret_cast to cast pointes to completly other pointers, or pointers to long or long to pointers etc.
Your question:
HWND is defined as:
struct HWND__ { int unused; };
typedef HWND__ *HWND;
Therefor you have to explicitly cast HWND to int.
To convert a handle (HWND, HDC and all the others) to a string, use
char buf[8];
itoa(buf, (long)hwnd, 16); // hexadecimal output
or (C++ clean)
itoa(buf, reinterpret_cast<long>(hwnd), 16); // also hexadecimal
I like the many types windows has, because you always know what a variable stands for.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 29th, 2001, 12:11 PM
#5
Thread Starter
Addicted Member
well....
for the most part i agree with you that being able to put an hWnd into a HWND makes life easier to understand and keep track of but when you come from VB where its all long and you do converions the oh so hard way of
Code:
Dim lhWnd As Long, sMsg As String * 8
sMsg = Hex(lhWnd&) 'Hex
sMsg = lhWnd& 'base 10
this all seems a little bit confusing to you and you are going but why is it so much code...? not really I think it is pretty cool actualy
Magiaus
Visual Basic 6.0 SP5
Visual C++ 6.0 SP5
The only sovereign you can allow to rule you is reason.
-
Oct 29th, 2001, 01:06 PM
#6
What you gain in code size, you loose in speed - or even more. And it isn't that much code:
Code:
Dim lhWnd As Long, sMsg As String * 8
sMsg = Hex(lhWnd&) 'Hex
sMsg = lhWnd& 'base 10
is the same as
Code:
HWND lhWnd;
char sMsg[8];
itoa(sMsg, (int)lhWnd, 16); // hex
itoa(sMsg, (int)lhWnd, 10); // decimal
Not much more...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 29th, 2001, 04:00 PM
#7
Code:
LPSTR lpBuffer;
wsprintf(lpBuffer,"%d",hWnd);
-
Oct 29th, 2001, 04:44 PM
#8
you're producing an access violation megatron...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 29th, 2001, 06:22 PM
#9
It's not that hard to fix. Simply change LPSTR lpBuffer to char lpBuffer[6];
Try it now, and it should work.
Code:
char lpBuffer[6];
wsprintf(lpBuffer,"%d",hWnd);
-
Oct 29th, 2001, 06:49 PM
#10
I didnt have time to read all the post, but heres my 2 cents...
HWND is a dword (4 bytes)
char is a byte
HWND can not be converted to a byte.
-
Oct 30th, 2001, 07:57 AM
#11
HWND is 32-bit and therefor takes 8 chars at max megatron, not 6.
char buffer[9];
just to be sure (don't forget the \0)
And to make it nice:
char buffer[11];
sprintf(buffer, "0x%8x", (int)hWnd);
because hex numbers usually are in the format 0x00A34E25
byte: 2 (0xFF = 256)
word: 4 (0xFFFF = ~65000)
dword: 8 (0xFFFFFFFF)
qword (_int64): 16 (0xFFFFFFFFFFFFFFFF)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 30th, 2001, 09:23 AM
#12
transcendental analytic
why do you want to assign the HWND to a long int?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 30th, 2001, 09:29 AM
#13
maybe because he knew how to convert a long to a string...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 30th, 2001, 09:42 AM
#14
transcendental analytic
Okay... but I couldn't figure out his intentions
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 30th, 2001, 11:04 AM
#15
Actually its
Code:
Byte = 1
Word = 2
DWord = 4
QWord = 8 (i think)
-
Oct 30th, 2001, 11:13 AM
#16
transcendental analytic
I think CornedBee expressed hex digits, not bytes
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 30th, 2001, 11:34 AM
#17
yes, exactly. You need 2 hex digits per byte + 1 for the \0
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 30th, 2001, 03:46 PM
#18
Originally posted by CornedBee
HWND is 32-bit and therefor takes 8 chars at max megatron,
Nope.
A HWND is a special datatype that is used to identify windows. The lowest legal value it can have is 0 (1 character, 2 including null terminator). The highest legal value it can have is 65535 (5 chars, 6 including the null), therefor, we only need room for 6.
-
Oct 31st, 2001, 01:14 PM
#19
Monday Morning Lunatic
HWND is a pointer to a struct (when you use STRICT, which you should).
Either way, it's 4 bytes on a 32-bit system.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 31st, 2001, 01:45 PM
#20
transcendental analytic
HWND is a struct if strict, i think while a void pointer if not.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 1st, 2001, 02:23 AM
#21
Thread Starter
Addicted Member
because
I'm learning how to program in C++ and well Hello World is boring crap I preffer to make API spys its how I got my start in VB a long long time ago and it is good for data manipulation and getting strings down which from what i hear is the worst part of C++ since I understand that a pointer is just that a pointer. so I wanted to convert the HWND to a long then to a string so I can use it with TextOut or DrawText or DrawTextEx funny thing is it won't write out numbers only charecter arrays 
anyway these guys should me how to do it and i been gone for a few days so here is what I got.
I used what cornedbee gave me in his first post to the thread and it converted and TextOut was thrilled to finally have the deguger let it run and now I have a problem with it aking funny charecters wghen it does TextOut but I think this is just because I don't send an UpdateWindow(hwnd) at the start of my tmrProc to clear the window (testing theroy now) nope it still makes garbage the handle is correct mind you i checked that with my spy from vb but there are leading and trailing funny chars.
project attached
oh i been gone for a few days because I had rented Chrono Cross and I had to kick the SH*T out of it before it goes back tomorrow, I did it was cool much more fun than reading a book
Magiaus
Visual Basic 6.0 SP5
Visual C++ 6.0 SP5
The only sovereign you can allow to rule you is reason.
-
Nov 1st, 2001, 02:35 AM
#22
Thread Starter
Addicted Member
umm i think this is correct
1 Byte = 00 to FF = 0 to 255 (Byte in vb)
(2 Bytes)Word = 00 00 to FF FF = 0 to 65535 ( int in vb)
(4 Bytes)DWord =00 00 00 00 to FF FF FF FF = 0 to 4294967295 (long in vb)
(8 Bytes)QWord = 00 00 00 00 00 00 00 00 to FF FF FF FF FF FF FF FF = 0 to 18446744073709551615 (the big bad float)
that is unsigned mind you split it in half if you want to have -+ signed numbers
i'm not sure but i think those are the corrrect valuse and memory usages
i could talk about conversion to hex and binary and oct and dec but i find that to be to much typing and then there is always how each word get reversed in memory ugghhh
Magiaus
Visual Basic 6.0 SP5
Visual C++ 6.0 SP5
The only sovereign you can allow to rule you is reason.
-
Nov 4th, 2001, 12:50 PM
#23
sStr was too small. You made it
char sStr[20];
but the sentence "The window's hWnd is: " alone takes 23 characters. I think this is the reason for the failure. (At least when I corrected it by making sStr 40 characters, it worked, even after a cross-check with Spy++).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 7th, 2001, 08:07 PM
#24
Addicted Member
Best way to be sure
Code:
#include <windows.h>
#include <iostream.h>
void main()
{
cout << "HWND : " << sizeof(HWND) << endl
<< "MSG : " << sizeof(MSG) << endl
<< "WORD : " << sizeof(WORD) << endl
<< "DWORD: " << sizeof(DWORD) << endl
<< "BYTE : " << sizeof(BYTE) << endl;
}
/*
output:
HWND : 4
MSG : 28
WORD : 2
DWORD: 4
BYTE : 1
*/
-
Nov 7th, 2001, 11:35 PM
#25
Thread Starter
Addicted Member
CornedBee you were right. It was because the string truncated.
Thanks
Magiaus
Visual Basic 6.0 SP5
Visual C++ 6.0 SP5
The only sovereign you can allow to rule you is reason.
-
Nov 8th, 2001, 09:41 AM
#26
david, we talked about how many hex digits you need to represent the value. You need 2 per byte + '\0'. MSG is a struct, so it can't be outputted that way.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|