473,804 Members | 3,204 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

COMException HResults not Transferred to MFC Client

I'm trying to send custom COMExceptions from my C# server. My test client in
C# has no problem reading the HRESULT I send (say, 0x80040002). Needless to
say, the MFC client in C++ allows this HRESULT only to the .tli file, which I
assume is part of the client. The next thing that happens, it is converted to
an SEHException with a value of 0x80004005. This happens everytime, and does
it for any derivative of SEHException.

Is there some attribute of decoration I need to make?

I can post the code, it is pretty simple.
Feb 8 '06 #1
4 5332
Hi,

First of all, how to you throw custom COMExceptions? I'd recommend that you
did it with Marshal.ThrowEx ceptionFromHR.

Also, the exception code itself looks suspicious as it can easily interfer
with system error codes. As far as I remember, it is safe to use codes from
0x80040201 and above for custom errors (VB6 has a dedicated constant for
that called vbObjectError).

Finally, as I am not an MFC programmer, can you please elaborate on what the
..tli file is?

"dynastar" <u18512@uwe> wrote in message news:5b8bd64680 cf0@uwe...
I'm trying to send custom COMExceptions from my C# server. My test client
in
C# has no problem reading the HRESULT I send (say, 0x80040002). Needless
to
say, the MFC client in C++ allows this HRESULT only to the .tli file,
which I
assume is part of the client. The next thing that happens, it is converted
to
an SEHException with a value of 0x80004005. This happens everytime, and
does
it for any derivative of SEHException.

Is there some attribute of decoration I need to make?

I can post the code, it is pretty simple.

Feb 8 '06 #2
Hi Dmytro,
This is the entire server class (ServerTest assembly):

using System;
using System.Runtime. Remoting;
using System.Runtime. Remoting.Lifeti me; // to set the lease to infinite
using System.Runtime. InteropServices ;

namespace ServerTest
{
/// <summary>
/// Summary description for IOComponent.
/// </summary>
public class Class1 : MarshalByRefObj ect, ServerData
{
static int m_EC;
#region Marshal By Reference Methods
public override Object InitializeLifet imeService()
{
return null;
}
#endregion
public Class1()
{
m_EC = 0;
}
public int MAKE_HRESULT(in t sev, int fac, int code)
{
return (((sev)<<31) | ((fac)<<16) | ((code)));
}

#region ServerData Members

public void ThrowException( )
{
int hRes = MAKE_HRESULT(1, 4,(int)m_EC++);
throw new COMException("M y new exception",hRes );
// Creates an
HRESULT of 0x80040000
}

#endregion
}
}

The ServerData interface is this:
using System;

namespace ServerTest
{
/// <summary>
/// Summary description for serverInterface .
/// </summary>
public interface ServerData
{
// methods
void ThrowException( );
}
}
To use this, the #import of the server code creates a secondary header file (.
tli) from the .tlb.
Here's my code for the client proxy, also written in C#:
// ServerProxy.cs

using System;
using ServerTest;
using System.Runtime. Remoting;
using System.Runtime. Remoting.Lifeti me; // to set the lease to infinite
using System.Runtime. InteropServices ;

namespace COMServerPS
{
/// <summary>
/// Summary description for ServerProxy.
/// </summary>
public class ServerProxy : MarshalByRefObj ect, ServerData
{
public Class1 _Server; // object

#region Marshal By Reference Methods
public override Object InitializeLifet imeService()
{
return null;
}
#endregion

public ServerProxy()
{
_Server = new Class1();
}
#region ServerData Members

[PreserveSig]
public void ThrowException( )
{
try
{
_Server.ThrowEx ception();
}
catch (Exception ex)
{
int hr = Marshal.GetHRFo rException(ex);
throw ex;
}
}

#endregion
}
}

This is the MFC header file:
// WindowsMFCAppli cationDlg.h : header file
//

