CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: COM Error

  1. #1
    Join Date
    Aug 2004
    Posts
    141

    COM Error

    Hi guys,

    I have a COM exe server, which I added the following interface:

    .h
    [id(184), helpstring("method GetChange")] HRESULT GetChange([out] BSTR *pVal);

    .cpp
    STDMETHODIMP CATL::GetChange(BSTR *pVal)
    {
    CString strCommand;

    bstr_t bstr( "Hello" );
    *pVal = bstr.copy( );

    return S_OK;
    }

    and I call it from my CLient like this:


    BSTR Change;
    ComObj->GetChange( & Change );


    The problem is everytime I get to execute this instruction my application gets an access violation error and the callstack shows
    RPCRT4! 7802e81c() as the method. It does not even get to the COM function, I debugged to make sure it's not the function that is causing this, any ideas why this is happening??/

    Thanks a lot

  2. #2
    Join Date
    Mar 2004
    Posts
    382

    Re: COM Error

    Can you show all COM relevant code in your client? I want to see how you initialize your COM server.

    Also, does your interface have any other methods? And if it does, are you able to call those methods successfully or not?

  3. #3
    Join Date
    Oct 2004
    Posts
    481

    Re: COM Error

    Hi JamesBond007,

    Try this:
    Code:
    STDMETHODIMP CATL::GetChange(BSTR *pVal)
    {
       CString strCommand;
    
       bstr_t bstr( "Hello" );
       //*pVal = bstr.copy( );
       ::memcpy(pVal, bstr, bstr.length());
    
       return S_OK;
    }
    Cheers

  4. #4
    Join Date
    Aug 2004
    Posts
    141

    Re: COM Error

    Thanks guys, but the problem is not in the code of the function, I put a breakpoint at the first instruction and it never reaches there.

    my server has about 180 method and properties and they all work great, I just added this one method and it crashes the client whenever I make the call.

    I get an access violation error. I'm thinking maybe the way I'm calling the interface, maybe I should not be using a BSTR as a param maybe a _bstr_t


    while we're on the subject, is it possible for the COM interface to have a CSTring & param that the client can pass to it to get initialized??? as in [out].


    Thanks

  5. #5
    Join Date
    Jun 2002
    Location
    Florida
    Posts
    32

    Re: COM Error

    I would suggest using CComBSTR instead of bstr_t. Im not sure if its legal to use bstr_t in the way you are using it.

    Edit: IIRC, COM does not allow the use of a bstr_t as an [out] parameter.

  6. #6
    Join Date
    Feb 2002
    Location
    Krispl, Austria
    Posts
    197

    Re: COM Error

    How did you add the Method? Did you use the IDE wizard or by hand. If by hand did you add the method to the IDL file?

  7. #7
    Join Date
    Aug 2004
    Posts
    141

    Re: COM Error

    I simply right clicked on the interface and add method, visual studio did all the rest.

    tx

  8. #8
    Join Date
    Feb 2002
    Location
    Krispl, Austria
    Posts
    197

    Re: COM Error

    Can you post the IDL file to here for me to look at.

  9. #9
    Join Date
    Apr 2000
    Location
    Boston
    Posts
    124

    Re: COM Error

    I know that you do not get into the code for the new method, but I keep seeing this:
    Code:
    BSTR Change;
    ComObj->GetChange( & Change );
    Change is just a pointer, no memory was allocated for this but your method will copy data into it. Will you change it to:
    Code:
    CComBSTR Change;
    ComObj->GetChange( & Change );

  10. #10
    Join Date
    Aug 2004
    Posts
    141

    Re: COM Error

    Hi Mike200,

    I removed the method and replaced it with a property and still get the same error.
    basically the property is this :

    .h
    STDMETHOD(get_Change)(/*[out, retval]*/ BSTR *pVal);

    .idl
    [propget, id(186), helpstring("property Change")] HRESULT Change([out, retval] BSTR *pVal);

    .cpp
    STDMETHODIMP CSwitchConnectionATL::get_LastConfigChange(BSTR *pVal)
    {
    // TODO: Add your implementation code here
    bstr_t bstr( "Hello", false);
    *pVal = bstr.copy( );

    return S_OK;
    }


    Thanks

  11. #11
    Join Date
    Apr 2000
    Location
    Boston
    Posts
    124

    Re: COM Error

    How are you calling the method? Are you still passing in only a pointer or a pointer to a buffer??
    Code:
    CComBSTR Change;
    ComObj->get_Change( & Change );

  12. #12
    Join Date
    Aug 2004
    Posts
    141

    Re: COM Error

    It's a property you don't pass a value, the property returns one.

    _bstr_t = ComObj->Change;


    I'm still thinking it has something to do with RPC since the error is return access violation and the call stack show the last call made was inside the RPCRT4 library.

    Thanks

  13. #13
    Join Date
    Aug 2004
    Posts
    141

    Re: COM Error

    Ok, I checked the typelib in the OleView tools, and my property that I added is not there, any ideas , remind you it's not a COM dll in process so registration is done basically when launch the exe.

    Thanks

  14. #14
    Join Date
    Apr 2000
    Location
    Boston
    Posts
    124

    Re: COM Error

    Isn't registration of a COM exe done by passing RegServer on the command line? For your app would it be ComObj.exe RegServer. The _tWinMain function should have the code for this. Have you tried reregistering the server?

  15. #15
    Join Date
    Aug 2004
    Posts
    141

    Re: COM Error

    Hi Mike200,

    Thanks for your patience, yes you're absolutely right, my problem I guess originated because I was having 2 versions of the server one that did not have the this method was registered, and when I added the method and compiled I launched the exe as you said but for some reason it did not update the entries of the interface in the typelib of OleView, I suspect because i did not speicify RegServer at the end of the command line, but now I did and it seems to have update the entries and it works fine.

    Thanks for you feedback, appreciate it.


    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured