hSharedMemory* OpenSharedMemory(char* MapFileName, char* mutex, int size) { hSharedMemory* handle = malloc(sizeof(hSharedMemory)); handle->hFile = NULL; handle->hFileView = NULL; handle->hFileMutex = NULL; // Try to open File Mapping... handle->hFile = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, MapFileName); // ... ok it might not exist, so create try to create it... if (handle->hFile == NULL) handle->hFile = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, size & 0xFFFF0000, size & 0xFFFF, MapFileName); // ... if create fails we are lost... if (handle->hFile == 0) { CloseSharedMemory(handle); return NULL; } handle->hFileView = MapViewOfFile(handle->hFile, FILE_MAP_ALL_ACCESS, 0, 0, size); if (handle->hFileView == NULL) { CloseSharedMemory(handle); return NULL; } handle->hFileMutex = CreateMutexA(NULL, FALSE, mutex); if (handle->hFileMutex == NULL) { CloseSharedMemory(handle); return NULL; } return handle; }
static bool GetLargestEntrySize (APRGlobalStorage *storage_p, unsigned int *size_p) { bool success_flag = false; unsigned int *largest_size_p = (unsigned int *) OpenSharedMemory (storage_p -> ags_largest_entry_memory_id, 0); if (largest_size_p) { *size_p = *largest_size_p; CloseSharedMemory (largest_size_p); success_flag = true; } else { PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to open shared memory %d for largest size entry", storage_p -> ags_largest_entry_memory_id); } return success_flag; }
static DWORD StoreConnectionInfo( IN LPCWSTR LocalName, IN LPCWSTR ConnectionName, IN USHORT ConnectionNameLength, IN LPNETRESOURCE lpNetResource) { DWORD status; HANDLE hMutex, hMemory; PNFS41NP_SHARED_MEMORY pSharedMemory; PNFS41NP_NETRESOURCE pNfs41NetResource; INT Index; BOOLEAN FreeEntryFound = FALSE; #ifdef __REACTOS__ status = OpenSharedMemory(&hMutex, &hMemory, (PVOID *)&pSharedMemory); #else status = OpenSharedMemory(&hMutex, &hMemory, &(PVOID)pSharedMemory); #endif if (status) goto out; DbgP((TEXT("StoreConnectionInfo: NextIndex %d, NumResources %d\n"), pSharedMemory->NextAvailableIndex, pSharedMemory->NumberOfResourcesInUse)); for (Index = 0; Index < pSharedMemory->NextAvailableIndex; Index++) { if (!pSharedMemory->NetResources[Index].InUse) { FreeEntryFound = TRUE; DbgP((TEXT("Reusing existing index %d\n"), Index)); break; } } if (!FreeEntryFound) { if (pSharedMemory->NextAvailableIndex >= NFS41NP_MAX_DEVICES) { status = WN_NO_MORE_DEVICES; goto out_close; } Index = pSharedMemory->NextAvailableIndex++; DbgP((TEXT("Using new index %d\n"), Index)); } pSharedMemory->NumberOfResourcesInUse += 1; pNfs41NetResource = &pSharedMemory->NetResources[Index]; pNfs41NetResource->InUse = TRUE; pNfs41NetResource->dwScope = lpNetResource->dwScope; pNfs41NetResource->dwType = lpNetResource->dwType; pNfs41NetResource->dwDisplayType = lpNetResource->dwDisplayType; pNfs41NetResource->dwUsage = RESOURCEUSAGE_CONNECTABLE; pNfs41NetResource->LocalNameLength = (USHORT)(wcslen(LocalName) + 1) * sizeof(WCHAR); pNfs41NetResource->RemoteNameLength = (USHORT)(wcslen(lpNetResource->lpRemoteName) + 1) * sizeof(WCHAR); pNfs41NetResource->ConnectionNameLength = ConnectionNameLength; StringCchCopy(pNfs41NetResource->LocalName, pNfs41NetResource->LocalNameLength, LocalName); StringCchCopy(pNfs41NetResource->RemoteName, pNfs41NetResource->RemoteNameLength, lpNetResource->lpRemoteName); StringCchCopy(pNfs41NetResource->ConnectionName, pNfs41NetResource->ConnectionNameLength, ConnectionName); // TODO: copy mount options -cbodley out_close: #ifdef __REACTOS__ CloseSharedMemory(&hMutex, &hMemory, (PVOID *)&pSharedMemory); #else CloseSharedMemory(&hMutex, &hMemory, &(PVOID)pSharedMemory); #endif out: return status; }