#include <iostream>
#include<windows.h>
using namespace std;
int main()
{
char szBuffer[] = "Shine";
HANDLE MappingHandle = NULL;
PVOID BaseAddress = NULL;
MappingHandle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 0x1000, L"HelloWorld");
if (MappingHandle == NULL)
{
return 0;
}
if (ERROR_FILE_INVALID == GetLastError())
printf("ERROR_FILE_INVALID");
else if (ERROR_INVALID_HANDLE == GetLastError())
printf("ERROR_INVALID_HANDLE");
else if (ERROR_ALREADY_EXISTS == GetLastError())
printf("ERROR_ALREADY_EXISTS");
else
printf("Success");
BaseAddress = MapViewOfFile(MappingHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
memcpy(BaseAddress, szBuffer, strlen(szBuffer) + 1);
Sleep(5000);
UnmapViewOfFile(BaseAddress);
CloseHandle(MappingHandle);
system("pause");
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<windows.h>
#include<QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
int Share_Memory()
{
int nRetCode = 0;
while(1){
HANDLE hMapping = OpenFileMapping(FILE_MAP_ALL_ACCESS,NULL,L"HelloWorld");
if (hMapping)
{
qDebug()<<"Success";
LPVOID lpBase = MapViewOfFile(hMapping,FILE_MAP_READ|FILE_MAP_WRITE,0,0,0);
char szBuffer[20] = {0};
strcpy(szBuffer,(char*)lpBase);
qDebug()<<szBuffer;
UnmapViewOfFile(lpBase);
CloseHandle(hMapping);
nRetCode=1;
break;
}
else
{
qDebug()<<"OpenMapping Error";
}
}
return nRetCode;
}
void MainWindow::on_pushButton_clicked()
{
Share_Memory();
}