🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #4368

Re: NT Service / DCOM server failing on Windows 2003

#4368
From: Remy Lebeau (TeamB)
Date: Wed, 22 Feb 2006 23:32
102 lines
4456 bytes
"Mike Collins" <its@TheBottomOfThePost.com> wrote in message
news:43fc85d2$1@newsgroups.borland.com...

> Hi all, got a really annoying problem that we are struggling to solve.

Win2003 changes the requirements of DCOM servers in services, and the VCL
prior to BDS 2006 could not natively handle it.  The workaround is to edit
the project's WinMain() function to not call Application->Initialize() under
Win2003, and then call Application->Initialize() after the service is being
started.  For example:

    --- Project.cpp ---

    #include <ComObj.hpp>
    external bool bIsAppInitialized;

    bool __fastcall IsRunningInInstallMode()
    {
        static TSysCharSet switches = TSysCharSet() << '-' << '/';

        return FindCmdLineSwitch("REGSERVER", switches, true) ||
            FindCmdLineSwitch("UNREGSERVER", switches, true) ||
            FindCmdLineSwitch("INSTALL", switches, true) ||
            FindCmdLineSwitch("UNINSTALL", switches, true);
    }

    WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
    {
        Comobj::CoInitFlags = COINIT_MULTITHREADED;

        CoInitializeEx(NULL, COINIT_MULTITHREADED);
        CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_NONE,
RPC_C_IMP_LEVEL_ANONYMOUS, NULL, EOAC_NONE, NULL);

        try
        {
            // To support Windows Server 2003 we need to make sure
            // that StartServiceCtrlDispatcher() is called before
            // CoRegisterClassObject().  Unfortunately, the VCL does
            // not do this.  Application->Initialize() below calls
            // CoRegisterClassObject() first, which will then cause
            // the COM server to fail to initialize properly under
            // Windows Server 2003.  Earlier versions of Windows do
            // not suffer from this issue...

            // Win Server 2003 = v5.2
            if( !((Win32MajorVersion == 5) && (Win32MinorVersion >= 2)) ||
IsRunningInInstallMode() )
            {
                Application->Initialize();
                bIsAppInitialized = true;
            }

            //...
        }
        catch (Exception &exception)
        {
            Sysutils::ShowException(&exception, Sysutils::ExceptAddr());
        }

        CoUninitialize();
        return 0;
    }

    --- Service.cpp ---

    bool bIsAppInitialized = false;

    void __fastcall TVPIEventCenterService::ServiceStart(TService *Sender,
bool &Started)
    {
        ErrCode = 0;
        Win32ErrCode = 0;

        // To support Windows Server 2003 we need to make sure
        // that StartServiceCtrlDispatcher() is called before
        // CoRegisterClassObject().  Unfortunately, the VCL does
        // not do this.  Application->Initialize() calls
        // CoRegisterClassObject() first, which will then cause
        // the COM server to fail to initialize properly under
        // Windows Server 2003.  Application->Initialize() is
        // delayed until below so that StartServiceCtrlDispatcher()
        // can be called first.  Earlier versions of Windows do
        // not suffer from this issue...

        if( !bIsAppInitialized )
        {
            Svcmgr::Application->Initialize();
            bIsAppInitialized = true;
        }

        //...
    }


As for BDS 2006, Borland tried to address the issue with a new property in
TApplication, but it did not work properly in the first release.  I do not
know if Update 1 fixed it or not.


Gambit
Technical Details
Message-ID: <43fcf481$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <43fc85d2$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,45cf8d20e0960657
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <43fc85d2$1@newsgroups.borland.com>
Subject: Re: NT Service / DCOM server failing on Windows 2003
Date: Wed, 22 Feb 2006 15:32:58 -0800
Lines: 104
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <43fcf481$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1140651137 68.69.17.41 (22 Feb 2006 15:32:17 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail