ATL C++ Wrapper problem

M

Michael Brown

Hey there, it's been a long time since I've done alot in C++ (pre windows
stuff).. so I've got this lib that I've written a wrapper for so I can work
with it in C#.. I created the wrapper as an ATL Project in C++, and
everything is cool... except that when I create more than one instance of
the object in C#, the old references seem to dissappear/are overwritten by
the last created instance of the object.

The ATL Project uses Apartment Threading, so this shouldn't be happening
right? I should be able to create as many instances of my "wrapper", and
inside the wrapper new objects are created.. is ATL to blame here? (did I
explain it right?)


Thanks!

-Mike
 
B

Brian Muth

Michael Brown said:
Hey there, it's been a long time since I've done alot in C++ (pre windows
stuff).. so I've got this lib that I've written a wrapper for so I can
work with it in C#.. I created the wrapper as an ATL Project in C++, and
everything is cool... except that when I create more than one instance of
the object in C#,

the old references seem to dissappear/are overwritten by the last created
instance of the object.

You need to explain this last phrase better. Can you give a concrete
example?
The ATL Project uses Apartment Threading, so this shouldn't be happening
right?

That is correct. Each time you instantiate a new "wrapper" class, you will
get a new ATL COM object.
I should be able to create as many instances of my "wrapper", and inside
the wrapper new objects are created.. is ATL to blame here?

Don't be quite so fast to blame your tools. Almost certainly it is your
understanding or your code that is at fault, not ATL.

Brian
 
M

Michael Brown

(code below is fictional, just explains what I'm encountering)
[C#]
MyObj obj1 = new MyObj("value 1");
MyObj obj2 = new MyObj("value 2");

Debug.WriteLine(obj1.myvalue);
Debug.WriteLine(obj2.myvalue);

// output
value 2
value 2

Basically MyObj is an ATL Project created in C++ (MS VS 2005 Beta 2), which
acts as a wrapper for a C++ Lib file that I'm needing to communicate with in
C#. The ATL wrapper calls a function within the C++ Lib that returns an
object pointer. But when I create a second instance of that wrapper, all the
data from the first instance is also in the second. Any idea what's
happening here?
 
B

Brian Muth

Did you create a singleton ATL COM object using
DECLARE_CLASSFACTORY_SINGLETON?

If not, you will need to show real code. If you'd prefer, package a trimmed
down version as a zip file and email it to me privately at b m u t h 2 0 0 3
at v a n d o t s w i d o t c o m

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top