473,405 Members | 2,185 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,405 software developers and data experts.

COM <-> .Net Exception handling

I have a COM object that calls into a C# Forms library. The library can throw exceptions and I want to handle the exceptions in COM. Adam Nathan wrote a Microsoft sponsored book titled .Net and Com the complete interoperability guide which shows examples of how to do this.

Below please find the book example code for getting extended error information from a V-Table bound .Net component. This code does not compile because the compiler gives a C1001 (Internal Compiler Error)

If I remove the #import for mscorlib.tlb no_namespace named_guids then the compiler does not complain but I can no longer access _Exception and other interfaces which are referenced in the core lib type library. I have also tried changing the import to a later version of the type lib

Is there a way to reliably pass exceptions between the 2 worlds

Mar

---------------------------------------------------------- Example (it is supposed to build wtih cl filename)----------------------------------------------------------------------------------

#define _WIN32_DCO
#include <stdio.h
#include <wtypes.h

#import "c:\\Windows\\Microsoft.NET\\Framework\\v1.0.3705\ \mscorlib.tlb" no_namespace named_guids raw_interfaces_onl

int main(int argc, char* argv[]

IUnknown* pUnk = NULL
IList* pList = NULL
ISupportErrorInfo* pSuppErrInfo = NULL
IErrorInfo* pErrInfo = NULL
_Exception* pException = NULL
_Type* pType = NULL
HRESULT hresult

// Initialize CO
hresult = CoInitializeEx(NULL, COINIT_MULTITHREADED)
if (FAILED(hresult))

printf("ERROR: Cannot initialize COM: 0x%x\n", hresult)
return -1
// Instantiate the System.Collections.ArrayList clas
hresult = CoCreateInstance(CLSID_ArrayList, NULL, CLSCTX_INPROC_SERVER,
IID_IUnknown, (void **)&pUnk)
if (FAILED(hresult))
{
printf("ERROR: Cannot create object: 0x%x\n", hresult)
return -1
// Get the IList interfac
hresult = pUnk->QueryInterface(IID_IList, (void**)&pList)
if (FAILED(hresult))
{
printf("ERROR: Cannot obtain the IList interface pointer: 0x%x\n"
hresult)
pUnk->Release()
return -1
hresult = pList->RemoveAt(1)
if (FAILED(hresult))
{
printf("ERROR: RemoveAt failed: 0x%x\n\n", hresult)

// Check if the object supports extended error informatio
hresult = pList->QueryInterface(IID_ISupportErrorInfo
(void**)&pSuppErrInfo)
if (SUCCEEDED(hresult)

hresult = pSuppErrInfo->InterfaceSupportsErrorInfo(IID_IList)
if (hresult == S_OK)

// Attempt to get the IErrorInfo pointe
hresult = GetErrorInfo(0, &pErrInfo)
if (SUCCEEDED(hresult)

BSTR source = NULL
BSTR description = NULL
BSTR helpFile = NULL
unsigned long helpContext

hresult = pErrInfo->GetSource(&source)
if (SUCCEEDED(hresult)) printf("Source: %S\n\n",
source)

hresult = pErrInfo->GetDescription(&description)
if (SUCCEEDED(hresult)) printf("Description: %S\n\n"
description)

hresult = pErrInfo->GetHelpFile(&helpFile)
if (SUCCEEDED(hresult)) printf("HelpFile: %S\n\n",
helpFile)

hresult = pErrInfo->GetHelpContext(&helpContext)
if (SUCCEEDED(hresult)) printf("HelpContext: %d\n\n"
helpContext)

if (source != NULL) SysFreeString(source)
if (description != NULL) SysFreeString(description)
if (helpFile != NULL) SysFreeString(helpFile)

// Call members of the .NET exception objec
BSTR stackTrace = NULL
BSTR toString = NULL
BSTR exceptionTypeName = NULL

hresult = pErrInfo->QueryInterface(IID__Exception
(void**)&pException)
if (SUCCEEDED(hresult)

hresult = pException->get_StackTrace(&stackTrace)
if (SUCCEEDED(hresult)) printf("Stack Trace: %S\n\n",
stackTrace)

hresult = pException->get_ToString(&toString)
if (SUCCEEDED(hresult)) printf("ToString: %S\n\n",
toString);

hresult = pException->GetType(&pType);
if (SUCCEEDED(hresult))
{
hresult = pType->get_ToString(&exceptionTypeName);
if (SUCCEEDED(hresult)) printf("Exception Type: %S\n\n",
exceptionTypeName);
}

if (toString != NULL) SysFreeString(toString);
if (stackTrace != NULL) SysFreeString(stackTrace);
if (exceptionTypeName != NULL)
SysFreeString(exceptionTypeName);
}
}
}
}
}

// Release interface pointers
if (pUnk != NULL) pUnk->Release();
if (pList != NULL) pList->Release();
if (pSuppErrInfo != NULL) pSuppErrInfo->Release();
if (pErrInfo != NULL) pErrInfo->Release();
if (pException != NULL) pException->Release();
if (pType != NULL) pType->Release();

CoUninitialize();
return 0;
}
Nov 17 '05 #1
4 3872
Mark wrote:
I have a COM object that calls into a C# Forms library. The library
can throw exceptions and I want to handle the exceptions in COM.
Adam Nathan wrote a Microsoft sponsored book titled .Net and Com the
complete interoperability guide which shows examples of how to do
this.

Below please find the book example code for getting extended error
information from a V-Table bound .Net component. This code does not
compile because the compiler gives a C1001 (Internal Compiler Error).


What compiler are you using? I don't havbe VC7 (VS.NET 2003) installed, but
this example compiled and ran fine with VC 7.1 (VS.NET 2003). (I also
changed the #import to refer to the 1.1 framework).

-cd
Nov 17 '05 #2
Mark wrote:
Hi Carl,

cl /? displays - Microsoft (R) 32-bit C/C++ Optimizing Compiler
Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

The about box of Visual Studio says Version 7.1.3088 of the
Development Environment and Version 1.1.4322 of the framework.


Well, that's VC7.1, and the example you posted compiled & ran fine for me
using that version. You're encountering something "strange". Anything else
that might be unique (or less common) about your development environment?
What OS are you using?

-cd
Nov 17 '05 #3
I am compiling on XP Professional Version 2002 Service Pack 1. I have both .Net 1.0 and 1.1 installed but i know the typlelib being loaded is the one referenced in the source file

The machice is an IBM Thinkpad and it has 1gb of memory

When you do cl /? do you also get Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86? This version number isn't obviously related to VS version 7.1.
Nov 17 '05 #4
Mark wrote:
I am compiling on XP Professional Version 2002 Service Pack 1. I
have both .Net 1.0 and 1.1 installed but i know the typlelib being
loaded is the one referenced in the source file.

The machice is an IBM Thinkpad and it has 1gb of memory.

When you do cl /? do you also get Microsoft (R) 32-bit C/C++
Optimizing Compiler Version 13.10.3077 for 80x86? This version
number isn't obviously related to VS version 7.1.


Yes, that's the same version, and the same OS - XP Pro SP1. I too have .NET
1.0 and 1.1 installed, yet the example worked perfectly for me.

cl version 13.10 = VC version 7.10 (VC1 was cl version 7.00).

Hmm. I'm out of ideas at the moment.

-cd
Nov 17 '05 #5

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

Similar topics

5
by: Jonny T | last post by:
hi, i want to echo the string '<?php' in a php script like : echo "<?php"; but when i try nothing gets displayed ... if I use echo "<\?php";
7
by: haoren | last post by:
Can anybody help me with this problem: How can I echo a string that contain <? and <?php? For example, $str="test <? and <?php echo"; echo $str;
3
by: winderjj | last post by:
Hi All, I need everyones opinion. I am very new to XML but am temporarily putting all my efforts into using it. This is what I need to do. Write an xml parser (in C) that will parse a...
4
by: matatu | last post by:
Hi to all, I have a xml file, a substring like: &lt;a href=&quot;#&quot;&gt;text&lt;/a&gt; which after an xslt trasform is rendered as (using xsl:output method html): &lt;a...
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
5
by: tobbe | last post by:
Hi Im trying to load a XmlDataDocument with the following xml: <ROOT> <NAME> &LT; &AMP; &GT; " '</NAME> </ROOT> And i know I have a entity problem here, but i cant find any solution for...
10
by: Jon Noring | last post by:
Out of curiosity, may a CDATA section appear within an attribute value with datatype CDATA? And if so, how about other attribute value datatypes which accept the XML markup characters? To me,...
4
by: Armel Asselin | last post by:
Hello, I've been using XML for a while in a rather 'free' manner (i.e. as long as IE accept it it's OK), I read recently (again) the Xml standard 1.0 (3rd edition) and found this sentence: ...
4
by: spibou | last post by:
I saw it at http://www.faqs.org/rfcs/rfc1305.html Is it not the same as writing ``if (m)'' ?
3
by: webmasterATflymagnetic.com | last post by:
Folks, I'm struggling to put the question together, but I have this problem. I have written an HTML form that I can use for data entry. This uses PHP to write a SQL UPDATE command that gets...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
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,...
0
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...
0
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...
0
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...

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.