elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Introducción a la Factorización De Semiprimos (RSA)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  [Ayuda] C++ llamando Dll
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [Ayuda] C++ llamando Dll  (Leído 2,655 veces)
Miseryk

Desconectado Desconectado

Mensajes: 225


SI.NU.SA U.GU.DE (2NE1 - D-Unit)


Ver Perfil
[Ayuda] C++ llamando Dll
« en: 12 Octubre 2010, 22:28 pm »

Hola a todos, tengo una duda.

Tengo una Dll hecha en VB6,

Código
  1. Public Sub Mensaje()
  2. Msgbox "Mensaje!"
  3. End Sub
  4.  

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

Código
  1. // DLL function signature
  2. typedef double (*importFunction)(void);
  3.  
  4. importFunction MyFunction;
  5. // Load DLL file
  6. HINSTANCE hinstLib = LoadLibrary("Project1.dll");
  7. if (hinstLib == NULL)
  8. {
  9. MessageBox(0,"ERROR: unable to load DLL","Error",0);
  10. }
  11. else
  12. {
  13. // Get function pointer
  14. MyFunction = (importFunction)GetProcAddress(hinstLib, "Mensaje");
  15. if (MyFunction == NULL)
  16. {
  17. MessageBox(0,"ERROR: unable to find DLL function","Error",0);
  18. FreeLibrary(hinstLib);
  19. }
  20. else
  21. {
  22.  
  23. MyFunction();
  24.  
  25. // Unload DLL file
  26. FreeLibrary(hinstLib);
  27. }
  28. }
  29.  

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é  ;-)


« Última modificación: 12 Octubre 2010, 22:39 pm por Miseryk » En línea

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


Desconectado Desconectado

Mensajes: 392

Odio las ranas.


Ver Perfil
Re: [Ayuda] C++ llamando Dll
« Respuesta #1 en: 12 Octubre 2010, 22:41 pm »

Yo tengo hechas en C y cargadas en programas compilados en C.
lo primero, en la dll tenia que poner
Código
  1. __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



En línea

Miseryk

Desconectado Desconectado

Mensajes: 225


SI.NU.SA U.GU.DE (2NE1 - D-Unit)


Ver Perfil
Re: [Ayuda] C++ llamando Dll
« Respuesta #2 en: 12 Octubre 2010, 23:08 pm »

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
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6. typedef void (*MsgFunction) (void);
  7.  
  8. HINSTANCE hinstDLL;
  9.  
  10. int main()
  11. {
  12. MsgFunction MsgBox;
  13. hinstDLL = LoadLibrary("Project1.dll");
  14. if(hinstDLL != 0)
  15. {
  16. printf("DLL LOADED.\n");
  17. MsgBox = (MsgFunction)GetProcAddress(hinstDLL,"Mensaje");
  18.  
  19. if(MsgBox != 0)
  20. {
  21. printf("FUNCTION FOUND.\n");
  22. //Call Function
  23. MsgBox;
  24. }
  25. else
  26. {
  27. printf("FUNCTION NOT FOUND.\n");
  28. }
  29.  
  30. // Unload DLL file
  31. FreeLibrary(hinstDLL);
  32. }
  33. else
  34. {
  35. printf("DLL NOT LOADED.\n");
  36. }
  37. system("PAUSE");
  38. return 0;
  39. }
  40.  


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
  1. Option Explicit
  2.  
  3. 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
  4.  
  5. Public Sub Mensaje()
  6. MessageBox 0, "Test", "Caption", 0
  7. End Sub
  8.  
  9. Public Function Suma(ByVal n1 As Long, ByVal N2 As Long) As Long
  10. Suma = n1 + N2
  11. End Function

Código
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6. typedef void (WINAPI*MsgFunction) ();
  7. typedef long (WINAPI*SumaFunction) (long,long);
  8.  
  9. int main()
  10. {
  11. MsgFunction MsgBox;
  12. SumaFunction Suma;
  13.  
  14. HINSTANCE hinstDLL = LoadLibrary("Project1.dll");
  15.  
  16. if(hinstDLL != 0)
  17. {
  18. printf("DLL LOADED.\n");
  19. MsgBox = (MsgFunction)GetProcAddress(hinstDLL,"Mensaje");
  20.  
  21. MsgBox();
  22.  
  23.  
  24.  
  25. Suma = (SumaFunction)GetProcAddress(hinstDLL,"Suma");
  26.  
  27. long x = Suma(6,6);
  28. if(x == 12)
  29. {
  30. cout << "Message Displayed!\n";
  31. }
  32.  
  33. // Unload DLL file
  34. FreeLibrary(hinstDLL);
  35. }
  36. else
  37. {
  38. printf("DLL NOT LOADED.\n");
  39. }
  40.  
  41. system("PAUSE");
  42. return 0;
  43. }
« Última modificación: 13 Octubre 2010, 19:06 pm por Littlehorse » En línea

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


Desconectado Desconectado

Mensajes: 392

Odio las ranas.


Ver Perfil
Re: [Ayuda] C++ llamando Dll
« Respuesta #3 en: 13 Octubre 2010, 15:57 pm »

Código:
MessageBox 0, "Test", "Caption", 0

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

Código:
MessageBox 0, "Test", "Caption", MB_OK

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

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

Miseryk

Desconectado Desconectado

Mensajes: 225


SI.NU.SA U.GU.DE (2NE1 - D-Unit)


Ver Perfil
Re: [Ayuda] C++ llamando Dll
« Respuesta #4 en: 13 Octubre 2010, 16:13 pm »

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.
En línea

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!!!
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Suplantan el correo del presidente del Congreso llamando a 'desocuparlo' el 25-S
Noticias
wolfbcn 0 1,049 Último mensaje 20 Septiembre 2012, 20:47 pm
por wolfbcn
¿Quién me está llamando?
Foro Libre
El_Andaluz 2 1,779 Último mensaje 21 Diciembre 2017, 22:03 pm
por El_Andaluz
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines