[Ayuda] C++ llamando Dll

Iniciado por Miseryk, 12 Octubre 2010, 22:28 PM

0 Miembros y 1 Visitante están viendo este tema.

Miseryk

Hola a todos, tengo una duda.

Tengo una Dll hecha en VB6,

Código (vb) [Seleccionar]

Public Sub Mensaje()
Msgbox "Mensaje!"
End Sub


Como llamo a esa funcion desde C++?, ya probé con LoadLibrary y me tira error,

Código (cpp) [Seleccionar]

// DLL function signature
typedef double (*importFunction)(void);

importFunction MyFunction;
// Load DLL file
HINSTANCE hinstLib = LoadLibrary("Project1.dll");
if (hinstLib == NULL)
{
MessageBox(0,"ERROR: unable to load DLL","Error",0);
}
else
{
// Get function pointer
MyFunction = (importFunction)GetProcAddress(hinstLib, "Mensaje");
if (MyFunction == NULL)
{
MessageBox(0,"ERROR: unable to find DLL function","Error",0);
FreeLibrary(hinstLib);
}
else
{

MyFunction();

// Unload DLL file
FreeLibrary(hinstLib);
}
}


alguna idea? Desde ya muchas gracias, si quieren subo los archivos.

Edit:
Es verdad  :laugh:, lo que pasa es que estaba muy desesperado xDDDDD, ahi lo etiqueté  ;-)
Can you see it?
The worst is over
The monsters in my head are scared of love
Fallen people listen up! It's never too late to change our luck
So, don't let them steal your light
Don't let them break your stride
There is light on the other side
And you'll see all the raindrops falling behind
Make it out tonight
it's a revolution

CL!!!

Oblivi0n

Yo tengo hechas en C y cargadas en programas compilados en C.
lo primero, en la dll tenia que poner __declspec(dllexport)
para que me deje usar las funciones, y luego hacer alguna que otra cosa mas(en el msdn tienes toda la informacion.

A ver si esto te ayuda
http://msdn.microsoft.com/es-es/library/ms235636.aspx

http://www.lawebdelprogramador.com/news/mostrar_new.php?id=13&texto=C/Visual+C&n1=456604&n2=1&n3=0&n4=0&n5=0&n6=0&n7=0&n8=0&n9=0&n0=0


Miseryk

#2
Gracias, pero eso utiliza referencias, osea reutiliza el codigo de la dll, aca tengo la dll en la carpeta con esa función sola, para testear el programa y un proyecto en VC++ el cuál estoy tratando de llamar a esa funcion.

Lh: No hagas doble post, utiliza el botón modificar.

Pude lograr que no tire error, pero no me muestra el MsgBox "Mensaje!"

Código (cpp) [Seleccionar]

#include <iostream>
#include <windows.h>

using namespace std;

typedef void (*MsgFunction) (void);

HINSTANCE hinstDLL;

int main()
{
MsgFunction MsgBox;
hinstDLL = LoadLibrary("Project1.dll");
if(hinstDLL != 0)
{
printf("DLL LOADED.\n");
MsgBox = (MsgFunction)GetProcAddress(hinstDLL,"Mensaje");

if(MsgBox != 0)
{
printf("FUNCTION FOUND.\n");
//Call Function
MsgBox;
}
else
{
printf("FUNCTION NOT FOUND.\n");
}

// Unload DLL file
FreeLibrary(hinstDLL);
}
else
{
printf("DLL NOT LOADED.\n");
}
system("PAUSE");
return 0;
}



Lh: Uniendo mensajes nuevamente, la próxima borro sin aviso. Utiliza el botón modificar.


Pude arreglar el problema, uno era en typedef, que le tenía que agregar WINAPI y otro en llamar a la función, para eso hice 2 funciones, una Mensaje y otra Suma, la Suma lo hace bien sin errores, pero a la hora de llamar a Mensaje, me muestra el mensaje ("Test") y luego crashea el programa tirando error ModName: msvbm60.dll. Alguna idea?

Acá les dejo todo el código:

Código (vb) [Seleccionar]
Option Explicit

Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long

Public Sub Mensaje()
MessageBox 0, "Test", "Caption", 0
End Sub

Public Function Suma(ByVal n1 As Long, ByVal N2 As Long) As Long
Suma = n1 + N2
End Function


Código (cpp) [Seleccionar]
#include <iostream>
#include <windows.h>

using namespace std;

typedef void (WINAPI*MsgFunction) ();
typedef long (WINAPI*SumaFunction) (long,long);

int main()
{
MsgFunction MsgBox;
SumaFunction Suma;

HINSTANCE hinstDLL = LoadLibrary("Project1.dll");

if(hinstDLL != 0)
{
printf("DLL LOADED.\n");
MsgBox = (MsgFunction)GetProcAddress(hinstDLL,"Mensaje");

MsgBox();



Suma = (SumaFunction)GetProcAddress(hinstDLL,"Suma");

long x = Suma(6,6);
if(x == 12)
{
cout << "Message Displayed!\n";
}

// Unload DLL file
FreeLibrary(hinstDLL);
}
else
{
printf("DLL NOT LOADED.\n");
}

system("PAUSE");
return 0;
}

Can you see it?
The worst is over
The monsters in my head are scared of love
Fallen people listen up! It's never too late to change our luck
So, don't let them steal your light
Don't let them break your stride
There is light on the other side
And you'll see all the raindrops falling behind
Make it out tonight
it's a revolution

CL!!!

Oblivi0n

MessageBox 0, "Test", "Caption", 0

El ultimo 0, no deberia de ser el estilo de botones? prueba a poner "MB_OK"

MessageBox 0, "Test", "Caption", MB_OK

Al menos en C se llaman asi:
MessageBox(hWnd, "Texto de mensaje", "Texto de titulo", MB_OK);

(bueno, mb_ok es un estilo de boton, puedes poner otros...)
Un saludo!.

Miseryk

Lo que pasa es que ese 0 equivale a ésto. Private Const MB_OK = &H0&

El problema es nativo del VB creo, hay que transformarlo para que en otro lenguaje lo pueda tomar, pero no sé como.
Can you see it?
The worst is over
The monsters in my head are scared of love
Fallen people listen up! It's never too late to change our luck
So, don't let them steal your light
Don't let them break your stride
There is light on the other side
And you'll see all the raindrops falling behind
Make it out tonight
it's a revolution

CL!!!