Trouble using .NET dll in Borland C++ Builder 4 
April 15th, 2009, 04:34 PM
| | Newbie | | Join Date: Feb 2009
Posts: 5
| | |
I have created a COM callable DLL in C# .NET 2.0 and created a TLB
from the assembly using the .NET regasm tool.
In Borland C++ Builder 4.0 I go to Project->Import Type Library-> and
find my DLL's type library there and click "Ok" to import it.
BCB creates an HardwareCheck_TLB.cpp & HardwareCheck_TLB.h file.
In a cpp file of the project I want to use the DLL I put:
#include "HardwareCheck_TLB.h"
at the top.
Then in code if I try to declare an object of that type in code:
IHardwareCheck hc = new IHardwareCheck();
I get the following compiler error:
[C++ Error] Unit1.cpp(22): E2352 Cannot create instance of abstract
class 'IHardwareCheck'.
[C++ Error] Unit1.cpp(22): E2353 Class 'IHardwareCheck' is abstract
because of '__stdcall IHardwareCheck::IsExpress(wchar_t * *,TOLEBOOL
*) = 0'.
Anybody have any ideas how to get rid of this error?
Thanks!
| 
April 19th, 2009, 02:34 AM
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,338
| | | re: Trouble using .NET dll in Borland C++ Builder 4
You cannot create objects of classes that have pure virtual functions.
You are supposed to create a derived object (which has no pure vrtual functions) and assign the address of that object to a base class pointer (like I HardwareCheck). Then you use IHardwareCheck->etc... to call member functions on that derived object.
| 
April 19th, 2009, 05:20 AM
| | Newbie | | Join Date: Feb 2009
Posts: 5
| | | re: Trouble using .NET dll in Borland C++ Builder 4
Can you please provide an example of the syntax of creating such a derived class or at least a link which illustrates this?
Thanks!
| 
April 19th, 2009, 05:09 PM
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,338
| | | re: Trouble using .NET dll in Borland C++ Builder 4
Here you go: - class Shape
-
{
-
public:
-
virtual void draw() = 0;
-
};
-
class Circle : public Shape
-
{
-
public:
-
virtual void draw();
-
};
-
void Circle::draw()
-
{
-
cout << I am a Circle" << endl;
-
}
-
-
int main()
-
{
-
//Shape* s = new Shape; //ERROR. Cannot create Shape objects
-
Shape* s = new Circle;
-
s->draw(); //calls Circle::draw()
-
}
This is fundamental C++ polymorphism. Any C++ textbook will discuss this.
| 
April 19th, 2009, 05:25 PM
| | Newbie | | Join Date: Feb 2009
Posts: 5
| | | re: Trouble using .NET dll in Borland C++ Builder 4
thank you I understand from back in college...
what I don't understand is which side do I do this on? Do I do it on the C# .NET side where my dll is written, or do I do it on the Borland C++ Builder side where I actually will be using the dll.
Have you ever actually done this before (i.e. used a C# dll with Borland C++ Builder)??
Thanks for all inputs so far.
| 
April 19th, 2009, 05:45 PM
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,338
| | | re: Trouble using .NET dll in Borland C++ Builder 4
It looks like IHardwareCheck is an interface of a C++ COM class.
I assume you know how to use COM. If not, you have a big learning curve here. Especially since this uses non-virtual multiple inheritance
You create your COM object by calling CoCreateInstance with the GUID for your COM class and then use a call to QueryInterface using the GUID for IHardwareCheck. That should return a pointer to the IHardwareCheck interface. Now you can use IHardwareCheck methods.
This is all done on the C++ side.
| 
April 20th, 2009, 11:59 AM
| | Newbie | | Join Date: Apr 2009
Posts: 1
| | | re: Trouble using .NET dll in Borland C++ Builder 4
The problem is in using the tlb created. Try using . It works with me.
//------------------Implementation Method1----------------------------
IHardwareCheckPtr pIDotnetObj;
pIDotnetObj = CreateComObject(CLSID_HardwareCheck);
pIDotnetObj->ShowDialog();
//--------------------Implementation Method2---------------------------
CoInitialize(NULL);
IHardwareCheckPtr pDotNetCOMPtr;
HRESULT hRes = pDotNetCOMPtr.CreateInstance(CLSID_HardwareCheck );
if (hRes == S_OK)
{
pDotNetCOMPtr->ShowDialog2();
}
CoUninitialize ();
| 
April 20th, 2009, 02:15 PM
| | Newbie | | Join Date: Feb 2009
Posts: 5
| | | re: Trouble using .NET dll in Borland C++ Builder 4
Great thanks a ton for all the help given here.
I really appreciate it.
I was told before that you couldn't use a .NET DLL in Borland C++ Builder 4, but now this proves that person was wrong.
I bet the same could be done in Delphi 3 using the same .NET dll.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|