Click here to Skip to main content
15,881,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im trying to call a function from a c++ dll within a class from vb 2008, the library its compiled with .def file to be accessible from vb 2008 and I have been calling other functions from it (the CrearClase and the DestruirClase) without any error messages, the code in c++ is:
void * _stdcall CrearClase();
void _stdcall DestruirClase(void * objptr);
int _stdcall Funcion(void * objptr, const char* Param1, const char* Param2, const char* Param3, __int64 Param4, double* Param5);
<p>
void * _stdcall CrearClase()
    {
        return new Clase;
    }
<p>
void _stdcall DestruirClase(void * objptr)
    {
        Clase *d = (Clase *) objptr;
        if (d)
        delete d;
    }
<p>
int _stdcall Funcion(void * objptr, const char* Param1, const char* Param2, const char* Param3, __int64 Param4, double* Param5)
    {
        Clase *d = (Clase *) objptr;
        if (d)
            return d->FuncionClase(hands, board, dead, numberOfTrials, results);
        else
            return 0;
    }
</p></p></p>

and in the vb end looks like this:

Private Declare Function CrearClase Lib "Library.dll" () As Long
<p>
Private Declare Sub DestruirClase Lib "Library.dll"; (ByVal objptr As Long)
<p>
Private Declare Function Funcion Lib "Library.dll"(ByVal objptr As Long, ByRef Param1 As String, ByRef Param2 As String, ByRef Param3 As String, ByVal Param4 As Int64, ByRef Param5 As Double) As Integer
<p>
Dim buf As Integer
Dim objptr As Long
Dim Param1 As String
Dim Param2 As String
Dim Param3 As String
Dim Param5(0 To 1) As Double 'since it should return two values in here
<p>
Param1 = &quot;something&quot;
Param2= &quot;&quot;
Param3= &quot;&quot;
Param5(0)=0
Param5(1)=0
<p>
objptr = CrearClase()
buf = Funcion(objptr, Param1, Param2, Param3, 100000, Param5(0))
DestruirClase(objptr)
</p></p></p></p></p>

What seems to be the problem, what types of variables should i pass to the function in c++, im getting this error:

"Debug Assertion Failed!

Program: c:\bla\la\microsoft visual studio 9.0\vc\include\vector
Line: 779

Expression: vector subscipt out of range"


Thanks in advance
Posted
Updated 12-Dec-10 2:45am
v4
Comments
Prerak Patel 9-Dec-10 7:57am    
Need more details on error/problem.
Abdul Quader Mamun 12-Dec-10 8:45am    
spelling check.

Honnestly, if you don't explain what the problem is, we won't be able to answer... What is going wrong ? Do you have an error message ? If yes, paste it here with a description on when it occurs.
 
Share this answer
 
Comments
Dalek Dave 9-Dec-10 3:54am    
You tell him Cedric!
Try to put breakpoint in your cpp code at the begining of each function.
Also you may try to use type VARIANT instead of String.
 
Share this answer
 
v2
Ok so i did it, maybe my question really wasnt correctly formulated, anyway tahnks to Kozlov and Cedric, the problem was creating the class and those string variables that are really different between languages, or at least thats how i get rid of it, heres how it all worked out:

C++:
int _stdcall Funcion(LPCSTR Param1, LPCSTR Param2, LPCSTR Param3, double * Param5);
br mode="hold" />int _stdcall Funcion(LPCSTR Param1, LPCSTR Param2, LPCSTR Param3, double * Param5)
	{
	    
		const char * Param11= (const char *) Param1;
		const char * Param21= (const char *) Param2;
		const char * Param31 = (const char *) Param3;
		int Param4 = 1000000;
		Clase Objeto;
		int Test = Objeto.FuncionClase(Param11, Param21, Param31, Param4, Param5);
		return Test; 
				
	}


I decide to leave Param4 as a constant within the wrapper function since it itsn to signifficative.

And in the VB end:

 [System.Runtime.InteropServices.DllImport>("Lib.dll")> _
    Private Shared Function Funcion(ByVal Param1 As String, ByVal Param2 As String, ByVal Param3 As String, ByRef Param5 As Double) As Integer
End Function
        Dim Param1 As String
        Dim Param2 As String
        Dim Param3 As String
        Dim Param5() As Double
        Dim Test as Double
        Param1="Something"
        Param2="Something Else"
        Param3="Something Even Else"
        Redim Param5(n) as Double 'n not a variable
        Param5(0)=0
        Test=Funcion(Param1, Param2, Param3, Param5(0))     


And that was it, again thanks a lot.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900