ShareMemory

Code

Copied!

//
// Copyright (c) 2021 - 2025 Advanced Micro Devices, Inc. All rights reserved.
//
//-------------------------------------------------------------------------------------------------


#include <Windows.h>
#include "GlobalDefs.h"

static HANDLE g_SharedFile = NULL;
static PVOID  g_SharedBuffer = NULL;

// Create shared memory
BOOL ShmCreate (WCHAR* shmFileName, ULONG sharedFilesize)
{
    SECURITY_DESCRIPTOR sd;
    SECURITY_ATTRIBUTES sa;

    // N.B: After setting this permission, the general application does not have administrator privileges
    // In order to start to open mapping g_SharedFile Otherwise Access Denny
    InitializeSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION);
    SetSecurityDescriptorDacl (&sd, TRUE, (PACL)NULL, FALSE);

    sa.nLength = sizeof (SECURITY_ATTRIBUTES);
    sa.bInheritHandle = TRUE;
    sa.lpSecurityDescriptor = &sd;

    HANDLE g_SharedFile = CreateFileMappingW (INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE, 0, sharedFilesize, shmFileName);
    if (g_SharedFile == NULL)
    {
        XTrace (L"ADLX Call Service: ShareMem::Create: CreateFileMappingW, error: %d\\n", GetLastError());
        return FALSE;
    }

    LPVOID g_SharedBuffer = MapViewOfFile (g_SharedFile, FILE_MAP_ALL_ACCESS, 0, 0, sharedFilesize);
    if (g_SharedBuffer == NULL)
    {
        XTrace (L"ADLX Call Service: ShareMem::Create: MapViewOfFile, error: %d\\n", GetLastError ());
        return FALSE;
    }

    XTrace (L"ADLX Call Service: ShareMem::Create: OK\\n");

    return TRUE;
}

// Destory shared memory
VOID ShmDestory ()
{
    if (g_SharedBuffer)
    {
        UnmapViewOfFile (g_SharedBuffer);
        g_SharedBuffer = NULL;
    }

    if (g_SharedFile)
    {
        CloseHandle (g_SharedFile);
        g_SharedFile = NULL;
    }
}

// Write data to shared memory
BOOL ShmWriteBuffer (WCHAR* shmFileName, PVOID buffer, ULONG size)
{
    HANDLE shmFile = OpenFileMappingW (FILE_MAP_ALL_ACCESS, FALSE, shmFileName);
    if (shmFile)
    {
        LPVOID g_SharedBuffer = MapViewOfFile (shmFile, FILE_MAP_ALL_ACCESS, 0, 0, size);
        if (g_SharedBuffer)
        {
            memcpy_s (g_SharedBuffer, size, buffer, size);
            return TRUE;
        }
    }
    return FALSE;
}

// Read data from shared memory
BOOL ShmReadBuffer (WCHAR* shmFileName, PVOID buffer, ULONG size)
{
    SECURITY_DESCRIPTOR sd;
    SECURITY_ATTRIBUTES sa;

    InitializeSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION);
    SetSecurityDescriptorDacl (&sd, TRUE, (PACL)NULL, FALSE);

    sa.nLength = sizeof (SECURITY_ATTRIBUTES);
    sa.bInheritHandle = TRUE;
    sa.lpSecurityDescriptor = &sd;

    BOOL result = FALSE;
    HANDLE g_SharedFile = OpenFileMappingW (FILE_MAP_ALL_ACCESS, FALSE, shmFileName);
    if (g_SharedFile)
    {
        g_SharedBuffer = MapViewOfFile (g_SharedFile, FILE_MAP_ALL_ACCESS, 0, 0, size);
        if (g_SharedBuffer)
        {
            memcpy_s (buffer, size, g_SharedBuffer, size);
            result = TRUE;
        }
    }

    return result;
}

Related pages

  • Visit the Adlx product page for download links and more information.

Looking for more documentation on GPUOpen?

AMD GPUOpen software blogs

Our handy software release blogs will help you make good use of our tools, SDKs, and effects, as well as sharing the latest features with new releases.

GPUOpen Manuals

Don’t miss our manual documentation! And if slide decks are what you’re after, you’ll find 100+ of our finest presentations here.

AMD GPUOpen Performance Guides

The home of great performance and optimization advice for AMD RDNA™ 2 GPUs, AMD Ryzen™ CPUs, and so much more.

Getting started: AMD GPUOpen software

New or fairly new to AMD’s tools, libraries, and effects? This is the best place to get started on GPUOpen!

AMD GPUOpen Getting Started Development and Performance

Looking for tips on getting started with developing and/or optimizing your game, whether on AMD hardware or generally? We’ve got you covered!

AMD GPUOpen Technical blogs

Browse our technical blogs, and find valuable advice on developing with AMD hardware, ray tracing, Vulkan®, DirectX®, Unreal Engine, and lots more.

Find out more about our software!

AMD GPUOpen Effects - AMD FidelityFX technologies

Create wonder. No black boxes. Meet the AMD FidelityFX SDK!

AMD GPUOpen Samples

Browse all our useful samples. Perfect for when you’re needing to get started, want to integrate one of our libraries, and much more.

AMD GPUOpen developer SDKs

Discover what our SDK technologies can offer you. Query hardware or software, manage memory, create rendering applications or machine learning, and much more!

AMD GPUOpen Developer Tools

Analyze, Optimize, Profile, Benchmark. We provide you with the developer tools you need to make sure your game is the best it can be!