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)