Archived

This forum has been archived. Please start a new discussion on GitHub.

How to transform "bidir" into a _tWinMain dll project?

Dear All,

I tried to make a new windows service project include ATLCOM and MFC with Ice-3.3.1-VC60.The dll entry point might be _tWinMain(....)
void WINAPI CServiceModule::_ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
    _Module.ServiceMain(dwArgc, lpszArgv);
}

void CServiceModule::Run()
{
    _Module.dwThreadID = GetCurrentThreadId();
    HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    _ASSERTE(SUCCEEDED(hr));

    Startup();

    // This provides a NULL DACL which will allow access to everyone.
    CSecurityDescriptor sd;
    sd.InitializeFromThreadToken();
	hr = CoInitializeSecurity(NULL, -1, NULL, NULL,RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IDENTIFY, NULL,EOAC_NONE, NULL); 
	_ASSERTE(SUCCEEDED(hr));

    hr = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER, REGCLS_MULTIPLEUSE);
    _ASSERTE(SUCCEEDED(hr));

    LogEvent(_T("Service started"));
    if (m_bService)
        SetServiceStatus(SERVICE_RUNNING);

    MSG msg;
    while (GetMessage(&msg, 0, 0, 0))
        DispatchMessage(&msg);

    _Module.RevokeClassObjects();

	Shutdown();

    CoUninitialize();
}

extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, 
    HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/)
{
    lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT
	_Module.Init(ObjectMap, hInstance, IDS_SERVICENAME, &LIBID_MTCLib);
	_Module.m_bService = TRUE;
    TCHAR szTokens[] = _T("-/");

    LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens);
    while (lpszToken != NULL)
    {
        if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
            return _Module.UnregisterServer();

        // Register as Local Server
        if (lstrcmpi(lpszToken, _T("RegServer"))==0)
            return _Module.RegisterServer(TRUE, FALSE);
        
        // Register as Service
        if (lstrcmpi(lpszToken, _T("Service"))==0)
            return _Module.RegisterServer(TRUE, TRUE);
        
        lpszToken = FindOneOf(lpszToken, szTokens);
    }

    // Are we Service or Local Server
    CRegKey keyAppID;
    LONG lRes = keyAppID.Open(HKEY_CLASSES_ROOT, _T("AppID"), KEY_READ);
    if (lRes != ERROR_SUCCESS)
        return lRes;

    CRegKey key;
    lRes = key.Open(keyAppID, _T("{14BA53FC-EBF0-4B2C-8DB6-F30821E04B38}"), KEY_READ);
    if (lRes != ERROR_SUCCESS)
        return lRes;

    TCHAR szValue[_MAX_PATH];
    DWORD dwLen = _MAX_PATH;
    lRes = key.QueryValue(szValue, _T("LocalService"), &dwLen);

    _Module.m_bService = FALSE;
    if (lRes == ERROR_SUCCESS)
        _Module.m_bService = TRUE;

	_Module.Start();

    // When we get here, the service has been stopped
    return _Module.m_status.dwWin32ExitCode;

}

This part with Ice::Application make me difficultly to integrate with _tWinMain()
class CallbackServer : public Ice::Application
int main(int argc, char* argv[])
CallbackServer::run(int argc, char* argv[])

I tried to do all I can.But never success.
Could someone please any help to provide a demo code like "bidir" in windows service project?

I've look at IceBox hello sample.If I want to make a dll or service project with ICE.I prefer the Ice::Application instead of Ice::Service.Is this the right choice?

Thanks,

Stanley.

Comments

  • I also look at the MFC demos included with the Ice distribution-- demo/Ice/MFC/.
    But there are some different between MFC console/service/win32 dialog base app.I can't create a independent Ice Class or Object(e.g. CCallbackServer) that can be re-use in a console app with MFC support.

    Thanks,

    Stanley.
  • benoit
    benoit Rennes, France
    Hi Stanley,

    I recommend not using Ice::Service or Ice::Application if they don't fit your needs. These classes are just helpers to write Ice based command line applications or Windows services.

    You can easily create the Ice communicator with the Ice::initialize() method. See the demo/Ice/minimal demo from your Ice distribution for an example which is not using Ice::Application. The MFC demo is also not using Ice::Application, you could do something similar: create the communicator in your CServiceModule class.

    Cheers,
    Benoit.
  • Dear Benoit ,

    Once I removed Ice::Application , then started working fine.

    Thanks for your help.

    Stanley.