Solved

A window created in a dll?

Posted on 2007-11-14
2
276 Views
Last Modified: 2013-11-20
Hi,

Is it possible in a C++ dll to create a window, then give control of that window to the application which uses the dll? Something like:

HWND* CMyDll::DoSomething()
{
    HWND* hwnd = new HWND;
    CreateWindow(hwnd);
    // Set it up for them - where would the window procedure sit etc? Like where do mouse click
    // handlers go? In my dll?

    return hwnd; // now they can control it, and are responsible for deleting it when done.
}

To make things more complex, the group that wants to use this window is using C#, but they are certain that this is possible. Any ideas if it is?

Thanks
0
Comment
Question by:DJ_AM_Juicebox
2 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 20280872
the short answer is: yes, you can, as far the process that is controlling the window (C#) is the same of the window itself (c++ dll).
in the C# side, the team can create a System.Windows.Forms.Control.FromHandle() control, using the hWnd.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 20281069
Actually, you can just use



HWND CMyDll::DoSomething()
{
    HWND hwnd;
  g_hInst = hInstance;

  WndClass.cbSize        = sizeof(WNDCLASSEX);
  WndClass.style         = NULL;
  WndClass.lpfnWndProc   = WndProc;
  WndClass.cbClsExtra    = 0;
  WndClass.cbWndExtra    = 0;
  WndClass.hInstance     = g_hInst;
  WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  WndClass.lpszMenuName  = NULL;
  WndClass.lpszClassName = g_szClassName;
  WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

  if(!RegisterClassEx(&WndClass))
  {
     MessageBox(0, "Window Registration Failed!", "Error!",
        MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
     return 0;
  }

  hwnd = CreateWindowEx(
     WS_EX_CLIENTEDGE,
     g_szClassName,
     "The title of my window",
     WS_OVERLAPPEDWINDOW,
     CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
     NULL, NULL, g_hInst, NULL);


    return hwnd;
}

The WndProc should reside inthe DLL (you won't get a pointer to it otherwise) and there is no need to 'delete' the Window handle - 'DestroyWindow(hwnd);' will take care of that, regardless where that is done.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sumDigits challenge 9 147
Doc'in system (example?) BA 7 107
Capture logon name 13 70
designing in object programming 12 75
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question