Solved

A window created in a dll?

Posted on 2007-11-14
2
260 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now