m_hMapFile=::CreateFileMapping((HANDLE)-1,NULL,PAGE_READWRITE,0,(DWORD)(256*(sizeof(char))),_T("MyFileMap"));
if(!m_hMapFile){
AfxMessageBox(_T("不能创建共享内存文件"));
return -1;
}
m_lpMapAddress=(LPLONG)::MapViewOfFile(m_hMapFile,FILE_MAP_WRITE,0,0,0);
if(!m_lpMapAddress){
AfxMessageBox(_T("不能映射共享内存文件"));
return -1;
}
//初始化内存,全设为0
::memset(m_lpMapAddress,0,256);

解决方案 »

  1.   

    var
      m_hMapFile :Cardinal;
      m_lpMapAddress :Pointer;
    begin
      m_hMapFile :=CreateFileMapping(Self.Handle - 1, nil, PAGE_READWRITE, 0, 256 * Sizeof(Char), {这里是宽字符,你需要使用WideString}'MyFileMap');
      if Boolean(m_hMapFile) then
      begin
        ShowMessage('不能创建共享内存文件');
        result :=False;  
      end;
      m_lpMapAddress :=MapViewOfFile(m_hMapFile, FILE_MAP_WRITE, 0, 0, 0);
      if m_lpMapAddress <> nil then
      begin
        ShowMessage('不能映射共享内存文件');
        result :=False;
      end;
      GetMem(m_lpMapAddress, 0, 256);
    end;