OpenFileMapping failed in Windows Server but Successfully in Windows 11

jizheng zhang 0 Reputation points
2023-11-10T09:16:46.78+00:00

I try to transfer file between C++ and Python using MMAP, python write data and C++ read data. The same code ok in win11 but failed in windows server 2016

--------------------Python Code:


C++ Code:

	#include <string>
	#include <exception>
	#include <iostream>
	#include <windows.h>
	#include <strsafe.h>
	
	
	static const long long HEAD_LENGTH = 4;
    long long SHARE_MEMORY_FILE_SIZE_BYTES = (1LL*1024*1024);
	std::string m_FileName;
	HANDLE dumpFd = 0L;
	HANDLE sharedFd = 0L;
	char * lp_base = 0L;
	int mode = 0;  //0, null, 1: open/create for write, 2. open for read!
	int m_Pos = 0L, m_ReadPos = 0L; //内存读写的位置	
	
	//打开共享内存,主要是用于读操作,
	// para1: pFileName, 共享内存
	// para2: buffer_size 共享内存大小
	// return: True/False
	bool OpenMMap(const char* pFileName, long long buffer_size)
	{
		bool rtn = false;
		release();
		m_FileName = pFileName;
		SHARE_MEMORY_FILE_SIZE_BYTES = buffer_size;

		//#2. map mem file
		sharedFd = OpenFileMapping(FILE_MAP_ALL_ACCESS,NULL,pFileName);		

		//sharedFd = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READONLY \
		//	, 0, SHARE_MEMORY_FILE_SIZE_BYTES, pFileName);

		if (sharedFd == 0L) {
			LPVOID lpMsgBuf;
			LPVOID lpDisplayBuf;
			DWORD  dw = GetLastError();
			FormatMessage(
				FORMAT_MESSAGE_ALLOCATE_BUFFER |
				FORMAT_MESSAGE_FROM_SYSTEM |
				FORMAT_MESSAGE_IGNORE_INSERTS,
				NULL,
				dw,
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
				(LPTSTR)&lpMsgBuf,
				0, NULL);
			lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
				(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)"OpenFileMapping") + 40) * sizeof(TCHAR));
			StringCchPrintf((LPTSTR)lpDisplayBuf,
				LocalSize(lpDisplayBuf) / sizeof(TCHAR),
				TEXT("%s failed with error %d: %s"),
				"OpenFileMapping", dw, lpMsgBuf);

			printf("OpenFileMapping Fail with ERROR %s", lpDisplayBuf);
			LocalFree(lpMsgBuf);
			LocalFree(lpDisplayBuf);
			return false;
		}
		//#3. Get MemFile base pointer.
		lp_base = (char*)MapViewOfFile(
			sharedFd,  // Handle of the map object
			FILE_MAP_READ,  // Read and write access
			0,                    // High-order DWORD of the file offset
			0,                    // Low-order DWORD of the file offset
			SHARE_MEMORY_FILE_SIZE_BYTES);           // The number of bytes to map to view

		if (lp_base == 0L) {
			return false;
		}
		mode = 2;
		return true;
	}
		//读入 字符信息
	// read
	// - parameter:
	// 1. buffer_ptr: 读入的信息缓存区
	// 2. buffer_size:缓存区大小
	// - return:
	// int :读入数据大小,读入成功 > 0 ,
	int read(char* buffer_ptr, int buffer_size)
	{
		if (lp_base == 0L)
			return 0;
		int len = 0;
		m_Pos = 0;
		memcpy(&len, lp_base, sizeof(len));
		if (len <= 0)
			return 0;
		m_Pos = len;
		if (len >= buffer_size)
		{
			len = buffer_size-1;
		}		
		memcpy(buffer_ptr, lp_base + HEAD_LENGTH , len);
		buffer_ptr[len] = '\0';
		return len;		
	}
	long SHARE_MEMORY_FILE_SIZE_BYTES = 1024;
	OpenMMap("_MMAP_TEST_TAG_", SHARE_MEMORY_FILE_SIZE_BYTES);
	char tmpStr[256];
	int len = mmap.read(tmpStr, sizeof(tmpStr));

The Error in windows server is:

OpenFileMapping failed with error 2: 系统找不到指定的文件。

Also, such mmap content can be read out by Python in windows server 2016. shows that MMAP FIle Maping exists

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,327 questions
{count} votes

2 answers

Sort by: Most helpful
  1. RLWA32 37,131 Reputation points
    2023-11-10T10:37:24.6666667+00:00

    On Server 2016 does the code that creates the shared memory run in the same session as the code that attempts to read it and fails?

    0 comments No comments

  2. jizheng zhang 0 Reputation points
    2023-11-11T00:23:02.5633333+00:00

    thanks to RLWA32, the reason is that Python and C++ in different Sessions, I visit the windows server through different remote desktop, when I run them in the same session, code works. thanks