Click here to Skip to main content
15,902,896 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: CFileDialog creates problem under unicode standard Pin
David Crow11-Nov-09 5:14
David Crow11-Nov-09 5:14 
QuestionCreating a expert system and need some help please Pin
akshay174111-Nov-09 4:19
akshay174111-Nov-09 4:19 
AnswerRe: Creating a expert system and need some help please Pin
Richard MacCutchan11-Nov-09 7:48
mveRichard MacCutchan11-Nov-09 7:48 
AnswerRe: Creating a expert system and need some help please Pin
Maximilien11-Nov-09 9:44
Maximilien11-Nov-09 9:44 
QuestionIntercepting the TerminateProcess event Pin
Krischu11-Nov-09 4:09
Krischu11-Nov-09 4:09 
AnswerRe: Intercepting the TerminateProcess event Pin
Covean11-Nov-09 4:23
Covean11-Nov-09 4:23 
GeneralRe: Intercepting the TerminateProcess event Pin
Krischu11-Nov-09 5:50
Krischu11-Nov-09 5:50 
GeneralRe: Intercepting the TerminateProcess event Pin
Covean11-Nov-09 21:19
Covean11-Nov-09 21:19 
In general I do not post my own code but doing some IPC-stuff is not such a secret.   Laugh | :laugh:

Here is a dll I wrote to communicate between 2 apps.
The one site calls
      GetFaceDetectionStatusChangedEventHandle(),
      CreateFaceDetectionStatusMapFile(),
      and GetCurrentFaceDetectionStatus() and closes it if it doesnt need it anymore.
the other site calls SetFaceDetectionStatus() instead of GetCurrentFaceDetectionStatus().

This code shows how to wait in one app for another and how to transfer
data from one app to the other by using shared memory.

Note: I left the german comments in for you.

------------------------------------------------------------------------------------------
fdinterop.h

#pragma once
#define WINVER 0x0501
#define _WIN32_WINNT 0x0501
#define _WIN32_WINDOWS 0x0410 
#define _WIN32_IE 0x0600
#define WIN32_LEAN_AND_MEAN

#include <windows.h>

////////////////////////////////////////////////////////////////////////////////////
// Konstanten //////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

TCHAR szEventName[]=TEXT("FDInteropDllFaceDetectionStatusChanged");
TCHAR szMapFileName[]=TEXT("FDInterOpDLLFaceDetectionMapFile");

////////////////////////////////////////////////////////////////////////////////////
// Strukturen //////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

// FaceDetectionStatus Hauptobjekt
typedef struct _fdsObject
{
     DWORD     dwTimeStamp;
     BOOL     bFaceDetected;
     INT          nX1;
     INT          nY1;
     INT          nX2;
     INT          nY2;
} FDS_OBJECT, *PFDS_OBJECT, NEAR *NPFDS_OBJECT, FAR *LPFDS_OBJECT;

////////////////////////////////////////////////////////////////////////////////////
// exportierte Funktionen //////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

// Gibt das Handle auf das systemweite, benannte Event zurück, welches ausgelöst wird, wenn mit
// SetFaceDetectionStatus neue Daten an den "wartenden" Prozess geschickt werden.
HANDLE _declspec(dllexport) __stdcall GetFaceDetectionStatusChangedEventHandle();

// Gibt das Event, dass mit GetFaceDetectionStatusChangedEventHandle geöffnet wurde, wieder frei.
void _declspec(dllexport) __stdcall CloseFaceDetectionStatusChangedEventHandle(HANDLE hFaceDetectionStatusChangedEventHandle);

// Gibt eine Handle auf einen prozessübergreifenden gemeinsamen Speicherbereich zurück.
HANDLE _declspec(dllexport) __stdcall CreateFaceDetectionStatusMapFile();

// Dekrementiert den Zähler, der angibt wieviele Prozesse derzeit auf den prozessübergreifenden gemeinsamen Speicherbereich zugreifen.
// Wird der Zähler auf 0 dekrementiert, so wird auch der Speicherbereich freigegeben.
void _declspec(dllexport) __stdcall CloseFaceDetectionStatusMapFile(HANDLE hFaceDetectionStatusMapFile);

