这是写一个字符串信息:
int Length = strUID.GetLength();
HANDLE hmap;
hmap = ::CreateFileMapping((HANDLE)-1,NULL,PAGE_READONLY,0,Length,_T("MemoShare"));
if(hmap != NULL && GetLastError() == ERROR_ALREADY_EXISTS)
{
    CString strUIDMap = (CString)(LPCTSTR)(LPSTR)(char*)::MapViewOfFile(hmap,FILE_MAP_READ,0,0,0);

    strcpy((char*)(LPSTR)(LPCTSTR)strUIDMap, (char*)(LPSTR)(LPCTSTR)strUID);
    strTemp = "TestMemoShare";
    CWnd *pWnd=CWnd::FindWindow(NULL,strTemp); 
    pWnd->SendMessage(WM_IMGRETRIVED,0,0); //通过消息通知其他进程去读共享内存
    UnmapViewOfFile(&strUIDMap);
    CloseHandle(hmap);
}这是读取内存信息:
HANDLE hmap;
CString str="";
hmap = ::OpenFileMapping(FILE_MAP_READ, FALSE, "DCMServMemoShare");
if (hmap != NULL)
{
str = (CString)(LPCTSTR)(LPSTR)(char*)::MapViewOfFile(
hmap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
UnmapViewOfFile(&str);
CloseHandle(hmap); AfxMessageBox(str);
}else{
int nError = GetLastError();
}
读出来的str是TestMemoShare程序的路径。请高手指点。

解决方案 »

  1.   

    hmap = ::OpenFileMapping(FILE_MAP_READ, FALSE, "MemoShare");写错了
      

  2.   

    CWnd *pWnd=CWnd::FindWindow(NULL,strTemp); 
        pWnd->SendMessage(WM_IMGRETRIVED,0,0); //通过消息通知其他进程去读共享内存
        UnmapViewOfFile(&strUIDMap);
        CloseHandle(hmap);TestMemoShare有没有时间去读取共享内存?
      

  3.   

    你的程序bug太多。我刚才改了下:
    // MemoShare.cpp : Defines the entry point for the console application.
    //#include "stdafx.h"
    #include "Afxwin.h"
    #include <IOSTREAM>int main(int argc, char* argv[])
    {
    HANDLE hmap;
    hmap = ::CreateFileMapping((HANDLE)-1,NULL,PAGE_READWRITE,0,1024,_T("MemoShare"));
    if(hmap != NULL)// && GetLastError() == ERROR_ALREADY_EXISTS
    {
    char *strUIDMap = (char*)::MapViewOfFile(hmap,FILE_MAP_WRITE,0,0,0);
    char strUID[50]="hahaFAFADSF\0";
    strcpy(strUIDMap, strUID);
    Sleep(30000);
    std::cout<<"UnmapViewOfFile"<<std::endl;
    UnmapViewOfFile(&strUIDMap);
    CloseHandle(hmap);
    } return 0;
    }// TestMemoShare.cpp : Defines the entry point for the console application.
    //#include "stdafx.h"
    #include "Afxwin.h"
    #include <IOSTREAM>#define WM_IMGRETRIVED (WM_USER+1)int main(int argc, char* argv[])
    {
    HANDLE hmap;
    hmap = ::OpenFileMapping(FILE_MAP_READ, FALSE, "MemoShare");
    if (hmap != NULL)
    {
    std::cout<<"Get string"<<std::endl;
    char *str = (char*)::MapViewOfFile(
    hmap, FILE_MAP_READ, 0, 0, 0);
    std::cout<<str<<std::endl;
    UnmapViewOfFile(&str);
    CloseHandle(hmap);

    else
    {
    int nError = GetLastError();
    }

    return 0;
    }