473,387 Members | 1,779 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,387 software developers and data experts.

baffled by exception

Hello.
Does anybody know if there is a difference between the way C# calls a COM
object, and the way C++ calls a COM object? And is there anyway to make the
latter emulate the former?

I have a COM object which seemingly can't be instantiated using C++, but can
using C# and VB, and it's driving me insane - I can't work out what's wrong
with it.
It just seems to throw an exception, citing 'User Breakpoint' as the cause
of the exception when any C++ code tried to instantiate it.
If I click 'continue' to the exception when it's in debug mode, it rethrows
it as:
System.Runtime.InteropServices.COMException (0xC0000096): Exception from
HRESULT: 0xC0000096.

But it still throws up the message box when the try..catch is round it!
This COM object, I fully own and the dongle to it is plugged in. Like I say,
I can instantiate this COM object in other languages and I can instantiate
other COM objects using these methods in C++.

Is it possible they could have not wanted you to use it from C++, and can
tell?
Any ideas?
Cheers!
Nov 16 '05 #1
20 2078
Bonj,

This seems highly unlikely to me, as the mechanism in .NET ultimately
uses the same mechanism that C++ uses. Can you show how you are doing it in
..NET and how you are doing it in C++? Perhaps you are not setting up the
environment or the apartment correctly, and this could be the source of your
problems.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bonj" <Bo**@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com...
Hello.
Does anybody know if there is a difference between the way C# calls a COM
object, and the way C++ calls a COM object? And is there anyway to make
the
latter emulate the former?

I have a COM object which seemingly can't be instantiated using C++, but
can
using C# and VB, and it's driving me insane - I can't work out what's
wrong
with it.
It just seems to throw an exception, citing 'User Breakpoint' as the cause
of the exception when any C++ code tried to instantiate it.
If I click 'continue' to the exception when it's in debug mode, it
rethrows
it as:
System.Runtime.InteropServices.COMException (0xC0000096): Exception from
HRESULT: 0xC0000096.

But it still throws up the message box when the try..catch is round it!
This COM object, I fully own and the dongle to it is plugged in. Like I
say,
I can instantiate this COM object in other languages and I can instantiate
other COM objects using these methods in C++.

Is it possible they could have not wanted you to use it from C++, and can
tell?
Any ideas?
Cheers!

Nov 16 '05 #2
Bonj,

This seems highly unlikely to me, as the mechanism in .NET ultimately
uses the same mechanism that C++ uses. Can you show how you are doing it in
..NET and how you are doing it in C++? Perhaps you are not setting up the
environment or the apartment correctly, and this could be the source of your
problems.

Hope this helps.
"Bonj" <Bo**@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com...
Hello.
Does anybody know if there is a difference between the way C# calls a COM
object, and the way C++ calls a COM object? And is there anyway to make
the
latter emulate the former?

I have a COM object which seemingly can't be instantiated using C++, but
can
using C# and VB, and it's driving me insane - I can't work out what's
wrong
with it.
It just seems to throw an exception, citing 'User Breakpoint' as the cause
of the exception when any C++ code tried to instantiate it.
If I click 'continue' to the exception when it's in debug mode, it
rethrows
it as:
System.Runtime.InteropServices.COMException (0xC0000096): Exception from
HRESULT: 0xC0000096.

But it still throws up the message box when the try..catch is round it!
This COM object, I fully own and the dongle to it is plugged in. Like I
say,
I can instantiate this COM object in other languages and I can instantiate
other COM objects using these methods in C++.

Is it possible they could have not wanted you to use it from C++, and can
tell?
Any ideas?
Cheers!


Nov 16 '05 #3
An apartment type mismatch is only going to be a problem if the COM marshalling code (proxy/stub) is not installed properly. This error is a COM error relating to a privileged instruction attempting to be used. It is often in relation to the proxy/stub dll (that has the interface marshalling code in it) being out of step with the definition of the interface the COM object has implemented.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<Og**************@TK2MSFTNGP11.phx.gbl>

Bonj,

This seems highly unlikely to me, as the mechanism in .NET ultimately
uses the same mechanism that C++ uses. Can you show how you are doing it in
.NET and how you are doing it in C++? Perhaps you are not setting up the
environment or the apartment correctly, and this could be the source of your
problems.

