Detect whether started by COM

I have a MFC Dialog-based Automation Server.  This server should not be able to be run "standalone".  I.e. it should only be able to be started up when invoked by a COM client.

I am looking for a clean way to quit this program when the user starts it up manually.  The most promising way I have found so far is the following code in OnInitDialog():

    // If there are any outstanding COM references to this app,
    // then it was created as an automation object via COM.
    // If not...

    // If we were not started by OLE...
    if (AfxOleCanExitApp())

        // Display a warning message and quit

        EnableWindow (FALSE);
        AfxMessageBox ("This application can only be run by a COM client");
        EnableWindow (TRUE);

        // Perform an "exit(1)" equivalent
        EndDialog (1);


Unfortunately, this code is also triggered when a COM client starts the app, and I can't see why this should be the case.  Could someone point out a way around this, or suggest an alternative method to achieve the result?

Thanks in advance!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

When a COM client starts the app,  the command line contains an appropriate switch ('/Automation'). Look for

    // Parse command line for standard shell commands, DDE, file open
    CCommandLineInfo cmdInfo;

in your application's 'InitInstance()' and add the following check:

    // Check to see if launched as OLE server
    if (!cmdInfo.m_bRunEmbedded || !cmdInfo.m_bRunAutomated)
        // Application wasn't run with /Embedding or /Automation.
        // Bail out!
        // ...

Feel free to ask if you need more information!
kbridgeAuthor Commented:
Thanks for your suggestion.  However I tried it and it is still possible to successfully start up the server from the command line with the "/Automation" switch.  I think that our client is looking for something a little more secure than that.

Is there possibly a more bulletproof method?
What about simply checking for the presence of this switch manually, e.g.

(also in 'InitInstance()')

if ( strstr ( GetCommandLine(), "/Automation"))
 ExitProcess( 0);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

kbridgeAuthor Commented:
That is exactly what I tried.  But that doesn't stop someone from firing up a DOS window, typing in "myapp /automation" and fooling my code into thinking that it's been started by an OLE client.  This is why my initial attempt was based around counting the number of actual OLE clients which the application has.

kbridgeAuthor Commented:
Thanks, anyway jkr for your proposal.  At least it is something which I will use in the meantime until I can find something better.  I was hoping that someone else would come up with a better answer, but unfortunately I have no more time available to work on it.
kbridgeAuthor Commented:
I have used a check in ParseCommandLine (similar to what you suggested initially), but the effect is the same.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.