0

I have the following main.cpp for a C++ Desktop Application (Console or DLL) that creates an Excel Application:

#include "stdafx.h"

#include <comutil.h>
#include <stdio.h>

#pragma comment(lib, "comsuppw.lib")
#pragma comment(lib, "kernel32.lib")

#import \
"C:\Program Files (x86)\Microsoft Office\root\VFS\ProgramFilesCommonX86\Microsoft Shared\OFFICE16\mso.dll" \
rename("DocumentProperties", "DocumentPropertiesXL") \
rename("RGB", "RBGXL")

#import \
"C:\Program Files (x86)\Microsoft Office\root\VFS\ProgramFilesCommonX86\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"

#import "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" \
rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") \
rename("DocumentProperties", "DocumentPropertiesXL") \
rename("ReplaceText", "ReplaceTextXL") \
rename("CopyFile", "CopyFileXL") \
exclude("IFont", "IPicture") no_dual_interfaces

using namespace std;

int main()
{
    Excel::_ApplicationPtr XL;
    try
    {
        //Initialise COM interface
        CoInitialize(NULL);
        //Start the Excel Application
        XL.CreateInstance(L"Excel.Application");
        //Make the Excel Application visible
        XL->Visible = true;
    }
    //If a communication error is thrown, catch it and complain
    catch (_com_error &error)
    {
        cout << "COM error" << endl;
    }
    Sleep(2000);
    return 0;
}

the thing is that the new Excel process quits as soon as the main function has completed (I can see the window for 2 seconds thanks to the Sleep(2000);).

I have found that using exit(1); prevents the Excel App from quitting in the end. But is there another way to "detach/disconnect" the child process from the parent console process ? I have found this for UNIX but I fail making it work in my case: Detach child process from parent

Plus, I just found that if make a DLL (not a Console Application), using exit(1); quits the Excel Application that calls the main function of the DLL before starting the new Application. This is not what I'd like to do. (it even seems the process becomes a zombie)

hymced
  • 570
  • 5
  • 19
  • 1
    Try to add `XL.AddRef();` after `XL->Visible = true;`. – zett42 Nov 22 '17 at 16:43
  • @zett42 OK it does the job! thank you. But when I quit this new instance and also the parent instance of Excel, I still have a Microsoft Excel processus running in the Windows Task Manager... So it might not be the perfect solution. Could you or anyone explain to me why incrementing the reference count for the IUnknown interface has this effect ? I am eagger to understand more about this ! :) – hymced Nov 23 '17 at 09:28
  • COM objects get deleted only when the reference count reaches zero. By calling `AddRef()` without a matching `Release()`, it will never reach zero, so the process won't be terminated. It thinks you are still using it. I have no better idea how to deal with this, though. What are you actually trying to achieve by creating an Excel COM object? If you give some more details, we might be able to find another solution. – zett42 Nov 23 '17 at 16:02
  • @zett42 It is my first attempt to interface Excel Object Model with C++. For now I would just like to open a new Application independent from caller. I noticed that if I add a Workbook to my new Application, then close it (back to an Application with a Workbooks collection's count = 0), the new Application seems correctly independent : stays open + both processes quit properly. I would just like to end up with the same behavior without using a workbook, just for fun and to learn :) – hymced Nov 23 '17 at 16:17

0 Answers0