Hi,
I am trying to wrap a simple C++ class called math and export it to VB.NET using a DLL made in VC++ 2008. The implementation for the wrapper functions is as follows:
void * _stdcall CreateMath()
{
return new math;
}
void _stdcall DestroyMath( void *Math )
{
math *mathPtr = (math *)Math;
if( mathPtr != NULL )
delete mathPtr;
}
void _stdcall SetValMath( void *Math, int val )
{
math *mathPtr = (math *)Math;
if( mathPtr != NULL )
mathPtr->setVal( val );
}
int _stdcall AddMath( void *Math1, void *Math2 )
{
math *mathPtr1 = (math *)Math1, *mathPtr2 = (math *)Math2;
if( ( mathPtr1 == NULL )
|| ( mathPtr2 == NULL ) )
return 0;
return mathPtr1->add( *mathPtr2 );
}
The wrapper functions are being used in the VB.NET app as follows:
Public Class Form1
Private Declare Function CreateMath Lib "CPPCLASSDLL.dll" () As Long
Private Declare Sub DestroyMath Lib "CPPCLASSDLL.dll" (ByVal Math As Long)
Private Declare Sub SetValMath Lib "CPPCLASSDLL.dll" (ByVal Math As Long, ByVal val As Integer)
Private Declare Function GetValMath Lib "CPPCLASSDLL.dll" (ByVal Math As Long) As Integer
Private Declare Function AddMath Lib "CPPCLASSDLL.dll" (ByVal Math1 As Long, ByVal Math2 As Long) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Math1 As Long = CreateMath()
Dim Math2 As Long = CreateMath()
Dim addition As Integer = 0
SetValMath(Math1, 2)
SetValMath(Math2, 3)
addition = AddMath(Math1, Math2)
DestroyMath(Math1)
DestroyMath(Math2)
TextBox1.Text = CStr(addition)
End Sub
End Class
I apologize for making this post lengthy but I believe it will be necessary to see all the code to understand the problem fully. I have tried using the C++ DLL and the wrapper functions in a VC++ application and it has worked without a problem. When I use the same DLL in VB.NET, the value shown in TextBox1 is normally an 8 digit number starting with 6 (ex: 60261136) instead of the correct value of 5. Maybe the wrapper function is returning the address of a variable somehow.
I don't believe that I am screwing up in data type conversion because the size of the "int" data type in VC++ on my computer is 4 bytes and the VB.NET data type "Integer" is a 32-bit signed integer. As for the "Long" data type - I was instructed by a tutorial to use it to represent C++ void pointers in VB.NET. Can anybody shed some light on this problem? Any help will be greatly appreciated!
Regards,
Adeel.