5

I'm writing a Windows application in Visual C++ 2008 and I want to embed the calculator (calc.exe) that comes with Windows in it. Does anyone know if that is possible, and if it is, can you give me hints on how I could achieve that?

bodacydo
  • 75,521
  • 93
  • 229
  • 319

2 Answers2

7

Yes, it's possible to embed the calc in your own application but it will still run in it's own process space. There may also be some restrictions imposed by UAC but that will depend on how calc is launched. All you need to do is change the parent of the main calc window and change it's style to WS_CHILD.

void EmbedCalc(HWND hWnd)
{
    HWND calcHwnd = FindWindow(L"CalcFrame", NULL);
    if(calcHwnd != NULL)
    {
        // Change the parent so the calc window belongs to our apps main window 
        SetParent(calcHwnd, hWnd);

        // Update the style so the calc window is embedded in our main window
        SetWindowLong(calcHwnd, GWL_STYLE, GetWindowLong(calcHwnd, GWL_STYLE) | WS_CHILD);

        // We need to update the position as well since changing the parent does not
        // adjust it automatically.
        SetWindowPos(calcHwnd, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
    }
}
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
  • 2
    It doesn't have to run in a separate process. You could use Spy++ to get an idea of the windowing structure and then you could use DLL injection (http://en.wikipedia.org/wiki/DLL_injection) to load arbitrary code and behavior into Calc, hooking window ownership and parenting. – Scott Jones May 20 '13 at 02:22
  • Check the comments on the question. The request is to embed Calc in the window of the OP's application. No mention is made of adding functionality to Calc. – Captain Obvlious May 20 '13 at 02:28
  • Yes, that's what I was responding to - "embed the window of calculator into my own window". The process question is something different. – Scott Jones May 20 '13 at 04:19
3

Microsoft has various technologies to support embedding, most famously OLE which is a COM-based technology. This is, for example, how you can embed an Excel spreadsheet in your application. However, I'm fairly certain that calc does not implement any of the required interfaces for that to happen.

So that only leaves you with hacky solutions, like trying to launch it yourself and play games with the window hierarchy, or try to present it to users and then copy the results out through the clipboard, etc. This is all technically possible, but not a good idea. In fact, it's probably more difficult than just writing your own calculator app... depending on what you want to to enable users to do. If you explain why you want to do this someone may have some better solutions to propose.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222