// Versucht die aktuellen Werte der Gesichtserkennung aus dem prozessübergreifenden gemeinsamen Speicherbereich auszulesen
// und kopiert bei Erfolg die Werte in den Buffer pFDSObject.
// Sollte hFaceDetectionStatusMapFile oder pFDSObject NULL sein gibt die Funktion auf jeden Fall FALSE zurück.
BOOL _declspec(dllexport) __stdcall GetCurrentFaceDetectionStatus(HANDLE hFaceDetectionStatusMapFile, LPFDS_OBJECT pFDSObject);

// Setzt den aktuellen Status der Gesichtserkennung.
// Wird für bFaceDetected der Wert 0 angegeben, so bedeutet dies, daß zur Zeit kein Gesicht erkannt wird, jeder andere Wert entspricht einem
// erkannten Gesicht.
// Die Werte X1, Y1, X2, Y2 stellen die Koordinaten des Rechtecks dar in dem die Gesichtserkennung vermutlich ein Gesicht erkannt hat.
void _declspec(dllexport) __stdcall SetFaceDetectionStatus(INT bFaceDetected, INT nX1, INT nY1, INT nX2, INT nY2);


------------------------------------------------------------------------------------------
fdinterop.c

#include "fdinterop.h"

/////////////////////////////////////////////////////////////////////////////////////
//// DLL Hauptfunktion //////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

BOOL __stdcall DllMain(HANDLE hModule, DWORD dwReasonForCall, LPVOID lpReserved)
{
     switch(dwReasonForCall)
     {
          case DLL_PROCESS_ATTACH:
          case DLL_THREAD_ATTACH:
          case DLL_THREAD_DETACH:
          case DLL_PROCESS_DETACH:
               break;
     }
     return TRUE;
}

/////////////////////////////////////////////////////////////////////////////////////
//// GetFaceDetectionStatusChangedEventHandle ///////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

HANDLE __stdcall GetFaceDetectionStatusChangedEventHandle()
{
     return CreateEvent(NULL, TRUE, FALSE, szEventName);
}

/////////////////////////////////////////////////////////////////////////////////////
//// CloseFaceDetectionStatusChangedEventHandle /////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

void __stdcall CloseFaceDetectionStatusChangedEventHandle(HANDLE hFaceDetectionStatusChangedEventHandle)
{
     CloseHandle(hFaceDetectionStatusChangedEventHandle);
}

/////////////////////////////////////////////////////////////////////////////////////
//// CreateFaceDetectionStatusMapFile ///////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

HANDLE __stdcall CreateFaceDetectionStatusMapFile()
{
     return CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(FDS_OBJECT), szMapFileName);
}

/////////////////////////////////////////////////////////////////////////////////////
//// CloseFaceDetectionStatusMapFile ///////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

void __stdcall CloseFaceDetectionStatusMapFile(HANDLE hFaceDetectionStatusMapFile)
{
     CloseHandle(hFaceDetectionStatusMapFile);
}

/////////////////////////////////////////////////////////////////////////////////////
//// GetCurrentFaceDetectionStatus //////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

BOOL __stdcall GetCurrentFaceDetectionStatus(HANDLE hFaceDetectionStatusMapFile, LPFDS_OBJECT pFDSObject)
{     
     LPFDS_OBJECT pFDSBuffer = NULL;
     
     if((hFaceDetectionStatusMapFile == NULL) || (pFDSObject == NULL))
          return FALSE;

     pFDSBuffer = (LPFDS_OBJECT)MapViewOfFile(hFaceDetectionStatusMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(FDS_OBJECT));
     if(pFDSBuffer == NULL)
          return   FALSE;

     memcpy(pFDSObject, pFDSBuffer, sizeof(FDS_OBJECT));
     UnmapViewOfFile(pFDSBuffer);
     return TRUE;
}

/////////////////////////////////////////////////////////////////////////////////////
//// SetFaceDetectionStatus /////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

