Click here to Skip to main content
15,912,932 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Is there any library which can detect the faulty statement at runtime? Pin
Rolf Kristensen27-Sep-11 8:20
Rolf Kristensen27-Sep-11 8:20 
QuestionErranous behavouir in Modless Dialog Pin
Amrit Agr26-Sep-11 21:30
Amrit Agr26-Sep-11 21:30 
SuggestionRe: Erranous behavouir in Modless Dialog Pin
Madhu Nair26-Sep-11 21:59
Madhu Nair26-Sep-11 21:59 
GeneralRe: Erranous behavouir in Modless Dialog Pin
Amrit Agr26-Sep-11 22:37
Amrit Agr26-Sep-11 22:37 
QuestionPost-mortem debugging under Win7 Home Premium Pin
Code-o-mat26-Sep-11 10:52
Code-o-mat26-Sep-11 10:52 
AnswerRe: Post-mortem debugging under Win7 Home Premium Pin
Erudite_Eric26-Sep-11 21:44
Erudite_Eric26-Sep-11 21:44 
GeneralRe: Post-mortem debugging under Win7 Home Premium Pin
Code-o-mat26-Sep-11 22:08
Code-o-mat26-Sep-11 22:08 
QuestionRead/Write from two process Pin
manju 326-Sep-11 3:39
manju 326-Sep-11 3:39 
Hi all,
I want to write and read simulataneously from one process to another process using memory mapped.
I am able to write from one process and read from the other.
I am not able to read simultaneously.
Example: I have two process:
procees1 and process2
"Process 1 is writing",so if i run both exe,Process2 will read the message from process2.
Same way
I want to write in process2 simultaneously,so that i can read message from Process1 simulatenously
Here is the code i am using

Process1.cpp
// Procees1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>


#define BUF_SIZE 2048
char buffer[BUF_SIZE];
TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
TCHAR szName_read[]=TEXT("Global\\MyFileMappingObject1");
char *pSharedBuffer;

//LPCTSTR pBuf;


int addMsg(char *msg, char *buffer)
{
    long msgCount, curMsgIndex, curMsgLen;
    char *newStrPos;
 
    // get count of messages stored
    memcpy(&msgCount, buffer, sizeof(long));
 
    // first message begins after sizeof(long) bytes - since we stored the num of messages as a long
    newStrPos = (char*) (long)buffer + sizeof(long);
 
    // step through the buffer, until we find the next free space to insert a new message
    for (curMsgIndex=0; curMsgIndex<msgCount; curMsgIndex++)
    {
        curMsgLen = strlen(newStrPos);
        newStrPos += curMsgLen + 1;
    }
 
    // copy the message
    strcpy(newStrPos, msg);
 
    // update the message count
    msgCount++;
    memcpy(buffer, &msgCount, sizeof(long));
 
    return 0;
}
 



int _tmain()
{
    HANDLE handleMappedFile;
	 HANDLE handleMappedFile_read;
    //LPVOID lpMsgBuf;
 
    handleMappedFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, HIWORD(BUF_SIZE), LOWORD(BUF_SIZE), szName);
    if (handleMappedFile == NULL)
    {
        printf("Could not create file mapping object.\n");
        //LocalFree( lpMsgBuf );
        //return 1;
    }
	 handleMappedFile_read = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, HIWORD(BUF_SIZE), LOWORD(BUF_SIZE), szName_read);
    if (handleMappedFile_read == NULL)
    {
        printf("Could not create file mapping object1.\n");
        //LocalFree( lpMsgBuf );
        return 1;
    }
 
    pSharedBuffer = (char*)MapViewOfFile(handleMappedFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
    if (pSharedBuffer == NULL)
    {
        printf("Could not map view of file.\n");
        CloseHandle(handleMappedFile);
        return 1;
    } 
 
    memset((PVOID)pSharedBuffer, 0, sizeof(char) * BUF_SIZE);
 
    addMsg("Process 1 is writing", pSharedBuffer);
    

    getch();
    UnmapViewOfFile(pSharedBuffer);  
    CloseHandle(handleMappedFile);  
 
    return 0;

}


Process2.cpp


// Process2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#pragma comment(lib, "user32.lib")



#define BUF_SIZE 2048

TCHAR szName1[]=TEXT("Global\\MyFileMappingObject");
TCHAR szName_writing[]=TEXT("Global\\MyFileMappingObject1");
LPCTSTR pSharedBuffer;

int _tmain()
{
	/*checks if the process1 is running or not*/
	HANDLE hMappedFile; 
    hMappedFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, szName1); // name of mapping object
    if (hMappedFile == NULL)
    {
        printf("Could not open file mapping object (%d).\n",GetLastError());
        //return 1;
    }
	//for writing
    	HANDLE hMappedFile_Writing;
        hMappedFile_Writing = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, szName_writing); 
		if (hMappedFile_Writing == NULL)
    {
        printf("Could not open file mapping object (%d).\n",GetLastError());
        return 1;
    }
    /**/
    pSharedBuffer =(LPTSTR)MapViewOfFile(hMappedFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
    if (pSharedBuffer == NULL)
    {
        printf("Could not map view of file (%d).\n", GetLastError());
        CloseHandle(hMappedFile);
        return 1;
    }
 
    long msgCount, curMsgNum;
    char *curMsg;
    memcpy(&msgCount, pSharedBuffer, sizeof(long));
    //printf("---- Messages retrieved: %d ----\n", msgCount);
    curMsg = (char*)pSharedBuffer + sizeof(long);
    for (curMsgNum=0; curMsgNum<msgCount; curMsgNum++)
    {
       // printf("msg(%d): %s\n", curMsgNum, curMsg);
		 printf("%s\n", curMsg);
        curMsg += strlen(curMsg) + 1;
    }
 
    UnmapViewOfFile(pSharedBuffer);
    CloseHandle(hMappedFile);
    getch();
    return 0;

}

If anyone has any idea,please let me know

Thanks and Regards
Sharan
AnswerRe: Read/Write from two process Pin
Software_Developer26-Sep-11 23:44
Software_Developer26-Sep-11 23:44 
GeneralRe: Read/Write from two process Pin
manju 327-Sep-11 0:31
manju 327-Sep-11 0:31 
QuestionSolved : Resize ComboBox Window Pin
tagopi26-Sep-11 0:56
tagopi26-Sep-11 0:56 
AnswerRe: Resize ComboBox Window Pin
Code-o-mat26-Sep-11 1:28
Code-o-mat26-Sep-11 1:28 
GeneralRe: Resize ComboBox Window Pin
tagopi26-Sep-11 1:38
tagopi26-Sep-11 1:38 
Question::__argv ist NULL, but ::__argc is correct, why? Pin
Erik25-Sep-11 23:58
Erik25-Sep-11 23:58 
AnswerRe: ::__argv ist NULL, but ::__argc is correct, why? Pin
Madhu Nair26-Sep-11 0:29
Madhu Nair26-Sep-11 0:29 
AnswerRe: ::__argv ist NULL, but ::__argc is correct, why? Pin
jk chan26-Sep-11 3:52
jk chan26-Sep-11 3:52 
QuestionInstall LSP(Layered Service Provider) without a reboot? Pin
Syouki_kou25-Sep-11 20:28
Syouki_kou25-Sep-11 20:28 
Questionwrite multistring value to ADS key Pin
jkirkerx25-Sep-11 13:08
professionaljkirkerx25-Sep-11 13:08 
AnswerRe: write multistring value to ADS key Pin
Richard Andrew x6425-Sep-11 14:05
professionalRichard Andrew x6425-Sep-11 14:05 
GeneralRe: write multistring value to ADS key Pin
jkirkerx25-Sep-11 15:31
professionaljkirkerx25-Sep-11 15:31 
Questionreference prob with namespace C++ Pin
Schehaider_Aymen25-Sep-11 12:00
Schehaider_Aymen25-Sep-11 12:00 
AnswerRe: reference prob with namespace C++ Pin
Schehaider_Aymen25-Sep-11 12:39
Schehaider_Aymen25-Sep-11 12:39 
GeneralRe: reference prob with namespace C++ Pin
Emilio Garavaglia25-Sep-11 21:20
Emilio Garavaglia25-Sep-11 21:20 
GeneralRe: reference prob with namespace C++ Pin
Schehaider_Aymen25-Sep-11 22:48
Schehaider_Aymen25-Sep-11 22:48 
GeneralRe: reference prob with namespace C++ Pin
Richard MacCutchan25-Sep-11 23:03
mveRichard MacCutchan25-Sep-11 23:03 

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.