473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.MyFunction(12, 34, ref strLog);
}
catch(System.Runtime.InteropServices.COMException ex)
{
string strErrMesg = ex.Message;
long hr = ex.ErrorCode;
MessageBox.Show(strLog); // <<==== always empty --- seems .Net looses
information.
}

Thanks
Dmitry
Nov 15 '05 #1
3 1321
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(UnmanagedType.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 PreserveSigAttribute
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.GetDescrription and Ex.Source
which is the string returned from IErrorInfo.GetSource.

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****************@TK2MSFTNGP09.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.MyFunction(12, 34, ref strLog);
}
catch(System.Runtime.InteropServices.COMException ex)
{
string strErrMesg = ex.Message;
long hr = ex.ErrorCode;
MessageBox.Show(strLog); // <<==== always empty --- seems .Net looses
information.
}

Thanks
Dmitry

Nov 15 '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****************@TK2MSFTNGP09.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(UnmanagedType.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 PreserveSigAttribute
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.GetDescrription and Ex.Source which is the string returned from IErrorInfo.GetSource.

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****************@TK2MSFTNGP09.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.MyFunction(12, 34, ref strLog);
}
catch(System.Runtime.InteropServices.COMException ex)
{
string strErrMesg = ex.Message;
long hr = ex.ErrorCode;
MessageBox.Show(strLog); // <<==== always empty --- seems .Net looses information.
}

Thanks
Dmitry


Nov 15 '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**************@TK2MSFTNGP09.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****************@TK2MSFTNGP09.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(UnmanagedType.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 PreserveSigAttribute
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.GetDescrription and

Ex.Source
which is the string returned from IErrorInfo.GetSource.

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****************@TK2MSFTNGP09.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.MyFunction(12, 34, ref strLog);
}
catch(System.Runtime.InteropServices.COMException ex)
{
string strErrMesg = ex.Message;
long hr = ex.ErrorCode;
MessageBox.Show(strLog); // <<==== always empty --- seems .Net

looses information.
}

Thanks
Dmitry



Nov 15 '05 #4

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

Similar topics

3
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...
22
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
1
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...
3
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"....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.