Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. excel automation, hprocess is not closing

excel automation, hprocess is not closing

Scheduled Pinned Locked Moved C / C++ / MFC
c++comtestingtoolshelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pnpfriend
    wrote on last edited by
    #1

    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?

    P R 2 Replies Last reply
    0
    • P pnpfriend

      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?

      P Offline
      P Offline
      pnpfriend
      wrote on last edited by
      #2

      can someone help me please:((

      1 Reply Last reply
      0
      • P pnpfriend

        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?

        R Offline
        R Offline
        Richard Ellis
        wrote on last edited by
        #3

        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.

        P 1 Reply Last reply
        0
        • R Richard Ellis

          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.

          P Offline
          P Offline
          pnpfriend
          wrote on last edited by
          #4

          thank you Richard. that's is a really helpful. Thanks

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups