excel automation, hprocess is not closing
-
hi all, I found an article about I have an excel automation dos based application . I take an article, Automate excel from c++ without using mfc or #import (Q216686) as an example. then based on another article Automate excel and then know the use closed it(Q192348) I changed my code again. actually, i added,
// wait until the user close the application HWND hWnd; char buf[1024]; hWnd = ::FindWindow("XLMain",NULL); if(NULL==hWnd) { long lErr = GetLastError(); sprintf(buf, "FindWindow Error code = %d",lErr); AfxMessageBox(buf); } DWORD pid; DWORD dThread; DWORD dwReason; dThread = ::GetWindowThreadProcessId(hWnd,&pid); HANDLE hProcess; hProcess = ::OpenProcess(SYNCHRONIZE|PROCESS_ALL_ACCESS,TRUE,pid); dwReason = ::WaitForSingleObject(hProcess,INFINITE); ::CloseHandle(hProcess);
everything is fine except dwReason = ::WaitForSingleObject(hProcess,INFINITE); is waiting forever because in windows task manager's processes tab, you will find EXCEL.EXE eventhough you have closed ms excel application. if you command the line dwReason = ::WaitForSingleObject(hProcess,INFINITE); you will not find EXCEL.EXE in windows task manager's processes tab. why is that? -
hi all, I found an article about I have an excel automation dos based application . I take an article, Automate excel from c++ without using mfc or #import (Q216686) as an example. then based on another article Automate excel and then know the use closed it(Q192348) I changed my code again. actually, i added,
// wait until the user close the application HWND hWnd; char buf[1024]; hWnd = ::FindWindow("XLMain",NULL); if(NULL==hWnd) { long lErr = GetLastError(); sprintf(buf, "FindWindow Error code = %d",lErr); AfxMessageBox(buf); } DWORD pid; DWORD dThread; DWORD dwReason; dThread = ::GetWindowThreadProcessId(hWnd,&pid); HANDLE hProcess; hProcess = ::OpenProcess(SYNCHRONIZE|PROCESS_ALL_ACCESS,TRUE,pid); dwReason = ::WaitForSingleObject(hProcess,INFINITE); ::CloseHandle(hProcess);
everything is fine except dwReason = ::WaitForSingleObject(hProcess,INFINITE); is waiting forever because in windows task manager's processes tab, you will find EXCEL.EXE eventhough you have closed ms excel application. if you command the line dwReason = ::WaitForSingleObject(hProcess,INFINITE); you will not find EXCEL.EXE in windows task manager's processes tab. why is that? -
hi all, I found an article about I have an excel automation dos based application . I take an article, Automate excel from c++ without using mfc or #import (Q216686) as an example. then based on another article Automate excel and then know the use closed it(Q192348) I changed my code again. actually, i added,
// wait until the user close the application HWND hWnd; char buf[1024]; hWnd = ::FindWindow("XLMain",NULL); if(NULL==hWnd) { long lErr = GetLastError(); sprintf(buf, "FindWindow Error code = %d",lErr); AfxMessageBox(buf); } DWORD pid; DWORD dThread; DWORD dwReason; dThread = ::GetWindowThreadProcessId(hWnd,&pid); HANDLE hProcess; hProcess = ::OpenProcess(SYNCHRONIZE|PROCESS_ALL_ACCESS,TRUE,pid); dwReason = ::WaitForSingleObject(hProcess,INFINITE); ::CloseHandle(hProcess);
everything is fine except dwReason = ::WaitForSingleObject(hProcess,INFINITE); is waiting forever because in windows task manager's processes tab, you will find EXCEL.EXE eventhough you have closed ms excel application. if you command the line dwReason = ::WaitForSingleObject(hProcess,INFINITE); you will not find EXCEL.EXE in windows task manager's processes tab. why is that?G'Day Win, I use the same code successfully. What I discovered was that as I developed the application I would run it and terminate it prematurely with Excel open. This of course did not close down excel properly. So that when I started the application again I had 2 versions of excel running etc. Thus when I had a 'real' excel application running and I tested my program and shut excel down this code failed because I had closed down a different excel to the one being watched. I think this is what may be happening in your case. So terminate all excel processes with task manager, run excel and then your application and see if it works then. Hope that is of some help, Richard.
-
G'Day Win, I use the same code successfully. What I discovered was that as I developed the application I would run it and terminate it prematurely with Excel open. This of course did not close down excel properly. So that when I started the application again I had 2 versions of excel running etc. Thus when I had a 'real' excel application running and I tested my program and shut excel down this code failed because I had closed down a different excel to the one being watched. I think this is what may be happening in your case. So terminate all excel processes with task manager, run excel and then your application and see if it works then. Hope that is of some help, Richard.