Link to home
Start Free TrialLog in
Avatar of afzalj
afzalj

asked on

Passing data to window on create

Experts,

      I have a WIN32 CONSOLE Application.

      I have to create a window and pass some user data so that the WM_CREATE message
      can use it. The data is a pointer to linked-list. Once passed, the window proc must
      be able to use it wherever and whenever.

      There may be many windows created of the same class but with different pointers passed
      (of the same type), so that each window can use the pointer it was given.
      Note that the application is a console app.

      How can I do this ? Code in C plz,

Thanx, Afzal.

Avatar of nietod
nietod

The last parameter to CreateWindow() (or CreateWindowEx()) is a 32 bit value that may be used for any purpose you want.  This value is passed with the WM_CREATE message.  You may use this to pass whatever you want.  So you can use it to pass a pointer to your linked list.

continues
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of afzalj

ASKER

I thought it was a parameter to CREATESTRUCT ? Does it matter what  I give it ?
Avatar of afzalj

ASKER

And what about the HINSTANCE ? Is it NULL ? It's a console app so what do i use as HINSTANCE ?
Itrs hard to give you much in the way of code, since we don't know any of the details of your case, but creating the window might look like

struct ListNode *ListHeadPtr;

HWND WndHnd = CreateWindow("CLASSNAME","WindowTitle", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100,150,400,300,NULL,NULL, GetModuleHandle(NULL), ListHeadPtr);

See how it passes the linke list head pointer in the last parameter.

Then to obtain the linked list in the WM_CREATE message, in the window procedure do

case WM_CREATE:
{
   CREATESTRUCT *CrtPrmPtr = (CREATESTRUCT *) lParam;
   struct ListNode *HeadPtr = (struct ListNode *) CrtPrmPtr->lpCreateParams;
}

Let me know if you have any questions.
>> I thought it was a parameter to CREATESTRUCT ?
>> Does it matter what  I give it ?
You pass whatever 32 bit value you want to CreateWindow().  When WM_CREATE (also WM_NCCREATE) is sent, the long parameter points to a CREATESTRUCT and in the first member of the CREATESTRUCT (lpCreateParams) the 32 bit value you specificed will be found.

>> And what about the HINSTANCE ? Is it NULL
You can get the current HINSTANCE using "GetModuleHandle(NULL)".  I showed that above.  That is often what I do since it is a liuttle more reliable than storing the module handle in a global variable that could become corrupted.
I've never created a regular window from a console application.  I don't know if you will experience any problems with that.  You may need to write a message loop for the window, but I'm not sure.  The console window itself has some sort of message loop, possibly running in a different thread.   If you write your own message loop, I'm not sure what will happen to the console.  So the answer I gave you was really on how to send parameters to the WM_CREATE message.  That works for any type of application.  The issue of how to create a window in a console application is seperate from that.  I don't know for sure how to do that, or even if it can be done.   However the reverse is easy and I've done that.  That is, you can create a GUI application that creates 1 or more console windows and (possibly) 1 or more GUI windows.  Your application can use AllocConsole() to create console windows.  
Avatar of afzalj

ASKER

Thanx !!!
Avatar of afzalj

ASKER

Thanx !!!