Memory Mapping in Visual C++ programming
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Memory Mapping in Visual C++ programming

  1. #1
    Join Date
    Jun 2012
    Posts
    8

    Unhappy Memory Mapping in Visual C++ programming

    Hi,

    I am doing the memory mapping in between two processes. I used the example from Creating Named and Shared memory from MSDN. I can write and read the data in between two processes, but the problem i am facing is, in middle of this process after some time, some values are read as zero while reading from Process 2 which are written by Process 1, resulting in error in my process. Also tell me whether I have to maintain any timing in between the processes when doing Memory mapping. How to solve this memory reset problem.

    Please help me in solving this problem. Will be waiting for reply.

    Thank You in advance.

  2. #2
    Join Date
    Oct 2011
    Posts
    97

    Re: Memory Mapping in Visual C++ programming

    This may seem obvious, but I have to ask before continuing: are you using a mutex or semaphore to protect access to the memory? Because yes, you do indeed have to maintain timing between the processes. The OS is going to schedule your processes completely randomly (at least to you) and if something needs to happen in a certain order, you need to make sure you do that.

  3. #3
    Join Date
    Jun 2012
    Posts
    8

    Re: Memory Mapping in Visual C++ programming

    Thank You for your reply.

    Actually what i am doing is, i have Process-1 which need hardware details and Process-2 which consists of hardware details. And these details are shared in between these two processes which are available in memory using the memory mapping example from Create Named and Shared memory from MSDN library. I am not using any threads in those two processess and no mutex and no semaphore. So during this I read zero in middle after sometime which results in errors. Please let me know how to proceed further.


    Thank You.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Memory Mapping in Visual C++ programming

    Quote Originally Posted by afroza View Post
    Please let me know how to proceed further.
    We don't have your code. The error can be anything -- maybe your program has a simple bug and has nothing to do with threads or even memory mapped files.

    Until we have your program, compile the code, and duplicate the error, you're the only one right now who should know how to proceed. All I can tell you is to debug your code more thoroughly.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jun 2012
    Posts
    8

    Re: Memory Mapping in Visual C++ programming

    I have gone through the code, and i am taking log of the data where i am writing continuously 1 and reading continuously 1. In log file writes remain 1 but reading side log changing in middle from 1 to 0 and again 0 to 1.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Memory Mapping in Visual C++ programming

    Quote Originally Posted by afroza View Post
    I have gone through the code, and i am taking log of the data where i am writing continuously 1 and reading continuously 1. In log file writes remain 1 but reading side log changing in middle from 1 to 0 and again 0 to 1.
    So how does that change the fact that we do not have your code to see what you're doing? You're asking us to magically guess what the issue is, and no one can do that without the code in hand.

    Again, you need to debug further -- you have the program, code, etc. You're in a much better position to see what the problem is then anyone here.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jun 2012
    Posts
    8

    Re: Memory Mapping in Visual C++ programming

    Hello Sir,

    I have gone through the code for debugging it as you said. I found that the pointer which i am using to point the memory is pointing to the wrong location after some time. And hence the wrong data i am reading. Now i am working on it to solve.

    Thank You for your support.

    Sorry for the inconvenience.

  8. #8
    Join Date
    Jun 2012
    Posts
    8

    Re: Memory Mapping in Visual C++ programming

    Please help me with the below issue, where i am going wrong.


    I am doing the memory mapping in between two process. Process 1 writing into memory and Process 2 reading from memory.My system is getting hanged when I execute the below Processess. But when I run these two process in Debugging mode, writes and reads of memory is correct and system won't hang. Please tell me where i am going wrong.

    Thank You .
    Will be waiting for the reply.

    Code:
    WRITE PROCESS
    
    //---------------------------------------------------------------------------
    
    #include "StdAfx.h"
    #include <stdio.h>
    #include <windows.h>
    #include <conio.h>
    
    /****************************************************************************/
    //Memeory Mapped
    
    #define		BUF_SIZE	1024
    TCHAR szName[]	=	TEXT("Global\\ContinousValues");
    LPWORD pBuf;
    LPWORD SavedPointer;
    
    HANDLE handleMappedFile;
    
    int WriteIntoGUIMemory();
    
    FILE *log1;
    
    unsigned int TimerCount;
    
    WORD Data_Buffer[20];
    
    unsigned int Data0 = 144;
    unsigned int Data1 = 1234;
    unsigned int Data2 = 2566;
    unsigned int Data3 = 3;
    unsigned int Data4 = 4;
    unsigned int Data5 = 5;
    
    unsigned int loop;
    unsigned char onetime = 0;
    
    unsigned int CloseCounter = 0;
    
    
    /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    
    //void main()//int argc, char **argv)
    UINT Communicate ( LPVOID param )
    {
    //	log1	=	fopen("D:\\WritingSide.xls","w");
    	
    	while(CloseCounter<50)
    	{
    		if(TimerCount >= 10)
    		{
    			WriteIntoGUIMemory();
    			TimerCount = 0;
    
    			CloseCounter++;
    		}		
    	}
    
    	UnmapViewOfFile(SavedPointer);
    	CloseHandle(handleMappedFile);
    
    	CloseCounter = 0;
    	onetime = 0;
    
    //	fclose(log1);
    	
    	return 1;
    
    }
    
    
    /******************************************************************/
    /**********************************************************************/
    
    int WriteIntoGUIMemory()
    {
    	if(!onetime)
    	{
    
    		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");
    			return 1;
    		}
    
    		pBuf = (LPWORD)MapViewOfFile(handleMappedFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
    
    		onetime = 1;
    	}
    
    	SavedPointer = pBuf;
    
    	if (pBuf == NULL)
    	{
    		printf("Could not map view of file.\n");
    		CloseHandle(handleMappedFile);
    		return 1;
    	} 
    	
    	memset((LPWORD)pBuf, 0, sizeof(char) * BUF_SIZE);
    
    
    	Data_Buffer[0]	=	 Data0;
    	Data_Buffer[1]	=	 Data1;
    	Data_Buffer[2]	=	 Data2;
    	Data_Buffer[3]	=	 Data3;
    	Data_Buffer[4]	=	 Data4;
    
    	for(loop=0;loop<5;loop++)
    	{
    		*pBuf	=	Data_Buffer[loop];
    	//	fprintf(log1,"value[%d]	=	%d\n",loop,*pBuf);
    		pBuf++;
    	}	
    
    	pBuf	=	SavedPointer;
    
    	Data0+=10; //@@
    
    	return 1;
    	
    }
    
    /**********************************************************************/
    Code:
    READ PROCESS
    
    
    //---------------------------------------------------------------------------
    
    #include "StdAfx.h"
    #include <stdio.h>
    #include <windows.h>
    #include <conio.h>
    
    /****************************************************************************/
    //Memeory Mapped
    
    #define		BUF_SIZE	1024
    TCHAR szName[]	=	TEXT("Global\\ContinousValues");
    LPWORD pBuf;
    LPWORD SavedPointer;
    
    
    int ReadFromGUIMemory();
    
    FILE *log1;
    
    unsigned int TimerCount;
    
    
    
    WORD Data_Buffer[20];
    
    unsigned int Data0 = 144;
    unsigned int Data1 = 1234;
    unsigned int Data2 = 2566;
    unsigned int Data3 = 3;
    unsigned int Data4 = 4;
    unsigned int Data5 = 5;
    
    unsigned int loop;
    
    HANDLE handleMappedFile;
    
    unsigned char onetime = 0;
    unsigned int CloseCounter = 0;
    
    /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    
    //void main()//int argc, char **argv)
    UINT Communicate ( LPVOID param )
    {
    //	log1	=	fopen("D:\\ReadingSide.xls","w");
    	
    	while(CloseCounter<50)
    	{
    		if(TimerCount >= 10)
    		{
    			ReadFromGUIMemory();
    			TimerCount = 0;
    
    			CloseCounter++;			
    		}
    	}
    
    	UnmapViewOfFile(SavedPointer);
    	CloseHandle(handleMappedFile);
    
    	CloseCounter = 0;
    	onetime = 0;
    
    //	fclose(log1);
    
    	return 1;
    
    }
    
    
    /******************************************************************/
    /**********************************************************************/
    int ReadFromGUIMemory()
    {
    	if(onetime == 0)
    	{
    
    		handleMappedFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, szName);
    		if (handleMappedFile == NULL)
    		{
    			printf("Could not open file mapping object (%d).\n",GetLastError());
    			return 1;
    		}
    		
    		pBuf = (LPWORD)MapViewOfFile(handleMappedFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
    
    		onetime = 1;
    	}
    
    	SavedPointer = pBuf;
    
    	if (pBuf == NULL)
    	{
    		printf("Could not map view of file.\n");
    		CloseHandle(handleMappedFile);
    		return 1;
    	} 
    	
    
    	for(loop=0;loop<5;loop++)
    	{
    		Data_Buffer[loop] = *pBuf;
    	//	fprintf(log1,"value[%d]	=	%d\n",loop,*pBuf);
    		pBuf++;
    	}
    
    	pBuf	=	SavedPointer;
    
    	return 1;
    	
    }
    
    /**********************************************************************/
    Last edited by afroza; June 4th, 2012 at 11:03 PM.

  9. #9
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: Memory Mapping in Visual C++ programming

    Please edit your post. Add code tags [code]Your code here[/code] and re-indent it to make it readable.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  10. #10
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Memory Mapping in Visual C++ programming

    Quote Originally Posted by afroza View Post
    Hello Sir,

    Please help me with the below issue, where i am going wrong.
    Until you use code tags no one is going to read that code.

    The code is unformatted and almost unreadable. Please edit your post to use code tags.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Jun 2012
    Posts
    8

    Re: Memory Mapping in Visual C++ programming

    Hello Sir,

    Problem am facing is my system hangs, when I run the two procesess.

    Sorry Sir, Now I have added the code tags. Forgive me for my mistake. Actually am the new user of VC++ and also looks only on the interfacing part of hardware with GUI, and doing first time the Memory mapping and also I have debugged the code using my knowledge. So please tell me my mistake.

    Thank You.

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: Memory Mapping in Visual C++ programming

    Quote Originally Posted by afroza View Post
    Hello Sir,

    Problem am facing is my system hangs, when I run the two procesess.
    The "system hangs" or your processes go into infinite (while) loops?
    Did you try to debug your processes?

    Your variable
    Code:
    unsigned int TimerCount;
    is not initialized. So are you using it?
    Victor Nijegorodov

  13. #13
    Join Date
    Jun 2012
    Posts
    8

    Re: Memory Mapping in Visual C++ programming

    Yes I have initialized it in other cpp file. TimerCount is not a problem.
    The System only hangs and not the processess. Is my way of using memory is correct? Can you please tell me how to do the same thing.

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: Memory Mapping in Visual C++ programming

    Again:
    Quote Originally Posted by VictorN View Post
    Did you try to debug your processes?
    Note that until you'll have learnt how to debug your code you won't be able to write any software (well, perhaps with the one exception: "Hello World!" application)
    Victor Nijegorodov

  15. #15
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Memory Mapping in Visual C++ programming

    Quote Originally Posted by afroza View Post
    Sorry Sir, Now I have added the code tags.
    Where is the main() program? The flow of the program is just as important, if not more so, than just posting two non-main() functions. Please post a main() program for both programs, so that we know the exact flow of the program.

    Also, saying that "you initialized a variable in another function" means nothing to us unless we see for ourselves everything you're doing. Too many times we have posters saying "I did this and did that", but when we finally see the code, they didn't do what they said they did (or they did it incorrectly).

    Regards,

    Paul McKenzie

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center