WinAPI线程无法写入内存映射文件

时间:2016-11-13 09:29:23

标签: winapi

我正在尝试创建2个线程,一个写入映射的内存位置,另一个读取此位置 到目前为止,我已经成功地为它们创建了线程和附加函数,但我的主要问题是当第一个进程想要写入映射文件时,我抛出错误值6,意思是INVALID_HANDLE_EXCEPTION,但是我无法弄清楚为什么会这样做,因为我已经制作了另一个可以以相同方式写入映射位置的程序。这是我的代码,错误在write_in_mapped函数抛出:

#include <iostream>
#include <string>
#include <Windows.h>

using namespace std;

HANDLE hMutex = CreateMutex(NULL, false, NULL);

int write_in_mapped(HANDLE memoryFile) {
    cout << memoryFile << endl;

    WaitForSingleObject(hMutex, INFINITE);

    string message = "";

    for (int i = 0; i < 200; i++) {
    int a = rand() % 10000;
    int b = a * 2;

    message.append(to_string(a)).append(",").append(to_string(b)).append(";");
}

HANDLE mapFile = MapViewOfFile(memoryFile, FILE_MAP_ALL_ACCESS, 0, 0, 1024);
if (mapFile == INVALID_HANDLE_VALUE) {
    cout << "Unable to map shared memory 1 - error value:" << GetLastError() << endl;
    CloseHandle(memoryFile);
    system("pause");
    return FALSE;
}

if (size(message)) {
    cout << message << endl;
    CopyMemory(mapFile, message.c_str(), strlen(message.c_str()) * sizeof(TCHAR));
    cout << "Succesfully loaded numbers!" << endl;
}
else {
    cout << "Couldn't create numbers!" << endl;
    CloseHandle(mapFile);
    CloseHandle(memoryFile);
    return FALSE;
}

    ReleaseMutex(hMutex);

    cout << "Done write_to_mapped" << endl;

    return 0;
}


int read_from_mapped(TCHAR name[]) {

cout << name << endl;

WaitForSingleObject(hMutex, INFINITE);

HANDLE openFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, name);
if (openFile == INVALID_HANDLE_VALUE) {
    cout << "Error at opening mapped memory 2 - error value:" << GetLastError() << endl;
    system("pause");
    return FALSE;
}

LPTSTR mapFile = (LPTSTR)MapViewOfFile(openFile, FILE_MAP_ALL_ACCESS, 0, 0, 1024);
if (mapFile == NULL) {
    cout << "Error at mapping memory 3 file - error value:" << GetLastError() << endl;
    CloseHandle(openFile);
    system("pause");
    return FALSE;
}

    string result = string(mapFile).c_str();

    cout << result << endl;

    ReleaseMutex(hMutex);

    return 0;
}

int main() {

    HANDLE process_1, process_2, memoryFile;
    TCHAR name[] = TEXT("Global\\MyFileMappingObject");

    memoryFile = CreateFileMapping(0, 0, PAGE_READWRITE, 0, 1024, name);

    if (memoryFile == INVALID_HANDLE_VALUE) {
        cout << "Error at opening mapped memory area - " << GetLastError() << endl;

    system("pause");
    return FALSE;
}

    process_1 = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)write_in_mapped, &memoryFile, NULL, NULL);
    WaitForSingleObject(process_1, INFINITE);

    process_2 = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)read_from_mapped, &name, NULL, NULL);
    WaitForSingleObject(process_2, INFINITE);

   CloseHandle(memoryFile);
   CloseHandle(hMutex);

   system("pause");
   return 0;
}

1 个答案:

答案 0 :(得分:2)

您的代码包含大量错误。所有代码都是一个大错误。 CreateFileMappingOpenFileMapping在出错时返回0,但不是INVALID_HANDLE_VALUE&memoryFile&name作为CreateThread的参数?!?何时需要memoryFilename没有& 你从CloseHandle(memoryFile);int write_in_mapped(HANDLE memoryFile)拨打int main() - 所以请尝试2次关闭memoryFile,但是write_in_mapped中的句柄无效,因为使用&memoryFile - 所以得到了INVALID_HANDLE_EXCEPTION中的CloseHandle HANDLE mapFile = MapViewOfFile - 这是什么?!?等等,

<强>恶梦