Hope this helps.
Nov 16 '05 #4
> Can you show how you are doing it in
..NET and how you are doing it in C++? (The library is called MyObjX and the object is called MyObj):

in C++:

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;
using namespace Interop::MyObjX;
using namespace System::Runtime::InteropServices;

[STAThread]
int _tmain()
{
MyObjClass* emb = __gc new MyObjClass();
return 0;
}
//in unmanaged c++ (I first added the myobj.IDL file with the contents of
its type library in COM / object viewer, and then compiled it to produce
myobj_i.c and myobj_h.h

#include <tchar.h>
#include <objbase.h>
#include <comdef.h>
#include "myobj_h.h"

void GetPrediction()
{
HRESULT hr = CoInitialize(NULL);
_MyObj* ip;
try
{
hr = CoCreateInstance(CLSID_MyObj, NULL, CLSCTX_INPROC_SERVER, IID__MyObj,
(void**)&ip);
}
catch(_com_error e)
{
const _TCHAR* s = e.ErrorMessage();
}
CoUninitialize();
}
// in C# (which works):

using System;
using MyObjX;

namespace UseEmbCsharp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static int Main(string[] args)
{
MyObjX.MyObjClass p = new MyObjClass();
return 0;
}
}
}
This seems highly unlikely to me, as the mechanism in .NET ultimately
uses the same mechanism that C++ uses.
That's what I thought. This is why it baffles me.
Perhaps you are not setting up the
environment or the apartment correctly, and this could be the source of your
problems.


I can't see what differences there could be - if you can think of any,
please let me know! I've added the [STAThread] attribute to the main of the
MC++ project just like the C# one, and it still doesn't work. I don't know
how to set this for UMC++ - but I think it's more likely to be a security
thing than a threading thing - as the exception that is thrown completely
ignores try..catch handlers, and the COM object is protected with a dongle.
But it is installed completely correctly and legally, and the dongle is
plugged in, and as I say, it works from all languages but ANY breed of
C++.... it's completely weird......
Nov 16 '05 #5
Bonj wrote:
//in unmanaged c++ (I first added the myobj.IDL file with the
contents of its type library in COM / object viewer, and then
compiled it to produce myobj_i.c and myobj_h.h


Try using #import and smart pointers instead.
--
Sigurd
http://utvikling.com
Nov 16 '05 #6
This sounds more like it...

Are you able to provide any more information on how this problem might have
manifested itself in this scenario, and how I might go about curing it?

"Richard Blewett [DevelopMentor]" wrote:
An apartment type mismatch is only going to be a problem if the COM marshalling code (proxy/stub) is not installed properly. This error is a COM error relating to a privileged instruction attempting to be used. It is often in relation to the proxy/stub dll (that has the interface marshalling code in it) being out of step with the definition of the interface the COM object has implemented.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<Og**************@TK2MSFTNGP11.phx.gbl>

Bonj,

This seems highly unlikely to me, as the mechanism in .NET ultimately
uses the same mechanism that C++ uses. Can you show how you are doing it in
.NET and how you are doing it in C++? Perhaps you are not setting up the
environment or the apartment correctly, and this could be the source of your
problems.

Hope this helps.

Nov 16 '05 #7
If you reversed engineered the typelib, you may have lost some of the marshalling information. Some IDL constructs don't make it into the typelib ([size_is] for example).You may be working from a different set of marshalling instructions than the COM object.

As Sigurd suggested, if they have't provided you with a header file use #import to import the typelib.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<4A**********************************@microsoft.co m>

This sounds more like it...

Are you able to provide any more information on how this problem might have
manifested itself in this scenario, and how I might go about curing it?
Nov 16 '05 #8
I've done that with still the same exception
"Sigurd Stenersen" wrote:
Bonj wrote:
//in unmanaged c++ (I first added the myobj.IDL file with the
contents of its type library in COM / object viewer, and then
compiled it to produce myobj_i.c and myobj_h.h


Try using #import and smart pointers instead.
--
Sigurd
http://utvikling.com

Nov 16 '05 #9
mmmm. I might try that again, but I definitely did that and it didn't work.

But you think that by getting the MIDL out of 'OLE/COM object viewer' from
the 'Type libraries' section and copying and pasting it into an .idl file and
then compiling that, I might have lost some information, that I possibly
wouldn't have lost by using smart pointers?