#pragma once
#import "COMServerProxy .tlb" named_guids
#import "ServerTest.tlb " named_guids
// CWindowsMFCAppl icationDlg dialog
class CWindowsMFCAppl icationDlg : public CDialog
{
// Construction
public:
CWindowsMFCAppl icationDlg(CWnd * pParent = NULL); // standard constructor

// Dialog Data
....
// Namespace(from .tlb)::Interfac ePtr
ServerTest::Ser verDataPtr m_ioServer;

// Implementation
protected:
....

public:
afx_msg void OnBnClickedButt on1();
};

And the implementation file:
// WindowsMFCAppli cationDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WindowsMFCAppl ication.h"
#include "WindowsMFCAppl icationDlg.h"
#include ".\windowsmfcap plicationdlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

using namespace System;
using namespace System::Runtime ::InteropServic es;

....
BOOL CWindowsMFCAppl icationDlg::OnI nitDialog()
{
CDialog::OnInit Dialog();
....
// must call CoInitialize() at least once prior to calling CreateInstance( )
HRESULT hRes = CoInitialize(NU LL);

// CreateInstance( Assembly Name::Class Name)
hRes = m_ioServer.Crea teInstance(__uu idof(COMServerP roxy::ServerPro xy));

return TRUE; // return TRUE unless you set the focus to a control
}

void CWindowsMFCAppl icationDlg::OnB nClickedButton1 ()
{
HRESULT hr = S_OK;
try
{
hr = m_ioServer->ThrowException ();
}
catch(COMExcept ion* ex )
{
int ec = ex->get_ErrorCode( );
Exception *exc = ex->GetBaseExcepti on();

hr = Marshal::GetHRF orException(ex) ;
}
catch(Exception * ex )
{
hr = Marshal::GetHRF orException(ex) ;
// HRESULT is 0x80004005
}
}

Tha's about it. It works between the managed types, but not unmanged C++.

Thanks,
Steve
Dmytro Lapshyn [MVP] wrote:
Hi,

First of all, how to you throw custom COMExceptions? I'd recommend that you
did it with Marshal.ThrowEx ceptionFromHR.

Also, the exception code itself looks suspicious as it can easily interfer
with system error codes. As far as I remember, it is safe to use codes from
0x80040201 and above for custom errors (VB6 has a dedicated constant for
that called vbObjectError).

Finally, as I am not an MFC programmer, can you please elaborate on what the
.tli file is?
I'm trying to send custom COMExceptions from my C# server. My test client
in

[quoted text clipped - 11 lines]

I can post the code, it is pretty simple.


--
Message posted via DotNetMonster.c om
http://www.dotnetmonster.com/Uwe/For...sharp/200602/1
Feb 8 '06 #3
Hi,

Please try using Marshal.ThrowEx ceptionForHR and correct HR codes first. If
this does not help, please follow up in this thread.

I am also curious why do you inherit your server class from
MarshalByRefObj ect. Is your server an out-of-proc (DCOM) server?

"dynastar via DotNetMonster.c om" <u18512@uwe> wrote in message
news:5b9337f2be a1d@uwe...
Hi Dmytro,
This is the entire server class (ServerTest assembly):

using System;
using System.Runtime. Remoting;
using System.Runtime. Remoting.Lifeti me; // to set the lease to infinite
using System.Runtime. InteropServices ;

namespace ServerTest
{
/// <summary>
/// Summary description for IOComponent.
/// </summary>
public class Class1 : MarshalByRefObj ect, ServerData
{
static int m_EC;
#region Marshal By Reference Methods
public override Object InitializeLifet imeService()
{
return null;
}
#endregion
public Class1()
{
m_EC = 0;
}
public int MAKE_HRESULT(in t sev, int fac, int code)
{
return (((sev)<<31) | ((fac)<<16) | ((code)));
}

#region ServerData Members

public void ThrowException( )
{
int hRes = MAKE_HRESULT(1, 4,(int)m_EC++);
throw new COMException("M y new exception",hRes );
// Creates
an
HRESULT of 0x80040000
}

#endregion
}
}

The ServerData interface is this:
using System;

