Accessing COM Component arguments defined by "ref", after it raise COM+ error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dmitry

    #1

    Accessing COM Component arguments defined by "ref", after it raise COM+ error

    Hi,

    I have defined interface for COM components which inludes an argument being
    filled with additional error info, if such occurs. If inside I raise COM
    Error, I populate that parameter.
    In COM environment this architecture works beautifully -- caller application
    gets negative HRESULT and error description from IErrorInfo object, and
    additional "log" information from that "ref" parameter:

    In .Net it just never returns that paramter populated!
    Can any one help?

    COM IDL declaration:
    HRESULT MyFunction([in] LONG lngA, [in] LONG lngB, [in, out] BSTR
    *bstrLog)

    In C++ Code:
    .....
    // error happened --> raise COM Error, fill log
    *bstrLog = SysAllocString( "something bad happened");
    IErrorInfo *pErr ........ //code to raise error.
    .....

    In .Net

    MyLib.MyClass objFunc = new MyLib.MyClass()

    string strLog = "";
    try
    {
    objFunc.MyFunct ion(12, 34, ref strLog);
    }
    catch(System.Ru ntime.InteropSe rvices.COMExcep tion ex)
    {
    string strErrMesg = ex.Message;
    long hr = ex.ErrorCode;
    MessageBox.Show (strLog); // <<==== always empty --- seems .Net looses
    information.
    }

    Thanks
    Dmitry


  • Sam Gentile [MVP]

    #2
    Re: Accessing COM Component arguments defined by &quot;ref&quot; , after it raise COM+ error

    First of all HRESULT types don't exist in the .NET Framework so they become
    32-bit integers in managed code with the MarshalAs attribute applied.

    HRESULT Foo([in] HRESULT hr)

    gets marshaled as:

    public virtual Foo([MarshalAs(Unman agedType.Error] int hr);

    Any success HRESULT values are swallowed by the RCW and the values are
    unattainable from .NET clients unless the managed signature coresponding to
    the method returning the HRESULT is marked with the PreserveSigAttr ibute
    pseudo-custom attribute.

    This all turns out to be a pain so you you shouldn't write interfaces like
    this if you plan to call them for managed clients. Instead the CLR takes the
    Exception object's properties with data from the COM error object. So
    instead of your strLog variable which didn't get marshaled correctly and
    swallowed, you should be looking in and printing out the ex properties like
    ex.Message which gets stuffed with IErrorInfo.GetD escrription and Ex.Source
    which is the string returned from IErrorInfo.GetS ource.

    HTH,
    --
    Sam Gentile [C#/.NET MVP]
    ..NET Blog http://samgentile.com/blog/
    MSDN Column:
    http://msdn.microsoft.com/library/de...tml/bridge.asp
    Please reply only to the newsgroup so that others can benefit.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    "Dmitry" <dkhodorkovsky@ fmco.com> wrote in message
    news:%232crd6b6 DHA.2560@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > Hi,
    >
    > I have defined interface for COM components which inludes an argument[/color]
    being[color=blue]
    > filled with additional error info, if such occurs. If inside I raise COM
    > Error, I populate that parameter.
    > In COM environment this architecture works beautifully -- caller[/color]
    application[color=blue]
    > gets negative HRESULT and error description from IErrorInfo object, and
    > additional "log" information from that "ref" parameter:
    >
    > In .Net it just never returns that paramter populated!
    > Can any one help?
    >
    > COM IDL declaration:
    > HRESULT MyFunction([in] LONG lngA, [in] LONG lngB, [in, out] BSTR
    > *bstrLog)
    >
    > In C++ Code:
    > ....
    > // error happened --> raise COM Error, fill log
    > *bstrLog = SysAllocString( "something bad happened");
    > IErrorInfo *pErr ........ //code to raise error.
    > ....
    >
    > In .Net
    >
    > MyLib.MyClass objFunc = new MyLib.MyClass()
    >
    > string strLog = "";
    > try
    > {
    > objFunc.MyFunct ion(12, 34, ref strLog);
    > }
    > catch(System.Ru ntime.InteropSe rvices.COMExcep tion ex)
    > {
    > string strErrMesg = ex.Message;
    > long hr = ex.ErrorCode;
    > MessageBox.Show (strLog); // <<==== always empty --- seems .Net looses
    > information.
    > }
    >
    > Thanks
    > Dmitry
    >
    >[/color]


    Comment

    • Dmitry

      #3
      Re: Accessing COM Component arguments defined by &quot;ref&quot; , after it raise COM+ error

      Sam,

      strLog -- may contain from 1 to 10 K of data, depending on number of
      errors, warnings coming from COM object. so packing that data in ErrorInfo
      properties might be waste of resource.
      Code is written for interaction within COM environment, but now I am looking
      in compatibility with .Net callers.

      "Sam Gentile [MVP]" <sam@nomail.com > wrote in message
      news:%23dsY3Kc6 DHA.2732@TK2MSF TNGP09.phx.gbl. ..[color=blue]
      > First of all HRESULT types don't exist in the .NET Framework so they[/color]
      become[color=blue]
      > 32-bit integers in managed code with the MarshalAs attribute applied.
      >
      > HRESULT Foo([in] HRESULT hr)
      >
      > gets marshaled as:
      >
      > public virtual Foo([MarshalAs(Unman agedType.Error] int hr);
      >
      > Any success HRESULT values are swallowed by the RCW and the values are
      > unattainable from .NET clients unless the managed signature coresponding[/color]
      to[color=blue]
      > the method returning the HRESULT is marked with the PreserveSigAttr ibute
      > pseudo-custom attribute.
      >
      > This all turns out to be a pain so you you shouldn't write interfaces like
      > this if you plan to call them for managed clients. Instead the CLR takes[/color]
      the[color=blue]
      > Exception object's properties with data from the COM error object. So
      > instead of your strLog variable which didn't get marshaled correctly and
      > swallowed, you should be looking in and printing out the ex properties[/color]
      like[color=blue]
      > ex.Message which gets stuffed with IErrorInfo.GetD escrription and[/color]
      Ex.Source[color=blue]
      > which is the string returned from IErrorInfo.GetS ource.
      >
      > HTH,
      > --
      > Sam Gentile [C#/.NET MVP]
      > .NET Blog http://samgentile.com/blog/
      > MSDN Column:
      >[/color]
      http://msdn.microsoft.com/library/de...tml/bridge.asp[color=blue]
      > Please reply only to the newsgroup so that others can benefit.
      >
      >
      > This posting is provided "AS IS" with no warranties, and confers no[/color]
      rights.[color=blue]
      > "Dmitry" <dkhodorkovsky@ fmco.com> wrote in message
      > news:%232crd6b6 DHA.2560@TK2MSF TNGP09.phx.gbl. ..[color=green]
      > > Hi,
      > >
      > > I have defined interface for COM components which inludes an argument[/color]
      > being[color=green]
      > > filled with additional error info, if such occurs. If inside I raise COM
      > > Error, I populate that parameter.
      > > In COM environment this architecture works beautifully -- caller[/color]
      > application[color=green]
      > > gets negative HRESULT and error description from IErrorInfo object, and
      > > additional "log" information from that "ref" parameter:
      > >
      > > In .Net it just never returns that paramter populated!
      > > Can any one help?
      > >
      > > COM IDL declaration:
      > > HRESULT MyFunction([in] LONG lngA, [in] LONG lngB, [in, out] BSTR
      > > *bstrLog)
      > >
      > > In C++ Code:
      > > ....
      > > // error happened --> raise COM Error, fill log
      > > *bstrLog = SysAllocString( "something bad happened");
      > > IErrorInfo *pErr ........ //code to raise error.
      > > ....
      > >
      > > In .Net
      > >
      > > MyLib.MyClass objFunc = new MyLib.MyClass()
      > >
      > > string strLog = "";
      > > try
      > > {
      > > objFunc.MyFunct ion(12, 34, ref strLog);
      > > }
      > > catch(System.Ru ntime.InteropSe rvices.COMExcep tion ex)
      > > {
      > > string strErrMesg = ex.Message;
      > > long hr = ex.ErrorCode;
      > > MessageBox.Show (strLog); // <<==== always empty --- seems .Net[/color][/color]
      looses[color=blue][color=green]
      > > information.
      > > }
      > >
      > > Thanks
      > > Dmitry
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Sam Gentile [MVP]

        #4
        Re: Accessing COM Component arguments defined by &quot;ref&quot; , after it raise COM+ error

        I am describing what the COM Interop marshaler does to your COM types in
        ..NET. You *don't* get a choice (unless you write a custom RCW or marshaler)

        --
        Sam Gentile [C#/.NET MVP]
        ..NET Blog http://samgentile.com/blog/
        MSDN Column:
        http://msdn.microsoft.com/library/de...tml/bridge.asp
        Please reply only to the newsgroup so that others can benefit.
        This posting is provided "AS IS" with no warranties, and confers no rights.


        "Dmitry" <dkhodorkovsky@ fmco.com> wrote in message
        news:O6YsZXc6DH A.2732@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Sam,
        >
        > strLog -- may contain from 1 to 10 K of data, depending on number of
        > errors, warnings coming from COM object. so packing that data in ErrorInfo
        > properties might be waste of resource.
        > Code is written for interaction within COM environment, but now I am[/color]
        looking[color=blue]
        > in compatibility with .Net callers.
        >
        > "Sam Gentile [MVP]" <sam@nomail.com > wrote in message
        > news:%23dsY3Kc6 DHA.2732@TK2MSF TNGP09.phx.gbl. ..[color=green]
        > > First of all HRESULT types don't exist in the .NET Framework so they[/color]
        > become[color=green]
        > > 32-bit integers in managed code with the MarshalAs attribute applied.
        > >
        > > HRESULT Foo([in] HRESULT hr)
        > >
        > > gets marshaled as:
        > >
        > > public virtual Foo([MarshalAs(Unman agedType.Error] int hr);
        > >
        > > Any success HRESULT values are swallowed by the RCW and the values are
        > > unattainable from .NET clients unless the managed signature coresponding[/color]
        > to[color=green]
        > > the method returning the HRESULT is marked with the PreserveSigAttr ibute
        > > pseudo-custom attribute.
        > >
        > > This all turns out to be a pain so you you shouldn't write interfaces[/color][/color]
        like[color=blue][color=green]
        > > this if you plan to call them for managed clients. Instead the CLR takes[/color]
        > the[color=green]
        > > Exception object's properties with data from the COM error object. So
        > > instead of your strLog variable which didn't get marshaled correctly and
        > > swallowed, you should be looking in and printing out the ex properties[/color]
        > like[color=green]
        > > ex.Message which gets stuffed with IErrorInfo.GetD escrription and[/color]
        > Ex.Source[color=green]
        > > which is the string returned from IErrorInfo.GetS ource.
        > >
        > > HTH,
        > > --
        > > Sam Gentile [C#/.NET MVP]
        > > .NET Blog http://samgentile.com/blog/
        > > MSDN Column:
        > >[/color]
        >[/color]
        http://msdn.microsoft.com/library/de...tml/bridge.asp[color=blue][color=green]
        > > Please reply only to the newsgroup so that others can benefit.
        > >
        > >
        > > This posting is provided "AS IS" with no warranties, and confers no[/color]
        > rights.[color=green]
        > > "Dmitry" <dkhodorkovsky@ fmco.com> wrote in message
        > > news:%232crd6b6 DHA.2560@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
        > > > Hi,
        > > >
        > > > I have defined interface for COM components which inludes an argument[/color]
        > > being[color=darkred]
        > > > filled with additional error info, if such occurs. If inside I raise[/color][/color][/color]
        COM[color=blue][color=green][color=darkred]
        > > > Error, I populate that parameter.
        > > > In COM environment this architecture works beautifully -- caller[/color]
        > > application[color=darkred]
        > > > gets negative HRESULT and error description from IErrorInfo object,[/color][/color][/color]
        and[color=blue][color=green][color=darkred]
        > > > additional "log" information from that "ref" parameter:
        > > >
        > > > In .Net it just never returns that paramter populated!
        > > > Can any one help?
        > > >
        > > > COM IDL declaration:
        > > > HRESULT MyFunction([in] LONG lngA, [in] LONG lngB, [in, out] BSTR
        > > > *bstrLog)
        > > >
        > > > In C++ Code:
        > > > ....
        > > > // error happened --> raise COM Error, fill log
        > > > *bstrLog = SysAllocString( "something bad happened");
        > > > IErrorInfo *pErr ........ //code to raise error.
        > > > ....
        > > >
        > > > In .Net
        > > >
        > > > MyLib.MyClass objFunc = new MyLib.MyClass()
        > > >
        > > > string strLog = "";
        > > > try
        > > > {
        > > > objFunc.MyFunct ion(12, 34, ref strLog);
        > > > }
        > > > catch(System.Ru ntime.InteropSe rvices.COMExcep tion ex)
        > > > {
        > > > string strErrMesg = ex.Message;
        > > > long hr = ex.ErrorCode;
        > > > MessageBox.Show (strLog); // <<==== always empty --- seems .Net[/color][/color]
        > looses[color=green][color=darkred]
        > > > information.
        > > > }
        > > >
        > > > Thanks
        > > > Dmitry
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...