473,654 Members | 3,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 21 '05 #1
3 1972
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" <dk***********@ fmco.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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

Jul 21 '05 #2
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]" <sa*@nomail.com > wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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" <dk***********@ fmco.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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


Jul 21 '05 #3
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" <dk***********@ fmco.com> wrote in message
news:O6******** ******@TK2MSFTN GP09.phx.gbl...
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]" <sa*@nomail.com > wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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" <dk***********@ fmco.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
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



Jul 21 '05 #4

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

Similar topics

22
3325
by: Dr Duck | last post by:
GDay all, Something seems odd to me.... I wrote a simple C# function public void bind(ref object a, ref object b, bool atob) { if(atob) b = a; else
3
1330
by: Dmitry | last post by:
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...
1
1266
by: Kris Takacs | last post by:
Hello, I recently upgraded a project from Visual Studio 6 to .NET and get the following error message when I call help: "The window name "REF" was not defined in your project file." If I click OK, the help comes up... Any ideas?
3
2316
exedotsree
by: exedotsree | last post by:
Hi Frnds My application is developed in vb.net When I run the application, the error message occurs.. Admin.exe-Application error The instruction at "0x10019e9d" ref mem at "0x79e4ffff". The memory couldnot be read. click OK to terminate, CANCEL to debug.
0
8814
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
8706
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...
0
8591
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...
0
7304
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2709
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
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1592
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.