namespace ServerTest
{
/// <summary>
/// Summary description for serverInterface .
/// </summary>
public interface ServerData
{
// methods
void ThrowException( );
}
}
To use this, the #import of the server code creates a secondary header
file (.
tli) from the .tlb.
Here's my code for the client proxy, also written in C#:
// ServerProxy.cs

using System;
using ServerTest;
using System.Runtime. Remoting;
using System.Runtime. Remoting.Lifeti me; // to set the lease to infinite
using System.Runtime. InteropServices ;

namespace COMServerPS
{
/// <summary>
/// Summary description for ServerProxy.
/// </summary>
public class ServerProxy : MarshalByRefObj ect, ServerData
{
public Class1 _Server; // object

#region Marshal By Reference Methods
public override Object InitializeLifet imeService()
{
return null;
}
#endregion

public ServerProxy()
{
_Server = new Class1();
}
#region ServerData Members

[PreserveSig]
public void ThrowException( )
{
try
{
_Server.ThrowEx ception();
}
catch (Exception ex)
{
int hr = Marshal.GetHRFo rException(ex);
throw ex;
}
}

#endregion
}
}

This is the MFC header file:
// WindowsMFCAppli cationDlg.h : header file
//

#pragma once
#import "COMServerProxy .tlb" named_guids
#import "ServerTest.tlb " named_guids
// CWindowsMFCAppl icationDlg dialog
class CWindowsMFCAppl icationDlg : public CDialog
{
// Construction
public:
CWindowsMFCAppl icationDlg(CWnd * pParent = NULL); // standard constructor

// Dialog Data
...
// Namespace(from .tlb)::Interfac ePtr
ServerTest::Ser verDataPtr m_ioServer;

// Implementation
protected:
...

public:
afx_msg void OnBnClickedButt on1();
};

And the implementation file:
// WindowsMFCAppli cationDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WindowsMFCAppl ication.h"
#include "WindowsMFCAppl icationDlg.h"
#include ".\windowsmfcap plicationdlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

using namespace System;
using namespace System::Runtime ::InteropServic es;

...
BOOL CWindowsMFCAppl icationDlg::OnI nitDialog()
{
CDialog::OnInit Dialog();
...
// must call CoInitialize() at least once prior to calling
CreateInstance( )
HRESULT hRes = CoInitialize(NU LL);

// CreateInstance( Assembly Name::Class Name)
hRes = m_ioServer.Crea teInstance(__uu idof(COMServerP roxy::ServerPro xy));

return TRUE; // return TRUE unless you set the focus to a control
}

void CWindowsMFCAppl icationDlg::OnB nClickedButton1 ()
{
HRESULT hr = S_OK;
try
{
hr = m_ioServer->ThrowException ();
}
catch(COMExcept ion* ex )
{
int ec = ex->get_ErrorCode( );
Exception *exc = ex->GetBaseExcepti on();

hr = Marshal::GetHRF orException(ex) ;
}
catch(Exception * ex )
{
hr = Marshal::GetHRF orException(ex) ;
// HRESULT is 0x80004005
}
}

Tha's about it. It works between the managed types, but not unmanged C++.

Thanks,
Steve
Dmytro Lapshyn [MVP] wrote:
Hi,

First of all, how to you throw custom COMExceptions? I'd recommend that
you
did it with Marshal.ThrowEx ceptionFromHR.

Also, the exception code itself looks suspicious as it can easily interfer
with system error codes. As far as I remember, it is safe to use codes
from
0x80040201 and above for custom errors (VB6 has a dedicated constant for
that called vbObjectError).

Finally, as I am not an MFC programmer, can you please elaborate on what
the
.tli file is?
I'm trying to send custom COMExceptions from my C# server. My test
client
in

[quoted text clipped - 11 lines]

I can post the code, it is pretty simple.


--
Message posted via DotNetMonster.c om
http://www.dotnetmonster.com/Uwe/For...sharp/200602/1

Feb 9 '06 #4
Hi Dmytro,
This is a remoted server which is why I use the MarshalByRefObj ect. I have
been working with C# servers for over a year now, and ran in to this problem
early on. Normally, COM objects return HRESULTs that can simply be assigned
as the STDMETHODIMP return value to the call. I simply want to maintain this
functionality because I have to deal with legacy code.

