English 中文(简体)
Cann t获得两个程序,通过油轮物体分享数据,开放式碎裂法无效。
原标题:Cann t get two processes to share data via a kernel object, OpenFileMapping is null

我一直在审查Pavel Yosifovich的书籍和录像。 我试图写出一个纯粹的C方案,它制造了一种ker子,并且允许一个案文从一个过程到另一个过程。 我的代码如下,但出于某种原因,读到数据的方案总是要求开放式打字,最后错误是2,没有发现。 我跑了温和派,我的共有主题在两场会议中显示。 我并不认为这是一个安全问题,因为没有发现错误,我试图将来源改变为当地货币共同交易,但这没有改变。 我也试图在行政指挥中迅速行动,但至今没有工作。 似乎像现在这样说的是没有找到档案,所以它就成了一条路。 下面是两个方案的准则。

<Producer>/em>

#include <windows.h>
#include <stdio.h>
#include <string.h>

int main() {
    HANDLE hFileMapping;
    LPVOID lpFileMap;
    char buffer[] = "Windows System Programming is fun!";

    hFileMapping = CreateFileMapping(
        INVALID_HANDLE_VALUE,
        NULL,
        PAGE_READWRITE,
        0,
        sizeof(buffer),
        L"MySharedMemory"
        );

    if (hFileMapping == NULL) {
        printf("Error creating file mapping: %d
", GetLastError());
        return 1;
    }

    lpFileMap = MapViewOfFile(hFileMapping, FILE_MAP_WRITE, 0, 0, 0);

    if (lpFileMap == NULL) {
        printf("Error mapping view of file: %d
", GetLastError());
        CloseHandle(hFileMapping);
        return 1;
    }

    strcpy((char*)lpFileMap, buffer);
    printf("Data written to shared memory.  Press Enter to exit...
");
    getchar();
    UnmapViewOfFile(lpFileMap);
    CloseHandle(hFileMapping);
    return 0;
}

Consumer

#include <windows.h>
#include <stdio.h>

int main() {
    HANDLE hFileMapping;
    LPVOID lpFileMap;

    hFileMapping = OpenFileMapping(
        FILE_MAP_READ,
        FALSE,
        L"MySharedMemory"
        );

    if (hFileMapping == NULL) {
        printf("Error opening file mapping: %d
", GetLastError());
        getchar();
        return 1;
        
    }

    lpFileMap = MapViewOfFile(
        hFileMapping,
        FILE_MAP_READ,
        0,
        0,
        0,
        0
        );

    if (lpFileMap == NULL) {
        printf("Error mapping view of file: %d
", GetLastError());
        CloseHandle(hFileMapping);
        getchar();
        return 1;
    }

    printf("Received message: %s
", (char*)lpFileMap);
    UnmapViewOfFile(lpFileMap);
    getchar();
    CloseHandle(hFileMapping);
    return 0;
}

最新法典,但仍未奏效。

#include <windows.h>
#include <stdio.h>
#include <string.h>

int main() {
    HANDLE hFileMapping;
    LPVOID lpFileMap;
    char buffer[] = "Windows System Programming is fun!";

    hFileMapping = CreateFileMapping(
        INVALID_HANDLE_VALUE,
        NULL,
        PAGE_READWRITE,
        0,
        1 << 12,
        L"MySharedMemory"
        );

    if (hFileMapping == NULL) {
        printf("Error creating file mapping: %d
", GetLastError());
        return 1;
    }

    lpFileMap = MapViewOfFile(hFileMapping, FILE_MAP_WRITE, 0, 0, 0);

    if (lpFileMap == NULL) {
        printf("Error mapping view of file: %d
", GetLastError());
        CloseHandle(hFileMapping);
        return 1;
    }

    strcpy((char*)lpFileMap, buffer);
    printf("Data written to shared memory.  Press Enter to exit...
");
    getchar();
    UnmapViewOfFile(lpFileMap);
    CloseHandle(hFileMapping);
    return 0;
}




#include <windows.h>
#include <stdio.h>

int main() {
    HANDLE hFileMapping;
    LPVOID lpFileMap;

    //hFileMapping = OpenFileMapping(
    //  FILE_MAP_READ,
    //  FALSE,
    //  L"MySharedMemory"
    //  );

    hFileMapping = CreateFileMapping(
        INVALID_HANDLE_VALUE,
        NULL,
        PAGE_READWRITE,
        0,
        1 << 12,
        L"MySharedMemory"
        );

    if (hFileMapping == NULL) {
        printf("Error opening file mapping: %d
", GetLastError());
        getchar();
        return 1;
        
    }

    lpFileMap = MapViewOfFile(
        hFileMapping,
        FILE_MAP_READ,
        0,
        0,
        1 << 12
        );

    if (lpFileMap == NULL) {
        printf("Error mapping view of file: %d
", GetLastError());
        CloseHandle(hFileMapping);
        getchar();
        return 1;
    }

    printf("Received message: %s
", (char*)lpFileMap);
    UnmapViewOfFile(lpFileMap);
    getchar();
    CloseHandle(hFileMapping);
    return 0;
}

问题回答

Martin Rosenau所述,利用统法协会的密码功能明确解决这一问题。

The function has a Unicode variant(ending with W): CreateFileMappingW().





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签