Programming, website development forum Get latest updates by RSS Follow TechnicalTalk on Twitter Follow TechnicalTalk on Facebook 
HomeSearchRecent PostsLoginRegisterContact Us

Username  
Password    
  Forgot your password?  

Pages: [1]   Go Down
 
  Email this topic  |  Print
0 Members and 1 Guest are viewing this topic.

Using wrapper functions for a C++ class (exported in a DLL) in a VB.NET app

 
webmaster forum
Adeel89  Offline
Activity
0%
 
New Poster
Posts: 1
Topics: 1
August 12, 2009, 01:58:32 PM

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:


Code:
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:

Code:
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)
        addition = GetValMath(Math1)
        SetValMath(Math2, 3)
        addition = GetValMath(Math2)
        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 an 8 digit number starting from 6 (ex:60261136) instead of the correct value of 5. The number is always different so maybe the wrapper function is returning the address of a variable. 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.
« Last Edit: July 09, 2011, 07:49:32 PM by Admin »
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.15 | SMF © 2011, Simple Machines


Google visited last this page January 25, 2012, 08:49:41 PM