1

I am new to C++ programming but I would like to know if there is an easy way to check if an Outlook Process is already running, or in other words check if Outlook is already open.

Can anyone show me, please?

Thanks

Johan
  • 20,067
  • 28
  • 92
  • 110
Andy5
  • 2,319
  • 11
  • 45
  • 91
  • Can you specify target OS and restrictions on use of additional libraries/frameworks (like boost or Qt)? – beduin Apr 19 '11 at 08:57
  • Why do you want this? A common answer is "because I want to start it if it's not". In which case the original question is moot; just start Outlook. It will not start a second instance. Let it deal with complex cases like multiple user account etc. – MSalters Apr 19 '11 at 09:18
  • @MSalters: In my experience Outlook *does* start new instances unless the command line switch `/recycle` is used; why else would this switch exist? – mousio Apr 19 '11 at 22:31
  • @mousio: I was thinking about `CoCreateInstance()`, so command line switches wouldn't apply. – MSalters Apr 21 '11 at 08:43
  • @MSalters: Ah OK, that is another story :) – mousio Apr 21 '11 at 10:56

4 Answers4

4

You can check the running instance of the outlook OLE:

static const bool isOutlookRunning()
{
    CLSID clsid;
    HRESULT hr = CLSIDFromProgID( _T("Outlook.Application"), &clsid );
    if( hr != S_OK ) return false;

    IUnknown *pUnknown = NULL;
    hr = GetActiveObject( clsid, 0, &pUnknown );
    return ( hr == S_OK );
}

This code will not work if the app from you invoke it is running in different user security context than Outlook. Its better to check for the process name.

Manuel
  • 3
  • 2
Blazes
  • 4,721
  • 2
  • 22
  • 29
1
//////////////////////////////////////////////////////////////////////
// IsProcessRunning
//////////////////////////////////////////////////////////////////////
bool IsProcessRunning(string &strProcessFullPathName)
{   
    // Get the list of process identifiers.
    DWORD dwProcesses[2048];

    DWORD dwSizeNeeded = 0;
    DWORD dwProcessesCount = 0;

    if ( !EnumProcesses( dwProcesses, sizeof(dwProcesses), &dwSizeNeeded ) )
    {
        _dwLastErrorCode = GetLastError();
        _strLastError = "EnumProcesses";        
        return false;
    }

    dwProcessesCount = dwSizeNeeded / sizeof(DWORD);


    string strToCheck = strProcessFullPathName;
    transform(strToCheck.begin(), strToCheck.end(), strToCheck.begin(), tolower);

    for(int i=0; i<(int)dwProcessesCount; i++ )
    {
        if( dwProcesses[i] != 0 )
        {                   
            HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcesses[i]);
            if(hProcess)
            {
                HMODULE hModule = NULL;
                dwSizeNeeded = 0;
                char cName[MAX_PATH];
                if(EnumProcessModules(hProcess, &hModule, sizeof(DWORD), &dwSizeNeeded) )
                {
                    if(GetModuleFileNameEx(hProcess, hModule, cName, MAX_PATH ) != 0)
                    {
                        string strName(cName);
                        // Convert to LowerCase
                        transform(strName.begin(), strName.end(), strName.begin(), tolower);

                        if(strName == strToCheck)
                        {
                            CloseHandle(hProcess);
                            return true;
                        }
                    }
                }

                CloseHandle(hProcess);
            }               
        }
    }


    return false;
}
João Augusto
  • 2,285
  • 24
  • 28
0

You can use CreateToolhelp32Snapshot and iterate through the running processes. If you need to heck for Outlook again (say you need to poll for the process), then save the process ID and use OpenProcess (many times faster). More details are given in the answer to this question: Check whether one specific process is running on windows with C++

Community
  • 1
  • 1
AlainD
  • 5,413
  • 6
  • 45
  • 99
0

I assume that you are using Windows as platform, since Outlook normally runs under a Windows OS.

For a simple check you can use the Windows API functions EnumProcesses() and GetModuleBaseName() to determine the executable filename of the running processes and check if the executable name is "outlook.exe". A reference for the functions can be found in the MSDN.

Be aware that the this check may fail if the user runs another program that is using the same executable filename as Outlook. Another trap with the provided solution might be the access rights a user needs for the specified API calls.

Improvements might be to check the version information in the executable file too by using GetFileVersionInfo().

Dolphin
  • 299
  • 1
  • 2