|
Hi,
I tried splitting the functions as you said,but some junk values is coming in second process.
This is what i am trying to do.
In Process 1
// 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 szMsg[]=TEXT(" Read Values from first process.");
LPCTSTR pBuf;
int addMsg(char *msg)
{
long msgCount, curMsgIndex, curMsgLen;
char *newStrPos;
memcpy((PVOID)&msgCount, buffer, sizeof(long));
newStrPos = (char*) (long)&buffer + sizeof(long);
for (curMsgIndex=0; curMsgIndex<msgCount; curMsgIndex++)
{
curMsgLen = strlen(newStrPos);
newStrPos += curMsgLen + 1;
}
{
printf("adding message\n");
strcpy(newStrPos, msg);
CopyMemory((PVOID)pBuf, msg, (_tcslen(szMsg) * sizeof(TCHAR)));
msgCount++;
memcpy((PVOID)buffer, &msgCount, sizeof(long));
}
return 0;
}
int _tmain()
{
HANDLE hMapFile1;
hMapFile1 = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
szName); // name of mapping object
if (hMapFile1 == NULL)
{
_tprintf(TEXT("Could not create file mapping object (%d).\n"),
GetLastError());
return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile1, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile1);
return 1;
}
memset((PVOID)buffer, 0, sizeof(char) * BUF_SIZE);
addMsg("This is msg 1");
addMsg("This is msg 2");
_getch();
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile1);
return 0;
}
In Process2
// 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")
/*int main(int argc, char* argv[])
{
printf("Hello World!\n");
return 0;
}*/
#define BUF_SIZE 256
TCHAR szName1[]=TEXT("Global\\MyFileMappingObject");
int _tmain()
{
HANDLE hMapFile;
LPCTSTR pBuf;
hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
szName1); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not open file mapping object (%d).\n"),
GetLastError());
return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
//MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
long msgCount, curMsgNum;
char *curMsg;
memcpy(&msgCount, pBuf, sizeof(long));
curMsg = (char*)(long)&pBuf + sizeof(long);
for (curMsgNum=0; curMsgNum<msgCount;curMsgNum++)
{
printf("%s\n", curMsg);
curMsg += strlen(curMsg) + 1;
}
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}
I dont know where i am wrong,Please can you help me where i am going wrong
Thanks
Sharan
|
|
|
|
|
Hullo again.
I've looked at your code and noticed that when referring to memory, there seemed to be mixed references to the buffer that was shared, and the one that was locally defined.
I've gone through and made a number of changes, sorry I can't think of them all right now. Sorry I also made it non-unicode. I've little experience with it and have no impulse to further that just now.
Try this: (yup, tested and working )
Hope your mileage is better than last time..
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#define BUF_SIZE 2048
char *szName = "Global\MyFileMappingObject";
char *pSharedBuffer;
int addMsg(char *msg, char *buffer)
{
long msgCount, curMsgIndex, curMsgLen;
char *newStrPos;
memcpy(&msgCount, buffer, sizeof(long));
newStrPos = (char*) (long)buffer + sizeof(long);
for (curMsgIndex=0; curMsgIndex<msgCount; curMsgIndex++)
{
curMsgLen = strlen(newStrPos);
newStrPos += curMsgLen + 1;
}
strcpy(newStrPos, msg);
msgCount++;
memcpy(buffer, &msgCount, sizeof(long));
return 0;
}
int main()
{
HANDLE handleMappedFile;
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;
}
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("This is msg 1", pSharedBuffer);
addMsg("This is msg 2", pSharedBuffer);
addMsg("This is msg 3", pSharedBuffer);
getch();
UnmapViewOfFile(pSharedBuffer);
printf("File unmapped\n");
CloseHandle(handleMappedFile);
printf("handle closed.\n");
return 0;
}
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#define BUF_SIZE 2048
char *szName1= "Global\MyFileMappingObject";
char *pSharedBuffer;
int main()
{
HANDLE hMappedFile;
hMappedFile = OpenFileMapping(ILE_MAP_ALL_ACCESS, FALSE, szName1);
if (hMappedFile == NULL)
{
printf("Could not open file mapping object (%d).\n",GetLastError());
return 1;
}
pSharedBuffer = 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);
curMsg += strlen(curMsg) + 1;
}
UnmapViewOfFile(pSharedBuffer);
CloseHandle(hMappedFile);
getch();
return 0;
}
|
|
|
|
|
Thanks a lott,its working fine
Regards
Sharan
|
|
|
|
|
Hi,
Can we share the data simulatenously from process1 and process2,as well as process2 and process1.
means write and read data to and from another process??
Thanks
Sharan
|
|
|
|
|
Hi all,
I detected some diferences in my program results between Release and Debug versions. After some research I realized that some floating point optimizations are causing those differences. I have solved the problem by using fenv_access pragma for disabling some optimizations for some critical methods.
Thinking about it, I realized that, probably, is better to use fp:strict model instead of fp:precise in my program because of its characteristics but I am worried about performance. I have tried to find some information about performance issues of fp:strict or the differences in performance between precise and strict model but I have find very little information. Anybody knwos anything about this??
Thanks in advance.
|
|
|
|
|
Is it possible to receive an event in an MFC program running on the client when a file on a web server is modified?
I want to do this without .net or any special software, only HTML, PHP and javascript. I plan to host the web on a shared host and the code should work out-of-the-box on any web host which supports php programming.
If this is not possible, would it be possible for javascript (I don't know javascript) to inform the MFC program running on the client when it modifies the file (socket communication?)?.
I know how to poll a php page from MFC, but polling a web page every second to evaluate if a file has changed is certainly not the way to go.
Since I'm totally new to all this, code snippets would be very much appreciated.
Thank you
|
|
|
|
|
A tcp socket connection is ideal because the connection is always there. But in real life it's rarely feasible in most internet situations because of networking administration issues (can't have open ports, firewall configurations, etc.).
However, HTTP on standard port 80 works for pretty much anyone with internet access. But HTTP is request/reply so polling is generally used. Take a look at this document to see a description of how MS implemented the Polling Duplex HTTP support for Silverlight. Silverlight 2 WCF Polling Duplex Support – Part 1: Architecture[^] Maybe some ideas there...
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
If I understand you and the linked article correctly, there is no real life possibility to use sockets. Setting up port-forwarding in modems is definitely out of question.
My only option seems, therefore, to be to use CHttpFile and send defined requests to a php web page every second (I need almost real-time performance) and read the web contents returned.
If this is the only way to do reliable duplex communications, so be it. To me it seems a terrible waste of bandwidth and I would have thaught that better methods existed.
Unless someone tells me that I have completely misunderstood the answer given, I will go ahead and experiment with CHttpFile and co.
|
|
|
|
|
The bandwidth issue isn't that bad. The implementation described at the link uses the reply timeout before sending another request, so if no data is available for the client, polling requests are only sent once every 60 seconds by default. And the requests are very small. Viewing a webpage can cause hundreds of requests in a very short period of time (like a second), so one little request a minute is nothing.
I personally would use (and have used) WinHTTP[^], but CHttpFile may be fine for your needs.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I am Using the VC++ coding,I don't know how to sort the list of files in a folder.Can anybody know please give me the solution with syntax also....
Thanks,
manojkumar.U
|
|
|
|
|
What do you mean exactly by "sort the list of files in a folder" ? There is no such thing as that. You can view them sorted in the explorer but they still are not "sorted" on your hard disk.
What are you trying to achieve ? Do you want to modify the sort order for a specific folder within the explorer ? Do you want to retrieve a sorted list of files to use within your application ?
Which kind of sorting do you need ?
Well, you really need to describe your problem in more details...
|
|
|
|
|
This control[^] should help you get what you want.
The best things in life are not things.
|
|
|
|
|
i want code for ,two string arrays,if one element in the string array is compared to the another all elements in th enext array,if it is findout discard the element go to the next element.
|
|
|
|
|
Sorry, but I don't understand what you are asking for here. If you need to sort a list of strings then either use one of the STL[^] types that provides for sorting, or use the CRT quick sort[^] function. If you are trying to reduce an array to unique entries, then once again one of the STL templates should work for you.
The best things in life are not things.
|
|
|
|
|
|
When I know the exe path of a file:
hExeModule = LoadLibrary(exe);
...blah..blah..
FindResource(hExeModule, "#N", RT_ICON);
While there are so many icons in the exe file, how can I pass the N parameter as the icon index used for application's representitive main icon which appear on explorer ?
Thanks!!
|
|
|
|
|
If you know the value of N then you should send it as described here[^]. However if you do not know the names then you will need to use the EnumResourceNamesEx()[^] function to get them.
The best things in life are not things.
|
|
|
|
|
First result on Google that I code, CodeProject article[]
Might be interesting to read. I suspect that the first Icon in the file is the one used for the application, but I am not sure.
|
|
|
|
|
Hi all.
I have a link list in a vector structure. It throws exception while freeing first node of the list. This is the sample code-
typedef struct _TEST_STRUCT
{
TCHAR _tchTemp[MAX_PATH];
__int64 i64Temp;
DWORD dwTemp;
_TEST_STRUCT *Next;
}TEST_STRUCT;
typedef struct _TEST_VECTORST
{
int nVal;
TEST_STRUCT m_ListInVector;
}TEST_VECTORST;
std::vector<TEST_VECTORST> m_StVector;
CString csVal = L"";
csVal = L"hello";
for (int i = 0; i < 2; i++)
{
memset (&wVectorSt, 0x00, sizeof(TEST_VECTORST));
ptrList = NULL;
wVectorSt.nVal = i + 1;
int j = 0;
while (j<10)
{
if (ptrList == NULL)
{
ptrTemp = (TEST_STRUCT*)calloc(1,sizeof(TEST_STRUCT));
_tcscpy(ptrTemp->_tchTemp, csVal);
ptrTemp->dwTemp = 10+j;
ptrTemp->i64Temp = 100+j;
ptrTemp->Next = NULL;
ptrCurrent = ptrList = ptrTemp;
}
else
{
ptrTemp = (TEST_STRUCT*)calloc(1,sizeof(TEST_STRUCT));
_tcscpy(ptrTemp->_tchTemp, csVal);
ptrTemp->dwTemp = 10+j;
ptrTemp->i64Temp = 100+j;
ptrTemp->Next = NULL;
ptrCurrent->Next = ptrTemp;
ptrCurrent = ptrTemp;
}
j ++;
}
memcpy(&wVectorSt.m_ListInVector, ptrList, sizeof(TEST_STRUCT));
m_StVector.push_back (wVectorSt);
}
for (int i = 0; i<m_StVector.size(); i++)
{
ptrList = m_StVector[i].m_ListInVector.Next;
while (ptrList != NULL)
{
ptrTemp = ptrList;
ptrList = ptrList->Next;
free(ptrTemp);
ptrTemp = NULL;
}
}
All the nodes become free but one that is very first node in the list does not free.
if i take start to free from first node like- ptrList = &m_StVector[i].m_ListInVector;
then it throws exception immidiately and does not free any node...
Please give me some idea...
Thanks
modified on Tuesday, June 21, 2011 4:17 AM
|
|
|
|
|
Please use the 'code block' button to format properly your code.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
You did not dynamically allocate the first member, so you cannot dynamically free it either.
More to the point: when you push_back() a struct object into that vector of yours, the vector will allocate the required memory and copy the struct you passed into that memory location. The vector is thus responsible for the releaese of that storage, not you. Since you didn't allocate the vector on the heap, it will be automatically deallocated when the variable leaves the scope. But if you defined it globally, then you might want to enforce this earlier, by calling
m_StVector.clear() .
|
|
|
|
|
thanks for the response.
Yes i have declared m_StVector in App class. So i need to clear it.
but even i do m_StVector.clear() then still i see those memory leaks that are due to the first node of the link list...
well I am still stuck how to free those.
|
|
|
|
|
I just noticed that of course you need to free the strings within your struct, and that is something your vector will not take care of.
The best way would be to define both a constructor and destructor for your struct, and use new/delete instead of calloc/free. In the destructor you can then just take care of the string:
struct TestStruct {
TestStruct() : tchTemp(0) {}
~TestStruct() { delete [] tchTemp; }
void set(const TCHAR* stringvalue) {
delete [] tchTemp;
if (stringvalue != 0) {
tchTemp = new TCHAR[_tcslen(stringvalue)+1];
_tcscpy(stringvalue, tchTemp);
}
else {
tchTemp = 0;
}
}
TCHAR* tchTemp;
};
void test() {
std::vector<TestStruct> struct_vect;
struct_vect.clear();
}
Note that in order to actually deconstruct (i. e. call the constructor of) the TestStruct objects, you must call delete , not free , on any dynamically allocated objects, and thus you must call new to allocate it. Also you must use new to allocate your strings if you want to use delete to free them. I added a set function to simplify the process of assigning this string properly, using new .
Of course, if you don't want to change your struct definition, you can also loop over the element vectory and free() tchTemp from your first nodes manually.
|
|
|
|
|
Why are you using your own list, cannot you use the STL one?
Why are you using C allocation functions?
What is wVectorSt ?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Madan Chauhan wrote: All the nodes become free but one that is very first node in the list does not free.
How are you verifying this?
Madan Chauhan wrote: Please give me some idea...
Just a tad bit of cleanup:
void main( void )
{
for (int i = 0; i < 2; i++)
{
TEST_STRUCT *ptrCurrent;
TEST_VECTORST wVectorSt = {0};
wVectorSt.nVal = i + 1;
for (int j = 0; j < 10; j++)
{
TEST_STRUCT *ptrTemp = (TEST_STRUCT *) calloc(1, sizeof(TEST_STRUCT));
_tcscpy_s(ptrTemp->_tchTemp, _countof(ptrTemp->_tchTemp), _T(""));
ptrTemp->dwTemp = 10 + j;
ptrTemp->i64Temp = 100 + j;
ptrTemp->Next = NULL;
if (wVectorSt.m_ListInVector == NULL)
wVectorSt.m_ListInVector = ptrTemp;
else
ptrCurrent->Next = ptrTemp;
ptrCurrent = ptrTemp;
}
m_StVector.push_back(wVectorSt);
}
for (unsigned int i = 0; i < m_StVector.size(); i++)
{
TEST_STRUCT *ptrList = m_StVector[i].m_ListInVector;
while (ptrList != NULL)
{
TEST_STRUCT *ptrTemp = ptrList;
ptrList = ptrList->Next;
free(ptrTemp);
}
}
}
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
|
|
|
|
|