Is copying and pasting from OLE/COM object viewer's Type Libraries section
the best way to do it if I am going to go down the CoCreateInstance path?
Anyhow thanks for your help on this

"Richard Blewett [DevelopMentor]" wrote:
If you reversed engineered the typelib, you may have lost some of the marshalling information. Some IDL constructs don't make it into the typelib ([size_is] for example).You may be working from a different set of marshalling instructions than the COM object.

As Sigurd suggested, if they have't provided you with a header file use #import to import the typelib.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<4A**********************************@microsoft.co m>

This sounds more like it...

Are you able to provide any more information on how this problem might have
manifested itself in this scenario, and how I might go about curing it?

Nov 16 '05 #10
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<4A**********************************@microsoft.co m>

Is the above a link to information you think I might benefit from looking at?
If so, could you possibly copy and paste the pertinent bits into a message
and post back, because our firewall doesn't allow downloading over port 119
(nntp), we're only allowed to look at http.

Cheers

Nov 16 '05 #11
And both C++ and MC++ exceptions thrown are the same?
Note that 0XC0000096 isn't a COM exception but a Win32 error code thrown as
a result of a CPU interupt - while trying to execute a privileged
instruction in user mode.
This is weird, and point's to the COM server code itself.

Willy.
"Bonj" <Bo**@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...
I've done that with still the same exception
"Sigurd Stenersen" wrote:
Bonj wrote:
> //in unmanaged c++ (I first added the myobj.IDL file with the
> contents of its type library in COM / object viewer, and then
> compiled it to produce myobj_i.c and myobj_h.h


Try using #import and smart pointers instead.
--
Sigurd
http://utvikling.com

Nov 16 '05 #12
That doesn't surprise me... but how would it be possible to write a COM
component that works successfully from C# and VB, but doesn't work from C++ ??

"Willy Denoyette [MVP]" wrote:
And both C++ and MC++ exceptions thrown are the same?
Note that 0XC0000096 isn't a COM exception but a Win32 error code thrown as
a result of a CPU interupt - while trying to execute a privileged
instruction in user mode.
This is weird, and point's to the COM server code itself.

Willy.
"Bonj" <Bo**@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...
I've done that with still the same exception
"Sigurd Stenersen" wrote:
Bonj wrote:
> //in unmanaged c++ (I first added the myobj.IDL file with the
> contents of its type library in COM / object viewer, and then
> compiled it to produce myobj_i.c and myobj_h.h

Try using #import and smart pointers instead.
--
Sigurd
http://utvikling.com


Nov 16 '05 #13

Is it possible to reproduce the problem in a simplest possible application
that only instantiates and uses this COM object and does nothing more?
(Preferrably in unmanaged C++)

Regards,
Oleg


Nov 16 '05 #14
Not really, because it isn't a standard one - it's a third party custom
software, and is protected with a dongle. I have emailed the company that
produces it to ask them but whether they'll know or be able to tell me I
don't know. However I have got it fully legally installed and the dongle is
installed on my pc. The strangest thing is it works from VB.NET, VB6 and C#,
just not from any brand of C++.

The code I have been using is this:

#import "c:\program files\emblem modeller 3\MyObjx.dll"
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
MyObjX::_MyObjPtr model;
HRESULT hr = model.CreateInstance(__uuidof(MyObjX::MyObj));
if(hr == S_OK) MessageBox(NULL, _T("EMB Model successfully created"),
_T("COM test"), MB_OK); //it never is.
CoUninitialize();
}

"Oleg Starodumov" wrote:

Is it possible to reproduce the problem in a simplest possible application
that only instantiates and uses this COM object and does nothing more?
(Preferrably in unmanaged C++)

Regards,
Oleg


Nov 16 '05 #15
Bonj wrote:
#import "c:\program files\emblem modeller 3\MyObjx.dll"
MyObjX::_MyObjPtr model;
HRESULT hr = model.CreateInstance(__uuidof(MyObjX::MyObj));


#import "c:\program files\emblem modeller 3\MyObjx.dll" no_namespace
_MyObjPtr model(__uuidof(MyObj));

If this doesn't work, are you sure you're using 1) the correct smart pointer
type, and 2) the correct source for uuid ?

