473,890 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 interoperabilit y 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\\M icrosoft.NET\\F ramework\\v1.0. 3705\\mscorlib. tlb" no_namespace named_guids raw_interfaces_ onl

int main(int argc, char* argv[]

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

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

printf("ERROR: Cannot initialize COM: 0x%x\n", hresult)
return -1
// Instantiate the System.Collecti ons.ArrayList clas
hresult = CoCreateInstanc e(CLSID_ArrayLi st, NULL, CLSCTX_INPROC_S ERVER,
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_ISupportEr rorInfo
(void**)&pSuppE rrInfo)
if (SUCCEEDED(hres ult)

hresult = pSuppErrInfo->InterfaceSuppo rtsErrorInfo(II D_IList)
if (hresult == S_OK)

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

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

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

hresult = pErrInfo->GetDescription (&description )
if (SUCCEEDED(hres ult)) printf("Descrip tion: %S\n\n"
description)

hresult = pErrInfo->GetHelpFile(&h elpFile)
if (SUCCEEDED(hres ult)) printf("HelpFil e: %S\n\n",
helpFile)

hresult = pErrInfo->GetHelpContext (&helpContext )
if (SUCCEEDED(hres ult)) printf("HelpCon text: %d\n\n"
helpContext)

if (source != NULL) SysFreeString(s ource)
if (description != NULL) SysFreeString(d escription)
if (helpFile != NULL) SysFreeString(h elpFile)

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

hresult = pErrInfo->QueryInterface (IID__Exception
(void**)&pExcep tion)
if (SUCCEEDED(hres ult)

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

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

hresult = pException->GetType(&pType );
if (SUCCEEDED(hres ult))
{
hresult = pType->get_ToString(& exceptionTypeNa me);
if (SUCCEEDED(hres ult)) printf("Excepti on Type: %S\n\n",
exceptionTypeNa me);
}

if (toString != NULL) SysFreeString(t oString);
if (stackTrace != NULL) SysFreeString(s tackTrace);
if (exceptionTypeN ame != NULL)
SysFreeString(e xceptionTypeNam e);
}
}
}
}
}

// 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 3906
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 interoperabilit y 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
2384
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
5196
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
1978
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 fairly complicated XML file that is full of technical data(most scalar values). Then I need to store all the info in a C structure. Could some people give me their opinion. Which is better suited for this, Expat or LT xml?
4
9662
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 href="#"&gt;text&lt;/a&gt;
4
62128
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 supposed to write this function? String.replace(/</g,'&lt;');
5
3550
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 it. The problem is that i recive this from external source and cant
10
42974
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, the XML specification seems a little ambiguous on this, so I defer to the XML authorities. Refer to sections 2.4 and 2.7 (it all hinges on if CDATA attribute values are part of markup or not.) Thanks.
4
1924
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: Well-formedness constraint: No < in Attribute Values The replacement text of any entity referred to directly or indirectly in an attribute value MUST NOT contain a <.
4
1820
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
4034
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 written to my MySQL database. I can later view this data back in the form. One thing I've noticed happening is if I enter code such as &lt; it gets rewritten as < (ie the less-than sign). Now I don't want this to happen, but something somewhere is...
0
9812
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10799
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
10446
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
9614
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
7154
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
5832
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
4655
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
4255
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3263
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.