Click here to Skip to main content
15,922,407 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
AnswerRe: stl sorting question Pin
Rob Caldecott24-Feb-06 9:52
Rob Caldecott24-Feb-06 9:52 
GeneralRe: stl sorting question Pin
Warren Stevens24-Feb-06 11:45
Warren Stevens24-Feb-06 11:45 
Questionstl string parsing for DB query statement Pin
xcavin23-Feb-06 5:48
xcavin23-Feb-06 5:48 
AnswerRe: stl string parsing for DB query statement Pin
Roland Pibinger24-Feb-06 23:23
Roland Pibinger24-Feb-06 23:23 
QuestionError In Building simple ATL application Pin
maharaja pandian23-Feb-06 1:47
maharaja pandian23-Feb-06 1:47 
AnswerRe: Error In Building simple ATL application Pin
Stephen Hewitt24-Feb-06 22:40
Stephen Hewitt24-Feb-06 22:40 
QuestionUsing ShellContextMenu in my application Pin
empit4ever22-Feb-06 11:18
empit4ever22-Feb-06 11:18 
QuestionAccess to COM methods inside Windows Service ATL Pin
cmacgowan22-Feb-06 6:01
cmacgowan22-Feb-06 6:01 
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()<br />
{<br />
    _Module.dwThreadID = GetCurrentThreadId();<br />
<br />
    HRESULT hr = CoInitialize(NULL);<br />
//  If you are running on NT 4.0 or higher you can use the following call<br />
//  instead to make the EXE free threaded.<br />
//  This means that calls come in on a random RPC thread<br />
//  HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);<br />
<br />
    _ASSERTE(SUCCEEDED(hr));<br />
<br />
    // This provides a NULL DACL which will allow access to everyone.<br />
    CSecurityDescriptor sd;<br />
    sd.InitializeFromThreadToken();<br />
    hr = CoInitializeSecurity(sd, -1, NULL, NULL,<br />
        RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);<br />
    _ASSERTE(SUCCEEDED(hr));<br />
<br />
    hr = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER, REGCLS_MULTIPLEUSE);<br />
    _ASSERTE(SUCCEEDED(hr));<br />
<br />
    LogEvent(_T("Blue Service started"));<br />
    LogEvent(_T("Message 1"));<br />
    LogEvent(_T("Message 2"));<br />
<br />
    if (m_bService)<br />
        SetServiceStatus(SERVICE_RUNNING);<br />
<br />
    MSG msg;<br />
    while (GetMessage(&msg, 0, 0, 0))<br />
        DispatchMessage(&msg);<br />
<br />
    _Module.RevokeClassObjects();<br />
<br />
<br />
    // --------------------------------------------<br />
    // We will try to call the COM Object from here <br />
<br />
    char progID[] = "Blue.Test.1";<br />
<br />
    // Now make that object.<br />
    CLSID clsid;<br />
    wchar_t wide[80]; <br />
    mbstowcs(wide, progID, 80);<br />
    CLSIDFromProgID(wide, &clsid);<br />
<br />
    LogEvent(_T("Attempt to use ITest"));<br />
<br />
    ITest* pTest = NULL;<br />
    if(SUCCEEDED(CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_ITest, (void**)&pTest)))<br />
    {<br />
        pTest->TestBeep(); <br />
    }<br />
    else<br />
    { <br />
        LogEvent(_T("Error: ITest failed"));<br />
    } <br />
<br />
<br />
    // We will try this another way !! <br />
    // We have downloaded a ATL COM Server sample and it had <br />
    // A COM Method in in and it was not called from within the <br />
    // Service (as we are trying to do) it was called from a<br />
    // Windows Console Application using the following.  <br />
    // We would like to make this call into com from in the <br />
    // Service !! <br />
    <br />
    CComPtr<ITest> pObj;<br />
    hr = pObj.CoCreateInstance(OLESTR("Blue.Test.1"));<br />
    if( SUCCEEDED(hr) )<br />
    {<br />
        hr = pObj->TestBeep();<br />
        if( SUCCEEDED(hr) )<br />
        { <br />
            LogEvent(_T("Call to ITest was successful"));<br />
        }<br />
        else<br />
        { <br />
            LogEvent(_T("Error: ITest failed"));<br />
        } <br />
    }<br />
<br />
    if( FAILED(hr) )<br />
    { <br />
        LogEvent(_T("CoCreateInstance failed"));<br />
    }<br />
<br />
    CoUninitialize();<br />
}<br />


This is the implementation of the CTest Class and the TestBeep() method


