Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Access to COM methods inside Windows Service ATL

Access to COM methods inside Windows Service ATL

Scheduled Pinned Locked Moved ATL / WTL / STL
helpcsharpc++visual-studiocom
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cmacgowan
    wrote on last edited by
    #1

    Hi ... I have written a Windows Service using ATL COM (ATL3 / Visual Studio 6.0). The service works fine, I am able to start and stop the service. I have created a COM Class called CTest with a methof called TestBeep(). I would like to access this object and call the method from the CServiceModule::Run() method. Below is the CServiceModule::Run() Method. The code will fail on both attempts to create the ITest* pTest using CoCreateInstance(). The error message are written to the event log "Error: ITest failed" "CoCreateInstance failed" Any help is appreciated, Thanks, Chris void CServiceModule::Run() { _Module.dwThreadID = GetCurrentThreadId(); HRESULT hr = CoInitialize(NULL); // If you are running on NT 4.0 or higher you can use the following call // instead to make the EXE free threaded. // This means that calls come in on a random RPC thread // HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); _ASSERTE(SUCCEEDED(hr)); // This provides a NULL DACL which will allow access to everyone. CSecurityDescriptor sd; sd.InitializeFromThreadToken(); hr = CoInitializeSecurity(sd, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); _ASSERTE(SUCCEEDED(hr)); hr = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER, REGCLS_MULTIPLEUSE); _ASSERTE(SUCCEEDED(hr)); LogEvent(_T("Blue Service started")); LogEvent(_T("Message 1")); LogEvent(_T("Message 2")); if (m_bService) SetServiceStatus(SERVICE_RUNNING); MSG msg; while (GetMessage(&msg, 0, 0, 0)) DispatchMessage(&msg); _Module.RevokeClassObjects(); // -------------------------------------------- // We will try to call the COM Object from here char progID[] = "Blue.Test.1"; // Now make that object. CLSID clsid; wchar_t wide[80]; mbstowcs(wide, progID, 80); CLSIDFromProgID(wide, &clsid); LogEvent(_T("Attempt to use ITest")); ITest* pTest = NULL; if(SUCCEEDED(CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_ITest, (void**)&pTest))) { pTest->TestBeep(); } else { LogEvent(_T("Error: ITest failed")); } // We will try this another way !! // We have downloaded a ATL COM Server sample and it had // A COM Method in in and it was not called from within the // Service (as we are trying to do) it was called from a

    C 1 Reply Last reply
    0
    • C cmacgowan

      Hi ... I have written a Windows Service using ATL COM (ATL3 / Visual Studio 6.0). The service works fine, I am able to start and stop the service. I have created a COM Class called CTest with a methof called TestBeep(). I would like to access this object and call the method from the CServiceModule::Run() method. Below is the CServiceModule::Run() Method. The code will fail on both attempts to create the ITest* pTest using CoCreateInstance(). The error message are written to the event log "Error: ITest failed" "CoCreateInstance failed" Any help is appreciated, Thanks, Chris void CServiceModule::Run() { _Module.dwThreadID = GetCurrentThreadId(); HRESULT hr = CoInitialize(NULL); // If you are running on NT 4.0 or higher you can use the following call // instead to make the EXE free threaded. // This means that calls come in on a random RPC thread // HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); _ASSERTE(SUCCEEDED(hr)); // This provides a NULL DACL which will allow access to everyone. CSecurityDescriptor sd; sd.InitializeFromThreadToken(); hr = CoInitializeSecurity(sd, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); _ASSERTE(SUCCEEDED(hr)); hr = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER, REGCLS_MULTIPLEUSE); _ASSERTE(SUCCEEDED(hr)); LogEvent(_T("Blue Service started")); LogEvent(_T("Message 1")); LogEvent(_T("Message 2")); if (m_bService) SetServiceStatus(SERVICE_RUNNING); MSG msg; while (GetMessage(&msg, 0, 0, 0)) DispatchMessage(&msg); _Module.RevokeClassObjects(); // -------------------------------------------- // We will try to call the COM Object from here char progID[] = "Blue.Test.1"; // Now make that object. CLSID clsid; wchar_t wide[80]; mbstowcs(wide, progID, 80); CLSIDFromProgID(wide, &clsid); LogEvent(_T("Attempt to use ITest")); ITest* pTest = NULL; if(SUCCEEDED(CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_ITest, (void**)&pTest))) { pTest->TestBeep(); } else { LogEvent(_T("Error: ITest failed")); } // We will try this another way !! // We have downloaded a ATL COM Server sample and it had // A COM Method in in and it was not called from within the // Service (as we are trying to do) it was called from a

      C Offline
      C Offline
      cmacgowan
      wrote on last edited by
      #2

      Hi ... The solution to this problem is to put the com call to ITest above the message loop. Below is the corrected code. Thanks, Chris void CServiceModule::Run() { _Module.dwThreadID = GetCurrentThreadId(); HRESULT hr = CoInitialize(NULL); // If you are running on NT 4.0 or higher you can use the following call // instead to make the EXE free threaded. // This means that calls come in on a random RPC thread // HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); _ASSERTE(SUCCEEDED(hr)); // This provides a NULL DACL which will allow access to everyone. CSecurityDescriptor sd; sd.InitializeFromThreadToken(); hr = CoInitializeSecurity(sd, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); _ASSERTE(SUCCEEDED(hr)); hr = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER, REGCLS_MULTIPLEUSE); _ASSERTE(SUCCEEDED(hr)); LogEvent(_T("Blue Service started")); LogEvent(_T("Message 1")); LogEvent(_T("Message 2")); if (m_bService) SetServiceStatus(SERVICE_RUNNING); // -------------------------------------------- // We will try to call the COM Object from here char progID[] = "Blue.Test.1"; string strTemp; char szTemp[10]; // Now make that object. CLSID clsid; wchar_t wide[80]; mbstowcs(wide, progID, 80); CLSIDFromProgID(wide, &clsid); LogEvent(_T("Attempt to create the ITest Com Object")); ITest* pTest = NULL; hr = CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_ITest, (void**)&pTest); if(SUCCEEDED(hr)) { pTest->TestBeep(); } else { // LogEvent(_T("Error: ITest failed hresult")); sprintf(szTemp, "%h", hr); strTemp = "Error: ITest failed. HResult=" + (string)szTemp; LogEvent(_T(strTemp.c_str())); } MSG msg; while (GetMessage(&msg, 0, 0, 0)) DispatchMessage(&msg); _Module.RevokeClassObjects(); CoUninitialize(); }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups