Ver Mensaje Individual
  #11 (permalink)  
Antiguo 15/10/2013, 16:18
vosk
 
Fecha de Ingreso: agosto-2012
Mensajes: 601
Antigüedad: 11 años, 8 meses
Puntos: 83
Respuesta: Temporizadores con parámetros

Como gesto de buena voluntad :)) te copio todo un proyecto con una dll que funciona correctamente; es decir que no influye el echo de estar en dll; incluso la dll la he echo en cpp y la aplicacion en c, donde la funcion de crear timers esta en la dll y el callback en la principal, mas interrelacion imposible y como podras ver funciona correctamente.

Código C++:
Ver original
  1. //dllmain.h
  2. #ifndef __MAIN_H__
  3. #define __MAIN_H__
  4.  
  5. #include <windows.h>
  6.  
  7. #ifdef BUILD_DLL
  8.     #define DLL_EXPORT __declspec(dllexport)
  9. #else
  10.     #define DLL_EXPORT __declspec(dllimport)
  11. #endif
  12.  
  13.  
  14. #ifdef __cplusplus
  15. extern "C"
  16. {
  17. #endif
  18.  
  19. struct TIMERDATA {
  20.     unsigned int hTimer;
  21.     int data;
  22. };
  23.  
  24.  
  25. void DLL_EXPORT crearTimers(HWND , struct TIMERDATA *, int , void CALLBACK (*cb)(HWND , unsigned int , unsigned int , unsigned long ));
  26.  
  27. #ifdef __cplusplus
  28. }
  29. #endif
  30.  
  31. #endif


Código C++:
Ver original
  1. //dllmain.cpp
  2. #include "dllmain.h"
  3.  
  4.  
  5. void DLL_EXPORT crearTimers(HWND hwnd, struct TIMERDATA *ptr_timerList, int numTimers, void CALLBACK (*cb)(HWND a, unsigned int b, unsigned int c, unsigned long d)) {
  6.     int q;
  7.  
  8.     for(q = 0; q < numTimers; q++) {
  9.         ptr_timerList[q].data = q+10;
  10.         ptr_timerList[q].hTimer = SetTimer(hwnd, q, 3000, cb);
  11.     }
  12. }
  13.  
  14.  
  15. extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
  16.     switch (fdwReason) {
  17.         case DLL_PROCESS_ATTACH:
  18.         break;
  19.         case DLL_PROCESS_DETACH:
  20.         break;
  21.         case DLL_THREAD_ATTACH:
  22.         break;
  23.         case DLL_THREAD_DETACH:
  24.         break;
  25.     }
  26.     return TRUE;
  27. }


Código C:
Ver original
  1. //aplicacion principal main.c
  2. #include <windows.h>
  3.  
  4. struct TIMERDATA {
  5.     unsigned int hTimer;
  6.     int data;
  7. };
  8. struct TIMERDATA timerList[3];
  9.  
  10.  
  11. typedef void (WINAPI *dllFuncion)(HWND , struct TIMERDATA *, int , void CALLBACK (*cb)(HWND , unsigned int , unsigned int , unsigned long ));
  12. dllFuncion crearTimers;
  13.  
  14.  
  15. void CALLBACK UnBan(HWND hwnd, unsigned int msg, unsigned int id, unsigned long st) {
  16.     struct TIMERDATA *ptr;
  17.  
  18.     if((ptr = (id < 3)? &timerList[id] : 0)) {
  19.         printf("ID %d, DATA %d\n", id, ptr->data);
  20.         ptr->data += 1;
  21.     }
  22. }
  23.  
  24.  
  25. LRESULT CALLBACK procedimientoPrincipal(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  26.     switch(msg) {
  27.         case WM_CREATE: {
  28.             HINSTANCE hLib;
  29.             char modulo[256];
  30.  
  31.             if((hLib = LoadLibrary("dlltest.dll"))) {
  32.                 GetModuleFileName((HMODULE)hLib, (LPTSTR)modulo, sizeof(modulo));
  33.                 if((crearTimers = (dllFuncion)GetProcAddress((HMODULE)hLib, "crearTimers"))) {
  34.                     crearTimers(hwnd, timerList, 3, &UnBan);
  35.                 }
  36.                 else {
  37.                     MessageBox(hwnd, "Error GetProcAddress", "", MB_OK);
  38.                 }
  39.  
  40.                 FreeLibrary((HMODULE)hLib);
  41.             }
  42.             else {
  43.                 MessageBox(hwnd, "Error LoadLibrary", "", MB_OK);
  44.             }
  45.         }
  46.         break;
  47.  
  48.         case WM_CLOSE: {
  49.             DestroyWindow(hwnd);
  50.         }
  51.         break;
  52.  
  53.         case WM_DESTROY: {
  54.             PostQuitMessage(0);
  55.         }
  56.         break;
  57.  
  58.         default: return DefWindowProc(hwnd, msg, wParam, lParam);
  59.     }
  60.  
  61.     return 0;
  62. }

Compila la dll, copia el *.dll resultante en el proyecto de la aplicacion, crea una winmain para la aplicacion, compila y ejecuta. En este caso he importado manualmente las funciones del dll, por eso no hace falta incluir la cabecera ni linkar la libreria (aun en caso que el compilador te la cree).

Prueba esto y haz que funcione (funciona perfecto), luego implementa en tu aplicacion. Si cuando implementas en tu aplicacion no funciona es que tienes algo mal; si no funciona el ejemplo es que tambien estas haciendo algo mal. Editado en code::blocks, compilado con el mingw que viene por defecto, y ejecutado en windows7.

Suerte
Saludos
vosk