Solved

A window created in a dll?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java ^ examples 8 62
noX challenge 17 91
Problem to event 3 78
Remove Password Protection from pdf (with password) using an command line executable 5 27
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

911 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

22 Experts available now in Live!

Get 1:1 Help Now