• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

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!
0
kbridge
Asked:
kbridge
  • 4
  • 2
1 Solution
 
jkrCommented:
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;
    ParseCommandLine(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!
0
 
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?
0
 
jkrCommented:
What about simply checking for the presence of this switch manually, e.g.

(also in 'InitInstance()')

if ( strstr ( GetCommandLine(), "/Automation"))
{
 ExitProcess( 0);
}
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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.

0
 
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.
0
 
kbridgeAuthor Commented:
I have used a check in ParseCommandLine (similar to what you suggested initially), but the effect is the same.
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now