I created a VB.NET DLL file which I am using in an unmanaged C++ project.
When I try to create an object of the class, I am getting an error:
cannot instantiate abstract class
Why would my class be abstract? How can I modify it so that it won't be abstract?
That's not how it works, you have to write COM code in C++ to use it. Take a good look at the #import directive and the smart pointers it creates.
Related
Here is what I am not sure about:
I have a c++ class that will create and own an obj-C object and very simply, when that C++ class is constructed I call:
this->objCObject = [ThatObject new];
But how do I deal with destroying the ObjC object in ~MyCPPClass() ? Under ARC there is no retain/release.
Thanks
You could disable ARC for the specific file that contains your CPP class. That will give you the freedom to use retain/release in your CPP class.
If you have multiple cases that need the same trick, you could look at the shared_ptr for Objective-C objects
I'm attempting to re-write my C# library, somewhat merged with my C++ implementation, in managed C++.
I have a managed base class that I need to use within unmanaged classes as well.
I have an unmanaged class (enclosed in #pragma unmanaged) that has a function that takes my base class as a parameter. It then adds it to a vector to be referenced later.
For some reason it isn't recognizing gcroot as a keyword. I assume because its in #pragma unmanaged code.
How can I accomplish this?
Thanks,
Alex
You're correct. Anything in #pragma unmanaged has to be "regular" C++ (it can't include C++/clr code). If you want a native class to use gcroot, just don't put the code in #pragma unmanaged (since it does contain managed code).
Why do you need it to be in #pragma unmanaged?
I have a C++ class that I need to create several instances of in a C# application. Apparently this means I'll need to make a C++/CLI wrapper as you can't import C++ classes into C#, but I've never used it before. The C++ class inherits from a base class which just contains several pure virtual functions, and no data. The DLL exports just one function which creates a new instance of the class and returns a pointer to the base class.
What C++/CLI type can be used to call that function and get the pointer to the C++ class, but which can also be imported into C#?
Thanks.
You don't need any special “C++/CLI type”, you should be able to call that function like from normal C++. But if you want to use the C++ class from C#, you really need to write C++/CLI managed wrapper class that you will be able to use from C#.
The managed wrapper class will contain a filed with a pointer to the unmanaged class. It will also contain the same members as the unmanaged class, that forward to their unmanaged equivalents.
For an example of how to do that, see How to: Wrap Native Class for Use by C# on MSDN.
How could I create a method in dll written in c++ which returns a class, and how could I use that dll in java?
If i am export that class the name of that class get changed. Could any body help me? Thanks in advance.
You can't export a C++ class from a DLL and use it from a different compiler. What you can, and should, do is to export a COM object.
You can't. You could use JNI, but that's pretty horrible, and you'll have to write the code to convert from a C++ object to a corresponding Java object.
You could also look at JNA, which tries to make this a little easier.
I'm writing an ATL project and I wonder how should I create classes here.
Right now I have one class created by Add/Class/ATL Simple Object. I want to divide it to smaller classes but method from this classes should use CComPtr and have CComPtr as an argument. I can't create 'simple' c++ class because I don't have CComPtr there.
Should I create ATL classes by ATL Simple Object Wizard and then use interface for this class to call methods. Like here:
CComPtr<ITestAtlClass> tptr;
tptr.CoCreateInstance(CLSID_TestAtlClass);
tptr->test();
And should I add all public methods by Class View/ITestAtlClass/Add/Add Method?
What about constructors? Do I must initialize my class only by properties (and add them by Class View/ITestAtlClass/Add/Add Property)? And pass every com object by IUnknown interface?
Can somebody tell me how it should be done in ATL project. I will use this smaller classes internally (nobody will create this classes outside my DLL) just to make my code more readable.
I don't understand your comment that you can't use CComPtr from a simple C++ class. Can you please clarify?
I see two strategies:
build a clean C++ object model that solves the problem, and then wrap it in a thin facade layer of one or more COM objects
Use ATL classes throughout, and use CComObject<> and derivatives to instantiate and maintain these without the overhead of CoCreateInstance and the limitations of only using public interfaces.
The first one is usually much nicer, but if you're building a data-heavy object model, the second can be a useful technique.
If you have an ATL COM class called CVehicle, that derives from CComObjectRootEx<> and friends, you can instantiate it like so;
CComObject<CVehicle>* vehicle = NULL;
CComObject<CVehicle>::CreateInstance(&vehicle);
vehicle->AddRef();
// To get at any of its interfaces, use:
CComPtr<ICar> car = 0;
vehicle->QueryInterface(&car);
// And to delete object, use:
vehicle->Release();
There's also variations on CComObject<>, e.g. CComObjectStack<> that use different allocation and reference counting strategies.
As you can see, this is pretty messy. If you can explain what you mean by your comment on not being able to use CComPtr, maybe I can expand on that.