0

I'm trying to make a function that scales the controls of a window with its parent window when I resize that window, in order to accomplish this I embedded CreateWindowEx() into my own function createscalingwindow

int createscalingwindow (HWND cswpara0,DWORD cswpara1,const CHAR* cswpara2,
const CHAR* cswpara3,DWORD cswpara4,int cswpara5,int cswpara6,int cswpara7,
int cswpara8,HWND cswpara9,HMENU cswpara10,HINSTANCE cswpara11,LPVOID cswpara12)
              {
               cswpara0 = CreateWindowEx (cswpara1, cswpara2, cswpara3, cswpara4,
                                          cswpara5,cswpara6,cswpara7,cswpara8,
                                         cswpara9,cswpara10,cswpara11,cswpara12);
               return 0;
              }

After changing this code in winmain

    mainwin = CreateWindowEx(
       0,                   
       mainclassname,         
       "Mafia Online",       
       WS_OVERLAPPED|WS_CAPTION|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX
      |WS_SYSMENU, 
       CW_USEDEFAULT,       
       CW_USEDEFAULT,       
       544,                 
       375,                 
       NULL,        
       playmenu,                
       hThisInstance,       
       NULL                 
       );

To the following code

createscalingwindow (mainwin, 0, mainclassname, "Mafia Online",
WS_OVERLAPPED|WS_CAPTION|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_SYSMENU, 
CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, NULL, playmenu, hThisInstance, NULL);
ShowWindow (mainwin, nCmdShow);

The main window no longer shows up at all. Calling GetLastError() after the embedded CreateWindowEx() returns error_success.

After examining both variants of the program in ollydbg, I noticed something strange, the execution of the program using the prototype doesn't work the way I'd expect. The program calls to CreateWindowEx and then returns from the call to an entirely different destination.

Here's how I would expect the program to execute (this is the program that doesn't use createscalingwindow): (I am not allowed to post images as a new user, so if a mod or someone could fix these for me that would be nice.) http://s8.postimage.org/qo8tqt5h1/mainnormal.png

Here's how it executes: http://s8.postimage.org/fa8ytr7qt/mainweird.png

Any effort you put forth into helping me is appreciated, so thanks in advance.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
user1934608
  • 405
  • 1
  • 4
  • 9
  • You need to pass `cswpara0` by reference. –  Dec 28 '12 at 14:49
  • @aleguna HWND is a pointer to begin with. – johnathan Dec 28 '12 at 14:50
  • @johnathon, it doesn't matter in `createscalingwindow` he is modifying a local copy of the HWND which is why it has no effect on callers copy. –  Dec 28 '12 at 14:51
  • Raymond Chen's edit gives away the answer in the question title. Very nice. @johnathon: that doesn't matter, the caller is calling `ShowWindow` on a garbage handle. – DCoder Dec 28 '12 at 14:51
  • @aleguna I'm actually a super noob at c++, how do I do that if I may ask? – user1934608 Dec 28 '12 at 14:51
  • @user1934608 HWND &cswpara0 as the paramater – johnathan Dec 28 '12 at 14:52
  • @aleguna and DCoder yea, I need more coffee. – johnathan Dec 28 '12 at 14:54
  • [When I change a parameter inside a function, does it change for the caller, too?](http://stackoverflow.com/questions/1698660/when-i-change-a-parameter-inside-a-function-does-it-change-for-the-caller-too). – DCoder Dec 28 '12 at 14:54

1 Answers1

1

By default, function arguments are passed by value, meaning that the function gets its own copy of the variable, and not a reference to the caller's variable. Any modifications made to the local copy will not affect whatever was passed to the function.

If you want a reference, then pass by reference:

int createscalingwindow (HWND & cswpara0 /* etc. */)
                              ^

Now any modifications you make to the function argument will also modify the caller's variable.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644