Results 1 to 26 of 26

Thread: convert from on to another *again

  1. #1

    Thread Starter
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    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.

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Can you do something like this:

    (char)HWND
    Baaaaaaaaah

  3. #3

    Thread Starter
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    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.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  5. #5

    Thread Starter
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    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.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  7. #7
    Megatron
    Guest
    Code:
    LPSTR lpBuffer;
    wsprintf(lpBuffer,"%d",hWnd);

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  9. #9
    Megatron
    Guest
    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);

  10. #10
    ChimpFace9000
    Guest
    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.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  15. #15
    ChimpFace9000
    Guest
    Actually its
    Code:
    Byte = 1
    Word = 2
    DWord = 4
    QWord = 8 (i think)

  16. #16
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  17. #17
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  18. #18
    Megatron
    Guest
    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.

  19. #19
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  20. #20
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  21. #21

    Thread Starter
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    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.

  22. #22

    Thread Starter
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191

    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.

  23. #23
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  24. #24
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151
    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
    
      */

  25. #25

    Thread Starter
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191
    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.

  26. #26
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width