I have now tried your suggestion but get the same result: the 0x80040001
HRESULT becomes 0x80004005 and I must use try..catch blocks. There is
something in the layer between the c# agent and the MFC proxy that is
modifying the HRESULT to the base class (SEHException) HRESULT.

Thanks,
Steve

Dmytro Lapshyn [MVP] wrote:
Hi,

Please try using Marshal.ThrowEx ceptionForHR and correct HR codes first. If
this does not help, please follow up in this thread.

I am also curious why do you inherit your server class from
MarshalByRefOb ject. Is your server an out-of-proc (DCOM) server?
Hi Dmytro,
This is the entire server class (ServerTest assembly):

[quoted text clipped - 216 lines]

I can post the code, it is pretty simple.


--
Message posted via DotNetMonster.c om
http://www.dotnetmonster.com/Uwe/For...sharp/200602/1
Feb 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1586
by: MikeH | last post by:
I have the weirdest problem with an Access 97 database... The application comprises the usual front-back split database. It's built around Access 97 and had been running without serious problems for around seven years. The back-ends are replicated across two servers (althugh I don't think this is a replication problem). Around two weeks ago users suddenly started to complain about slow performance. One particular operation that used...
0
1023
by: lakshmi | last post by:
Hi all I posted a related question a couple days back. I did get a few helpful replies. However, I've not yet figured out the solution. I've read that passing HRESULTs back from .NET to COM is not a good practice. Exceptions should be thrown and sent back to COM. I'm rewriting a C++ program in C#. The C++ program used to
3
2766
by: Shmulik | last post by:
I have an application written in C# that creates a queue of items to process, connects via a TCP connection to a server on a Unix box, and then passes data files to the Unix box for processing (cluster) - normally this works fine. However, on the front end, another Unix system generates and transfers files to the DotNet application (Management system/controller) and recently I've witnessed the following: 1) Unix Box "A" generates data...
2
2662
by: Jim Lacenski | last post by:
I have a VB class that uses .NET and ADODB to write into an Excel spreadsheet (via Jet) on a server as part of a web application. ADODB is used instead of ADO.NET because it greatly simplifies the write process, and is supported for use on a server. (Excel is not supported (1), licensing issues with OWC). The routine works fine for a user at the server, but when a user from a system other than the server runs the page the error...
2
2335
by: Sabi | last post by:
hi, I have created a collection of serviced components that performs money transaction between 5 schools. I have marked all the components with Transaction(TransactionOption.Required) attribute. All the methods in the components are marked with the AutoComplete() attribute. But when I run the application, it is showing incorrect balance amount are being transferred. When I debug, I am getting this exception: System....
3
5609
by: Jay A. Moritz | last post by:
I have an application that loads IE and iterates through web pages to retrieve information from the page then activates specific controls to retrieve the next page and iterate through that page for it's information, etc, etc. I have found many articles from people getting COMException errors when trying to use Interop.SHDocVw from a windows service, so it's pretty apparent to me that this is a difficult application to build, but I have...
0
1141
by: =?Utf-8?B?QnVyZ2F6b24=?= | last post by:
Hi, I have a COM-.NET exception handling problem and I would appreciate your help. I have a COM server that “throws exception” from time to time (the COM object implements ISupportErrorInfo and generates IErrorInfo objects upon errors). The IErrorInfo object that is generated by my COM server extends the interface IErrorInfo by adding some additional properties (for example: MoreDetails) – let’s say the name of the extended...
1
2203
by: TheFid | last post by:
In case someone knows off the top.... I am accessing a COM-interface-exposing C# dll in VC++ 6. The C# disp method can throw a COMException. Do I catch a _com_error ? It's not obvious from the docs. TIA, Mike
19
9793
by: Kapps | last post by:
Hi, I'm having a problem with my program not being able to run on certain computers. They have the basics, such as the .Net Frameworks. Upon trying to run the program, it gives the error: Microsoft Windows EQ Client.exe has stopped working
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10578
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.