I'd say that given the uuid source is MyObj, it would be more logical if the
smart pointer was of type IMyObjPtr. But that is, of course, how it would
look if *I* implemented a server and used it.
--
Sigurd
http://utvikling.com
Nov 16 '05 #16
but thanks for your efforts anyway, and I like the no_namespace thing.
"Sigurd Stenersen" wrote:
Bonj wrote:
#import "c:\program files\emblem modeller 3\MyObjx.dll"
MyObjX::_MyObjPtr model;
HRESULT hr = model.CreateInstance(__uuidof(MyObjX::MyObj));


#import "c:\program files\emblem modeller 3\MyObjx.dll" no_namespace
_MyObjPtr model(__uuidof(MyObj));

If this doesn't work, are you sure you're using 1) the correct smart pointer
type, and 2) the correct source for uuid ?

I'd say that given the uuid source is MyObj, it would be more logical if the
smart pointer was of type IMyObjPtr. But that is, of course, how it would
look if *I* implemented a server and used it.
--
Sigurd
http://utvikling.com

Nov 16 '05 #17
IMyObjPtr isn't defined. _MyObjPtr is the only thing that is.
I've given up, to be honest, I've resigned myself that it's something
non-standard about the COM object. I've emailed the company to ask them and
if they don't email me back then I'm giving up.

"Sigurd Stenersen" wrote:
Bonj wrote:
#import "c:\program files\emblem modeller 3\MyObjx.dll"
MyObjX::_MyObjPtr model;
HRESULT hr = model.CreateInstance(__uuidof(MyObjX::MyObj));


#import "c:\program files\emblem modeller 3\MyObjx.dll" no_namespace
_MyObjPtr model(__uuidof(MyObj));

If this doesn't work, are you sure you're using 1) the correct smart pointer
type, and 2) the correct source for uuid ?

I'd say that given the uuid source is MyObj, it would be more logical if the
smart pointer was of type IMyObjPtr. But that is, of course, how it would
look if *I* implemented a server and used it.
--
Sigurd
http://utvikling.com

Nov 16 '05 #18

In addition to Sigurd's message...
#import "c:\program files\emblem modeller 3\MyObjx.dll"
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
MyObjX::_MyObjPtr model;
HRESULT hr = model.CreateInstance(__uuidof(MyObjX::MyObj));
if(hr == S_OK) MessageBox(NULL, _T("EMB Model successfully created"),
_T("COM test"), MB_OK); //it never is.
CoUninitialize();
}


So you run this application and it still throws an exception
inside CreateInstance call and does not reach the next line?

Can you try with the raw CoCreateInstance, requesting only IUnknown,
so that other interfaces are not yet involved?

What threading model is used by the COM object?

Regards,
Oleg

P.S. It is better to have the smart pointer in its own scope,
otherwise (in case of successful creation of the object instance)
it will be released after CoUninitialize call, thus causing problems.

Something like this is needed:

CoInitialize()
{
MyObjX::_MyObjectPtr model;
...
}
CoUninitialize()


Nov 16 '05 #19
as in this..?

#import "c:\program files\emblem modeller 3\embpredictx.dll" no_namespace
#include <tchar.h>
#include <objbase.h>

int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
IUnknown* iu;
GUID IID_EMBPredictX = {0x601EA0CD, 0xBB9B, 0x11D1,
{0x81, 0x58, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}};

GUID CLSID_EMBPredictX = {0x601EA0D0, 0xBB9B, 0x11D1,
{0x81, 0x58, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}};

HRESULT hr = CoCreateInstance(IID_EMBPredictX, NULL, CLSCTX_INPROC_SERVER,
IID_IUnknown,
(void**)&iu);

if(hr == S_OK) MessageBox(NULL, _T("EMB Model successfully created"),
_T("COM test"), MB_OK);
CoUninitialize();
}

it does the same thing (priv. instr.), when passing the IID_EMBPredictX, and
'class not registered' when with any other combination of swapping round the
CLSID_ and the IID_ and the IID_IUnknown.

"Oleg Starodumov" wrote:

In addition to Sigurd's message...
#import "c:\program files\emblem modeller 3\MyObjx.dll"
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
MyObjX::_MyObjPtr model;
HRESULT hr = model.CreateInstance(__uuidof(MyObjX::MyObj));
if(hr == S_OK) MessageBox(NULL, _T("EMB Model successfully created"),
_T("COM test"), MB_OK); //it never is.
CoUninitialize();
}

