Click here to Skip to main content
15,887,485 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: All member variables contain same value Pin
BuckBrown22-Feb-06 11:05
BuckBrown22-Feb-06 11:05 
GeneralRe: All member variables contain same value Pin
David Crow23-Feb-06 2:29
David Crow23-Feb-06 2:29 
Questionlist view / how do i get a list of all selected items? Pin
Sebastian Pipping22-Feb-06 7:09
Sebastian Pipping22-Feb-06 7:09 
AnswerRe: list view / how do i get a list of all selected items? Pin
James R. Twine22-Feb-06 7:27
James R. Twine22-Feb-06 7:27 
GeneralRe: list view / how do i get a list of all selected items? Pin
Sebastian Pipping22-Feb-06 7:57
Sebastian Pipping22-Feb-06 7:57 
AnswerRe: list view / how do i get a list of all selected items? Pin
David Crow22-Feb-06 7:38
David Crow22-Feb-06 7:38 
GeneralRe: list view / how do i get a list of all selected items? Pin
Sebastian Pipping22-Feb-06 8:04
Sebastian Pipping22-Feb-06 8:04 
QuestionAccess to COM methods inside Windows Service ATL Pin
cmacgowan22-Feb-06 6:03
cmacgowan22-Feb-06 6:03 
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 />
<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
QuestionLGPL query Pin
Chintoo72322-Feb-06 5:51
Chintoo72322-Feb-06 5:51 
AnswerRe: LGPL query Pin
Chris Losinger22-Feb-06 7:45
professionalChris Losinger22-Feb-06 7:45 
GeneralRe: LGPL query Pin
Chintoo72322-Feb-06 15:21
Chintoo72322-Feb-06 15:21 
GeneralRe: LGPL query Pin
Chris Losinger22-Feb-06 16:01
professionalChris Losinger22-Feb-06 16:01 
AnswerRe: LGPL query Pin
Joe Woodbury22-Feb-06 8:26
professionalJoe Woodbury22-Feb-06 8:26 
AnswerRe: LGPL query Pin
Sebastian Pipping22-Feb-06 15:40
Sebastian Pipping22-Feb-06 15:40 
QuestionPassing Arrays of structures from VB.NET to unmanaged C++ Pin
CPop22-Feb-06 5:45
CPop22-Feb-06 5:45 
QuestionListview controls Pin
Waldermort22-Feb-06 5:23
Waldermort22-Feb-06 5:23 
AnswerRe: Listview controls Pin
Joe Woodbury22-Feb-06 5:41
professionalJoe Woodbury22-Feb-06 5:41 
AnswerRe: Listview controls Pin
Gavin Taylor22-Feb-06 6:07
professionalGavin Taylor22-Feb-06 6:07 
AnswerRe: Listview controls Pin
Sebastian Pipping22-Feb-06 10:32
Sebastian Pipping22-Feb-06 10:32 
GeneralRe: Listview controls Pin
Waldermort22-Feb-06 15:13
Waldermort22-Feb-06 15:13 
AnswerRe: Listview controls Pin
Sebastian Pipping22-Feb-06 15:30
Sebastian Pipping22-Feb-06 15:30 
GeneralRe: Listview controls Pin
Waldermort23-Feb-06 1:09
Waldermort23-Feb-06 1:09 
QuestionCComboBox dropdown bit is hidden Pin
rw10422-Feb-06 4:18
rw10422-Feb-06 4:18 
AnswerRe: CComboBox dropdown bit is hidden Pin
Iain Clarke, Warrior Programmer22-Feb-06 4:52
Iain Clarke, Warrior Programmer22-Feb-06 4:52 
GeneralRe: CComboBox dropdown bit is hidden Pin
rw10422-Feb-06 4:56
rw10422-Feb-06 4:56 

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.