// Test.h : Declaration of the CTest<br />
<br />
#ifndef __TEST_H_<br />
#define __TEST_H_<br />
<br />
#include "resource.h"       // main symbols<br />
<br />
/////////////////////////////////////////////////////////////////////////////<br />
// CTest<br />
class ATL_NO_VTABLE CTest : <br />
    public CComObjectRootEx<CComSingleThreadModel>,<br />
    public CComCoClass<CTest, &CLSID_Test>,<br />
    public IDispatchImpl<ITest, &IID_ITest, &LIBID_BLUELib><br />
{<br />
public:<br />
    CTest()<br />
    {<br />
    }<br />
<br />
DECLARE_REGISTRY_RESOURCEID(IDR_TEST)<br />
<br />
DECLARE_PROTECT_FINAL_CONSTRUCT()<br />
<br />
BEGIN_COM_MAP(CTest)<br />
    COM_INTERFACE_ENTRY(ITest)<br />
    COM_INTERFACE_ENTRY(IDispatch)<br />
END_COM_MAP()<br />
<br />
// ITest<br />
public:<br />
    STDMETHOD(TestBeep)();<br />
    STDMETHOD(GetCount)();<br />
    int nCount;<br />
};<br />
<br />
#endif //__TEST_H_<br />
<br />
<br />
<br />
// Test.cpp : Implementation of CTest<br />
#include "stdafx.h"<br />
#include "Blue.h"<br />
#include "Test.h"<br />
<br />
/////////////////////////////////////////////////////////////////////////////<br />
// CTest<br />
<br />
<br />
STDMETHODIMP CTest::GetCount()<br />
{<br />
    // TODO: Add your implementation code here<br />
<br />
    return S_OK;<br />
}<br />
<br />
STDMETHODIMP CTest::TestBeep()<br />
{<br />
    // TODO: Add your implementation code here<br />
    Beep(4000, 200); <br />
    Beep(1000, 50); <br />
    Beep(4000, 200); <br />
    Beep(1000, 50); <br />
    return S_OK;<br />
}<br />
<br />


Thanks
Chris
AnswerRe: Access to COM methods inside Windows Service ATL Pin
cmacgowan24-Feb-06 8:28
cmacgowan24-Feb-06 8:28 
QuestionActiveX classname chnage Pin
Anilkumar K V20-Feb-06 16:42
Anilkumar K V20-Feb-06 16:42 
Questioni write a general dll want only use ATL's some windows class, how to do? Pin
HOW WHAT18-Feb-06 20:01
HOW WHAT18-Feb-06 20:01 
AnswerRe: i write a general dll want only use ATL's some windows class, how to do? Pin
georgeraafat20-Feb-06 9:07
georgeraafat20-Feb-06 9:07 
GeneralRe: i write a general dll want only use ATL's some windows class, how to do? Pin
HOW WHAT20-Feb-06 20:48
HOW WHAT20-Feb-06 20:48 
GeneralRe: i write a general dll want only use ATL's some windows class, how to do? Pin
Stephen Hewitt21-Feb-06 19:28
Stephen Hewitt21-Feb-06 19:28 
Questionin WTL, can these code not use in WinMain? Pin
HOW WHAT18-Feb-06 19:58
HOW WHAT18-Feb-06 19:58 
AnswerRe: in WTL, can these code not use in WinMain? Pin
Douglas Troy20-Feb-06 7:16
Douglas Troy20-Feb-06 7:16 
Questionproblem with ocx... Pin
padma_0117-Feb-06 1:07
padma_0117-Feb-06 1:07 
GeneralRe: problem with ocx... Pin
Joonsong Jang17-Feb-06 19:59
Joonsong Jang17-Feb-06 19:59 
QuestionAdd a New ATL Object Pin
Anil_vvs15-Feb-06 18:42
Anil_vvs15-Feb-06 18:42 
AnswerRe: Add a New ATL Object Pin
Stephen Hewitt16-Feb-06 11:45
Stephen Hewitt16-Feb-06 11:45 
QuestionActiveX ATL control Pin
Anilkumar K V13-Feb-06 18:43
Anilkumar K V13-Feb-06 18:43 
GeneralRe: ActiveX ATL control Pin
Joonsong Jang17-Feb-06 19:36
Joonsong Jang17-Feb-06 19:36 
QuestionIcons not dragging in listctrl Pin
Parampreet Sidhu13-Feb-06 5:13
Parampreet Sidhu13-Feb-06 5:13 
AnswerRe: Icons not dragging in listctrl Pin
Parampreet Sidhu14-Feb-06 0:25
Parampreet Sidhu14-Feb-06 0:25 
QuestionHow to determine if an ActiveX control is signed or not Pin
KKTECH12-Feb-06 23:03
KKTECH12-Feb-06 23:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.