So you run this application and it still throws an exception
inside CreateInstance call and does not reach the next line?

Can you try with the raw CoCreateInstance, requesting only IUnknown,
so that other interfaces are not yet involved?

What threading model is used by the COM object?

Regards,
Oleg

P.S. It is better to have the smart pointer in its own scope,
otherwise (in case of successful creation of the object instance)
it will be released after CoUninitialize call, thus causing problems.

Something like this is needed:

CoInitialize()
{
MyObjX::_MyObjectPtr model;
...
}
CoUninitialize()



"Oleg Starodumov" wrote:

In addition to Sigurd's message...
#import "c:\program files\emblem modeller 3\MyObjx.dll"
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
MyObjX::_MyObjPtr model;
HRESULT hr = model.CreateInstance(__uuidof(MyObjX::MyObj));
if(hr == S_OK) MessageBox(NULL, _T("EMB Model successfully created"),
_T("COM test"), MB_OK); //it never is.
CoUninitialize();
}


So you run this application and it still throws an exception
inside CreateInstance call and does not reach the next line?

Can you try with the raw CoCreateInstance, requesting only IUnknown,
so that other interfaces are not yet involved?

What threading model is used by the COM object?

Regards,
Oleg

P.S. It is better to have the smart pointer in its own scope,
otherwise (in case of successful creation of the object instance)
it will be released after CoUninitialize call, thus causing problems.

Something like this is needed:

CoInitialize()
{
MyObjX::_MyObjectPtr model;
...
}
CoUninitialize()


Nov 16 '05 #20
Well, if the problem was only with C# and MC++, I would think about COM
interop in the CLR. But you also have the same problem using native C++, so
no interop involved :-)

Willy.

"Bonj" <Bo**@discussions.microsoft.com> wrote in message
news:58**********************************@microsof t.com...
That doesn't surprise me... but how would it be possible to write a COM
component that works successfully from C# and VB, but doesn't work from
C++ ??

"Willy Denoyette [MVP]" wrote:
And both C++ and MC++ exceptions thrown are the same?
Note that 0XC0000096 isn't a COM exception but a Win32 error code thrown
as
a result of a CPU interupt - while trying to execute a privileged
instruction in user mode.
This is weird, and point's to the COM server code itself.

Willy.
"Bonj" <Bo**@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...
> I've done that with still the same exception
>
>
> "Sigurd Stenersen" wrote:
>
>> Bonj wrote:
>> > //in unmanaged c++ (I first added the myobj.IDL file with the
>> > contents of its type library in COM / object viewer, and then
>> > compiled it to produce myobj_i.c and myobj_h.h
>>
>> Try using #import and smart pointers instead.
>>
>>
>> --
>>
>>
>> Sigurd
>> http://utvikling.com
>>
>>
>>


Nov 16 '05 #21

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

Similar topics

1
by: Tolga Erdogus | last post by:
Hi, I have the following code: for (int i=0;i<childOptimizations.Length;i++) //for each partition { row = childOptimizations;
0
by: Steve Barker | last post by:
Hi guys, I've written an application in C# that connects to a Paradox table via ODBC. My code has always worked fine, but now I've moved my application to another machine, it's stopped working!...
8
by: Ray | last post by:
Hello guys, OK, I've been reading some more about Python. There are some things about Python exception that I haven't been able to grasp: 1. This is a small thing, but why is object spelled...
1
by: Robert A Riedel | last post by:
I am completely baffled when the following managed exception is thrown: "Object reference not set to an instance of an object" from a nested subroutine when referencing a variable allocated on the...
20
by: Bonj | last post by:
Hello. Does anybody know if there is a difference between the way C# calls a COM object, and the way C++ calls a COM object? And is there anyway to make the latter emulate the former? I have a...
5
by: Alan Silver | last post by:
Hello, I have a page that is supposed to do some checking, and if OK, set a session variable before redirecting to another page. The following code is a simplified version, I have hard-coded the...
11
by: Russ P. | last post by:
I am baffled about why my exception messages are not displaying properly. I have a class that represents physical scalars with units. If I type I should get something like this: ...
2
by: =?Utf-8?B?Qw==?= | last post by:
In my development environment when an error is raised in my Buiness Layer I rethrow the exception and the message of he Exception is displayed. For example : Cannot Update where Record Status is...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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...

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.