void __stdcall SetFaceDetectionStatus(INT bFaceDetected, INT nX1, INT nY1, INT nX2, INT nY2)
{
     FDS_OBJECT fdsObject;
     LPFDS_OBJECT pFDSObject;
     HANDLE hFaceDetectionStatusMapFile = NULL;
     HANDLE hFaceDetectionStatusChangedEvent = NULL;

     fdsObject.dwTimeStamp = GetTickCount();
     fdsObject.bFaceDetected = (bFaceDetected==0) ? FALSE : TRUE;
     fdsObject.nX1 = nX1;
     fdsObject.nY1 = nY1;
     fdsObject.nX2 = nX2;
     fdsObject.nY2 = nY2;

     // ToDo: Speicherzugriffe synchronisieren (bzw. muß man das mit diesen Funktionen hier überhaupt?)
     hFaceDetectionStatusMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, szMapFileName);
     if (hFaceDetectionStatusMapFile != NULL) 
     {
          pFDSObject = MapViewOfFile(hFaceDetectionStatusMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(FDS_OBJECT));                            
          if(pFDSObject != NULL)
          {
               memcpy(pFDSObject, &fdsObject, sizeof(FDS_OBJECT));
               UnmapViewOfFile(pFDSObject);
               CloseHandle(hFaceDetectionStatusMapFile);
          }
     }

     hFaceDetectionStatusChangedEvent = GetFaceDetectionStatusChangedEventHandle();
     SetEvent(hFaceDetectionStatusChangedEvent);
     CloseFaceDetectionStatusChangedEventHandle(hFaceDetectionStatusChangedEvent);
}
;

I hope this gives you a clue how to do this.
But be aware of the data transfer between the two apps, cause I do not lock the memory
for writing or reading (I didn't needed it, functions were only call in cylce of 40 ms).

Greetings
Covean

GeneralRe: Intercepting the TerminateProcess event Pin
Krischu11-Nov-09 22:02
Krischu11-Nov-09 22:02 
QuestionDetection of USB device connect and discoonect in vc++ DLL Pin
draxayani11-Nov-09 3:40
draxayani11-Nov-09 3:40 
QuestionRe: Detection of USB device connect and discoonect in vc++ DLL Pin
David Crow11-Nov-09 3:58
David Crow11-Nov-09 3:58 
AnswerRe: Detection of USB device connect and discoonect in vc++ DLL Pin
Covean11-Nov-09 4:11
Covean11-Nov-09 4:11 
AnswerRe: Detection of USB device connect and discoonect in vc++ DLL Pin
David Crow11-Nov-09 4:14
David Crow11-Nov-09 4:14 
GeneralRe: Detection of USB device connect and discoonect in vc++ DLL Pin
Covean11-Nov-09 4:16
Covean11-Nov-09 4:16 
GeneralRe: Detection of USB device connect and discoonect in vc++ DLL Pin
draxayani12-Nov-09 1:55
draxayani12-Nov-09 1:55 
GeneralRe: Detection of USB device connect and discoonect in vc++ DLL Pin
Covean12-Nov-09 2:29
Covean12-Nov-09 2:29 
GeneralRe: Detection of USB device connect and discoonect in vc++ DLL Pin
draxayani12-Nov-09 2:41
draxayani12-Nov-09 2:41 
QuestionHow to know which version of WTL i am having in my machine. [modified] Pin
pandit8411-Nov-09 3:40
pandit8411-Nov-09 3:40 
AnswerRe: How to know which version of WTL i am having in my machine. Pin
Randor 11-Nov-09 9:23
professional Randor 11-Nov-09 9:23 
Questionprogram Pin
singh.niraj40@yahoo.com11-Nov-09 2:40
singh.niraj40@yahoo.com11-Nov-09 2:40 
QuestionRe: program Pin
Maximilien11-Nov-09 3:12
Maximilien11-Nov-09 3:12 
AnswerRe: program Pin
Krischu11-Nov-09 3:21
Krischu11-Nov-09 3:21 
AnswerRe: program Pin
MindCoder7912-Nov-09 19:45
MindCoder7912-Nov-09 19:45 
Questionhow to using the bmp pic of the resource to fill a rectangle? Pin
kk_Kevin11-Nov-09 2:31
kk_Kevin11-Nov-09 2:31 
QuestionRe: how to using the bmp pic of the resource to fill a rectangle? Pin
David Crow11-Nov-09 3:25
David Crow11-Nov-09 3:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.