> There is more reliable way. For example.
> 
> h = GetProcessWindowStation();
> if (h==NULL) fatal error; /* or return "runs as service" */
> if (GetUserObjectInformationW (h,UOI_NAME,NULL,0,NULL,&len) ||
>         GetLastError() != ERROR_INSUFFICIENT_BUFFER)
>    fatal error; /* or return "runs as service" */

Complete working snippet below. Note that it return -1 if it didn't
manage to initialize window station, 1 if called in [non interactive]
service context and 0 if called in interactive context. Tested with M$C
and Cygwin gcc. A.

#include <windows.h>
#include <malloc.h>

static const WCHAR *_own_wcsstr(const WCHAR *str,const WCHAR *key)
{ int i;

    if (str==NULL || key==NULL || *key==L'\0') return NULL;

    for (i=0;*str!=L'\0';str++)
        if (*str!=*key)                 str-=i, key-=i, i=0;
        else if (*(++key)==L'\0')       return str-i;
        else                            i++;

  return NULL;
}

int IsService()
{ HWINSTA h;
  DWORD len;
  WCHAR *name;

    GetDesktopWindow(); /* return value is ignored */

    h = GetProcessWindowStation();
    if (h==NULL) return -1;

    if (GetUserObjectInformationW (h,UOI_NAME,NULL,0,&len) ||
        GetLastError() != ERROR_INSUFFICIENT_BUFFER)
        return -1;

    if (len>512) return -1;             /* paranoia */
    len++,len&=~1;                      /* paranoia */
#ifdef _MSC_VER
    name=(WCHAR *)_alloca(len+sizeof(WCHAR));
#else
    name=(WCHAR *)alloca(len+sizeof(WCHAR));
#endif
    if (!GetUserObjectInformationW (h,UOI_NAME,name,len,&len))
        return -1;

    len++,len&=~1;                      /* paranoia */
    name[len/sizeof(WCHAR)]=L'\0';      /* paranoia */
#if 1
    /* This doesn't cover "interactive" services [working with real
     * WinSta0's] nor programs started non-interactively by Task
     * Scheduler [those are working with SAWinSta]. */
    if (_own_wcsstr(name,L"Service-0x"))
#else
    /* This covers all non-interactive programs such as services. */
    if (!_own_wcsstr(name,L"WinSta0"))
#endif
        return 1;
    else
        return 0;
}
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to