Thread: What's the proper sytax, and initialization of HWND *pHwnd[]={}; ?

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    15

    Question What's the proper sytax, and initialization of HWND *pHwnd[]={}; ?

    Hello, and good day to whomever reads this.

    I am trying to learn the proper syntax, usage, and initialization of
    "HWND *pHwnd[]={...};".I am trying to create a loop which creates 3 windows. Then it assigns
    the returned HWND to an HWND variable pointed to by "HWND *pHwnd[]={..};". So I do not want
    pHwnd to point to anything other than what it's been initialized with already, rather I want
    to know how to assign the return from CreatWindowEx() to the variables pHwnd points to.


    I am not looking for a work-around unless there is no other way. I am trying to learn the
    about C++ semantics in Win32. Also I should mention that I am new to C++ and Win32.


    The code below helps to depict what - approximately - I'm trying to do. Can you show what is wrong?



    Code:
     LPCTSTR lpClassName[]=  {"Static","Static","Static"};
        LPCTSTR lpWindowName[]= {"Win1",  "Win2",  "Win3"};
        
        int x[]      ={0, 0,  0};
        int y[]      ={50,100,150};
        int nWidth[] ={50,50, 50};
        int nHeight[]={20,20, 20};    
        
        HMENU hMenu[3];
    
    
        HWND hWin1, hWin2, hWin3;
        HWND *pHwnd[]={&hWin1, &hWin2, &hWin3};
    
    
         switch (message)                  /* handle the messages */
         {
            
            
            
    
    
            case WM_CREATE:
    
                 for(int makeWin=0; makeWin < 3; makeWin++)
                 {           
                     hMenu[makeWin]=(HMENU)makeWin;
          
                     pHwnd[makeWin]=CreateWindowEx(0,
                                                   lpClassName[makeWin],
                                                   lpWindowName[makeWin],
                                                   WS_CHILD|WS_VISIBLE,
                                                   x[makeWin],
                                                   y[makeWin],
                                                   nWidth[makeWin],
                                                   nHeight[makeWin],
                                                   hwnd,
                                                   hMenu[makeWin],
                                                   NULL,
                                                   NULL
                                                   );  
                     
                 }
    
    
            break;


    Thanks to anyone who reads this, and a big thanks to anyone who helps!
    Last edited by JimmyJones; 08-03-2008 at 09:03 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I have no idea why you need both the HWND's and the pHwnd array. Seems pointless.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    Quote Originally Posted by MacGyver View Post
    I have no idea why you need both the HWND's and the pHwnd array. Seems pointless.
    Well, I want to assign each returned HWND to a specific and named HWND
    rather than a pointer because a specific and named identifier is more
    comprehensible than a pointer and subscript. I need a pointer to
    receive the returned values in a loop.


    Also I just want to know how to do it... I think the answer would be
    enlightening.



    Thank you MacGyver for your input.
    Last edited by JimmyJones; 08-03-2008 at 08:52 PM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Bleh.... You do not understand what you're doing.

    • CreateWindowEx() does not return an HWND *. It returns a HWND. So your necessity for a pointer seems.... pointless.
    • You are overwriting where your array of pointers point to anyway when you assign the return of CreateWindowEx() to them. Thus, there is no point whatsoever in initializing them first to point to regular HWND variables. This means you are never using your regular HWND variables.
    • There is nothing "more comprehensible" about using variables in the way you are using them. Making three separate HWND variables in that manner just screams you should be using an array.


    I think you need to really read up on pointers and such before getting thoroughly confused on Windows API programming. The Windows API is a monster if you are not familiar with the langauge.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    Sir I just want to know how to do it. Do - you - know how? I'm confused why you just don't demonstrate with an example.-----But, that's allright, perhaps someone else will help me. Thank very much anyway.


    But if you do want to help then let me just say that the reason why I did not cast the returned HWND was because
    - somehow - I want the return to be passed to the HWND variable, and not the pointer, andI need the pointer to
    receive all the returns(which can be 10, 20, 100, etc..) in a loop.


    Thank you again MacGyver, and thank you to anyone who has ever helped anybody on this forum. You do
    not need to give me an answer to get a thank you. I thank you just for helping anybody at all.
    I will find an answer eventually. THANK YOU ALL!!!
    Last edited by JimmyJones; 08-03-2008 at 08:39 PM.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I'm not demonstrating anything because your question makes no sense. You're asking the totally wrong question because you have no idea what you are doing.

    Get it now?

    Since you seem to still think you know what you're doing, I'll let the others here have a crack at this topic per your request.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by JimmyJones View Post
    I want the return to be passed to the HWND variable, and not the pointer, andI need the pointer to
    receive all the returns(which can be 10, 20, 100, etc..) in a loop.
    I'm pretty sure this sentence means that you do not know the definition of "pointer" or of "loop". Whichever one you feel less confident about, look it up.

    But if you want to do it n times, make a for loop (you've got that part) -- have an array of HWND (since that's what CreateWindowEx returns) -- and put the results of calling the function in the array (you more or less have that part). If you're going to be programmatic about the window names (window1, window2, etc.) then you should build that name string inside the for loop; sprintf can do this sort of thing no problem.

    I don't really see the need for the HMENU array -- you can just pop that expression right where it goes in your CreateWindow call.

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    What makes the difference whether you use hWnd1, hWnd2 or hWnd[0], hWnd[1] ???
    It is the same, isn't it !?

    Define an array hWnd[3] and ...
    Code:
             for(int i=0; i < 3; i++)
             {           
                 hWnd[i]=CreateWindowEx(0,
                                       TEXT("static"),
                                       lpWindowName[i],
                                       WS_CHILD|WS_VISIBLE,
                                       x[i], y[i], nWidth[i], nHeight[i],
                                       hwnd, (HMENU) i, hInst, NULL); 
             }
    That's the way it should/could be.

    First you should learn C/C++ as well before you start with Win32 API.


    Greetz
    Last edited by Greenhorn__; 08-03-2008 at 10:24 PM.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    Hey, thank you tabstop buddy for the help. Although my objective here was
    not to assign the returns to the array, but to what the array points to.
    I think a pointer and subscript, in many instances, can become confusing
    if the array becomes to large or if you have many arrays. Of course one
    could just use a #define statement to clarify the array like so:


    Code:
             
    #define HWNDAPPLES1 1
    #define HWNDORANGES2 2
    #define HWNDPEACHES3 3
    //...#define HWNDUNIQUENAME 100 
    
        
        char Info[11]; 
       
        int x[]      ={0, 0,  0};
        int y[]      ={50,100,150};
        int nWidth[] ={50,50, 50};
        int nHeight[]={20,20, 20};    
    
        HWND phWnd[3];
    
    
         switch (message)                  /* handle the messages */
         {
            case WM_CREATE:
    
                 for(int makeWin=0; makeWin < 3; makeWin++)
                 {           
                     hMenu[makeWin]=(HMENU)makeWin;
          
                     pHwnd[makeWin]=CreateWindowEx(0,
                                                   "Edit",
                                                   "",
                                                   WS_CHILD|WS_VISIBLE,
                                                   x[makeWin],
                                                   y[makeWin],
                                                   nWidth[makeWin],
                                                   nHeight[makeWin],
                                                   hwnd,
                                                   (HMENU)makeWin+1,
                                                   NULL,
                                                   NULL
                                                   );  
                     
                 }
    
    
            break;
    
    
           case WM_COMMAND:
                if(LOWORD(wParam)==GetDlgCtrlID(pHwnd[HWNDAPPLES1]) )
                {
                    GetDlgItemText(hwnd, GetDlgCtrlID(pHwnd[HWNDAPPLES1]), Info, 10); 
                }
                
                if(LOWORD(wParam)==GetDlgCtrlID(pHwnd[HWNDORANGES2]) )
                {
                    GetDlgItemText(hwnd, GetDlgCtrlID(pHwnd[HWNDORANGES2]), Info, 10); 
                }
          
           break;





    This approach of identifying the contents of an array with a unique constant is fine,
    but I still would like to know an approach simular to the one I have mentioned.



    As a novice - it seems that if one can do this :

    Code:
    HWND hWnd = CreateWindowEx(...);


    Then one ought to be able to do something --- approximate --- to this:
    (keep in mind, good people, that I don't know the proper syntax,
    and that was the very point to this post)


    Code:
    HWND hWnd;
    
    HWND *phWnd=&hWnd;
    
    
    phWnd = CreateWindowEx(...);

    It seems to me that the contents of phWnd(namely hWnd) needs to receive
    a value in order to be useful. For Instance a variable of type int is virtually useless
    unless it has at least some value. Therefore if in the C++ language it is possible to
    assign hWnd to phWnd then it must be possible to assign a value to
    this very samehWnd. I simply want to know how to assign a value to hWnd in
    this manner - this is the main purpose of this post.


    Also, as I said before it seems that a good programmer would want to make their
    variables as clear as possible. It seems to me, as a novice, that an array -- having
    only one name and subscript which is unique through it's number -- would not be
    clear enough if the array were to large or if there were many such arrays. It seems
    that it can become necessary to give the elements of such an array a - unique name -
    rather than just refer to the elements only by number.


    Thank you Greenhorn__ for your reply.



    THANK you to all who read this and even more so to those who answer my question.
    Last edited by JimmyJones; 08-04-2008 at 11:24 PM.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Good grief, what is wrong with you? A HWND is NOT NOT NOT NOT a pointer to an HWND. Drill it through your head. Any variable of a given type is NOT the same as a pointer to such a type. What is with your cast from CreateWindowEx()? As I told you before, CreateWindowEx() returns a HWND, NOT a pointer to a HWND. Casting the return ruins it in this context.

    You need to go review pointers. You have no idea how they work if you think you can write code like that.

  11. #11
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    Quote Originally Posted by MacGyver View Post
    What is with your cast from CreateWindowEx()?



    MacGyver, that was simply a typo. What I have written here definitely makes sense.
    If you recall in what I have written I am simply asking for something -- similar, and not exact.
    Therefore it is to be expected that I will give approximate examples, especially considering
    that I am asking as a novice. Furthermore why do you bother to criticize my grammer
    , and yet you will not give me an approximate example to what I'm asking. What I am asking
    for makes sense. Please use your imagination. I think you don't understand what I'm asking
    because your not thinking broadly enough or perhaps you are just careless...

    I only want an approximate example. Or perhaps if what I am asking really doesn't make sense
    then can someone tell me why? Read through what I have written

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What you're asking for makes no sense.
    Again, as MacGyver implies, your answer lies in understanding how pointers work. Go learn them and you will probably have your answer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    Look people, if you read back your own post you will discover that your focus was on
    criticizing, and pointing out how stupid I am rather than on helping. You say that
    I need to learn C++ and pointers, but is that not the reason why people post at all?
    Because they - don't know - the answer to their question... Of course I need to learn
    pointers and C++ more thoroughly, that would obviously be the reason why I'm posting...
    so that I can learn at least one simple question from you. The fact that you say that
    what I have written makes no sense-- itself makes no sense -- because I either have
    - asked or implied:
    ---------------------------------------------------------------------------------------
    1) How can one pass the value returned by CreateWindowEx() to what pHwnd points to,
    which is hWnd?

    I have asked this under the impression that the hWnd that phWnd points to needs to
    retain a value in order to be useful, so there must be a way to indirectly give hWnd
    the return from CreateWindowEx().

    2)Is it possible in ANY way to indirectly pass the return from
    CreateWindowEx() through a HWND pointer and to what the pointer points to, which is hWnd?

    This is basically a reiteration of Q1. Also note that I use the word ANY,
    and this is why I am so baffled by the obtusity of most all of these responses... How
    can C++ not offer a way to do at least something - approximate or similar - to what I
    just asked?

    ------
    IMPLIED:

    3) If it is not possible to do what I have asked in any way than can you please ellaborate?
    Because it seems hard to grasp that there is not even a work-around.



    4)If what I have asked is not possible in ANY way then how do programmers easily
    distinguish the elements of large arrays from one another?

    It seems that to just refer to an element by it's number would decrease the comprehensibility
    of their code.


    5)Try to focus on why a novice would even question if it is possible to pass the return
    from CreateWindowEX() to hWnd through phWnd, or at least something similar.

    --------------------------------------------------------------------------------------


    The closest thing to a sufficient answer in this thread is from what Greenhorn__ wrote:

    Quote Originally Posted by Greenhorn__ View Post
    What makes the difference whether you use hWnd1, hWnd2 or hWnd[0], hWnd[1] ???
    It is the same, isn't it !?

    Define an array hWnd[3] and ...
    Code:
             for(int i=0; i < 3; i++)
             {           
                 hWnd[i]=CreateWindowEx(0,
                                       TEXT("static"),
                                       lpWindowName[i],
                                       WS_CHILD|WS_VISIBLE,
                                       x[i], y[i], nWidth[i], nHeight[i],
                                       hwnd, (HMENU) i, hInst, NULL); 
             }
    That's the way it should/could be.

    First you should learn C/C++ as well before you start with Win32 API.


    Greetz

    Although the "First you should learn C/C++ as well before you start with Win32 API"
    was redundant his answer at least gave me the impression that his way was the only
    right way, and I guess this was helpful.

    However all these responses clearly left something wanting.
    Ya know I don't even want a sufficient answer now, seeing as that I no doubt will
    not get one. I just want to inform you respondents that the way in which you
    responded was poor, with all the critism and lack of perception to see my simple
    problem. And MacGyver's remarks were flat out inappropriate.


    On the other hand I'm sure that you guys have put more effort into answering other
    posts at one time or another, considering that some of you have posted thousands of
    times. On this hand I say kudos to you all for the valiant effort!


    Good Day!!!
    Last edited by JimmyJones; 08-04-2008 at 07:53 AM.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. The correct way would be to dereference the pHwnd array, e.g.
    Code:
     *pHwnd[makeWin]=CreateWindowEx(0,
    ...
    However, it is also correct to point out that there's absolutely no need to use pointers here - it just serves to confuse and complicate things.


    2) yes. As above, dereference the pointer to store the return value. This is more purposeful, as you may find yourself needing to fill in a passed in HWnd parameter to a function, in which case a pointer to the variable you want to fill in would be the right thing.


    3) N/A as this assumes a "no" to Q2.
    4) As above, but I don't quite understand what you are asking for, really.

    5) I think it was pointed out that this is a bad idea, which I think is a correct response - you do not need a further indirection here, so there's no reason to strive for one. This may of course be caused by your example being too trivial or some such, but you really have to show WHY you need something that complicated for something that otherwise should be a simple situation.

    By the way:
    [code
    for(int makeWin=0; makeWin < 4; makeWin++)
    [/code]
    will walk outside your array by one element.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by JimmyJones View Post
    Look people, if you read back your own post you will discover that your focus was on
    criticizing, and pointing out how stupid I am rather than on helping. You say that
    I need to learn C++ and pointers, but is that not the reason why people post at all?
    Because they - don't know - the answer to their question... Of course I need to learn
    pointers and C++ more thoroughly, that would obviously be the reason why I'm posting...
    so that I can learn at least one simple question from you. The fact that you say that
    what I have written makes no sense-- itself makes no sense -- because I either have
    - asked or implied:
    Well, as people have said, your questions below are directly related to the knowledge of a specific thing in C++. A feature. Knowing that feature will solve ALL of your questions!
    It can't get much easier, can it?

    1) How can one pass the value returned by CreateWindowEx() to what pHwnd points to,
    which is hWnd?

    I have asked this under the impression that the hWnd that phWnd points to needs to
    retain a value in order to be useful, so there must be a way to indirectly give hWnd
    the return from CreateWindowEx().
    It is possible, and because you are using a pointer, then learning how to properly using pointers will directly give you the answer. There's no real need to ask even!

    2)Is it possible in ANY way to indirectly pass the return from
    CreateWindowEx() through a HWND pointer and to what the pointer points to, which is hWnd?

    This is basically a reiteration of Q1. Also note that I use the word ANY,
    and this is why I am so baffled by the obtusity of most all of these responses... How
    can C++ not offer a way to do at least something - approximate or similar - to what I
    just asked?
    To this, the same answer as above.
    Usually, when someone asks for something, we can point them in the right direction. After all, you are the one learning the language and therefore studying what we point you to is usually a good way to learn.
    Granted, if you still don't understand after you've studied what we told you, then you can return and ask again, stating that you are confused, that you did not understand or that you still fail to see a solution.
    The solution to this is a very basic rule or syntax about using pointers. It's not really "hidden" somewhere in the concept even. It should be fairly obvious to you once you have an understanding of how pointers work.

    3) If it is not possible to do what I have asked in any way than can you please ellaborate?
    Because it seems hard to grasp that there is not even a work-around.
    It is indeed possible, since you wish to know.

    4)[b]If what I have asked is not possible in ANY way then how do programmers easily
    distinguish the elements of large arrays from one another?
    It seems that to just refer to an element by it's number would decrease the comprehensibility
    of their code.
    OK, so it is a valid question at times. And the answer to this can be many-fold.
    Using C++, you can use std::map for example, to map a "name" to a certain object/element/variable.
    Of course, you can also use references (C++ only), or pointers (C/C++) to pick out a certain element in an array.
    But it all depends on what you are trying to do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed