流程中0到255之间的唯一编号

时间:2015-08-26 22:44:37

标签: c++ c++11

是否有任何方法可以生成介于0到255之间的唯一编号。此编号在整个过程中必须是唯一的。因此,如果我的应用程序有多个实例,则每个实例将被赋予0到255之间的唯一编号。当然,我的应用程序可以拥有的最大实例数为256。

根据Brad的建议,使用文件始终是一个选项。但我想知道是否有 windows API这样做?

我能用C ++做些什么吗?

1 个答案:

答案 0 :(得分:1)

正如其他人所说,您可以使用共享内存在进程之间交换数据。

由于您只对0-255之间的唯一ID感兴趣,并且我认为在使用它完成一个进程后可以重用任何给定ID(因此256个并发进程,对吗?)。如果是这样,您可以在共享内存中存储256个元素的布尔数组,当新进程加载DLL时,它可以在数组中查找可用的插槽,将其设置为true,并使用插槽索引作为其唯一ID 。卸载DLL时,让它将阵列槽重置为false以供以后的进程使用。您可以通过CreateMutex()使用命名的互斥锁来跨进程同步对阵列的访问。

这样的事情:

#pragma data_seg (".myseg")
bool AppsInUse[256] = {0};
#pragma data_seg()

HANDLE hMutex = NULL;
int UniqueID = -1;

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
    switch( fdwReason ) 
    { 
        case DLL_PROCESS_ATTACH:
            DisableThreadLibraryCalls(hinstDLL);
            hMutex = CreateMutex(NULL, FALSE, TEXT("MyMutex"));
            if (hMutex == NULL)
                return FALSE;
            if (WaitForSingleObject(hMutex, 5000) == WAIT_OBJECT_0)
            {
                for (int i = 0; i < 256; ++i)
                {
                    if (!AppsInUse[i])
                    {
                        AppsInUse[i] = true;
                        UniqueID = i;
                        break;
                    }
                }
                ReleaseMutex(hMutex);
            }
            if (UniqueID == -1)
                return FALSE;
            break;

        case DLL_PROCESS_DETACH:
            if (UniqueID != -1)
            {
                if (WaitForSingleObject(hMutex, 1000) == WAIT_OBJECT_0)
                {
                    AppsInUse[UniqueID] = false;
                    ReleaseMutex(hMutex);
                }
                UniqueID = -1;
            }
            if (hMutex != NULL)
            {
                CloseHandle(hMutex);
                hMutex = NULL;
            }
            break;
    }
    return TRUE;
}

如果您的编译器不支持#pragma data_seg,您可以使用CreateFileMapping()MapViewOfFile()动态分配共享数组:

HANDLE hMapping = NULL;
bool* AppsInUse = NULL;
HANDLE hMutex = NULL;
int UniqueID = -1;

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
    switch( fdwReason ) 
    { 
        case DLL_PROCESS_ATTACH:
        {
            DisableThreadLibraryCalls(hinstDLL);
            hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(bool) * 256, TEXT("MyArray"));
            if (hMapping == NULL)
                return FALSE;
            bool bDoInit = (GetLastError() != ERROR_ALREADY_EXISTS);
            AppsInUse = (bool*) MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, sizeof(bool) * 256);
            if (AppsInUse == NULL)
                return FALSE;
            if (bDoInit) ZeroMemory(AppsInUse, sizeof(bool) * 256);
            hMutex = CreateMutex(NULL, FALSE, TEXT("MyMutex"));
            if (hMutex == NULL)
                return FALSE;
            if (WaitForSingleObject(hMutex, 5000) == WAIT_OBJECT_0)
            {
                for (int i = 0; i < 256; ++i)
                {
                    if (!AppsInUse[i])
                    {
                        AppsInUse[i] = true;
                        UniqueID = i;
                        break;
                    }
                }
                ReleaseMutex(hMutex);
            }
            if (UniqueID == -1)
                return FALSE;
            break;
        }

        case DLL_PROCESS_DETACH:
            if (UniqueID != -1)
            {
                if (WaitForSingleObject(hMutex, 1000) == WAIT_OBJECT_0)
                {
                    AppsInUse[UniqueID] = false;
                    ReleaseMutex(hMutex);
                }
                UniqueID = -1;
            }
            if (AppsInUse != NULL)
            {
                UnmapViewOfFile(AppsInUse);
                AppsInUse = NULL;
            }
            if (hMapping != NULL)
            {
                CloseHandle(hMapping);
                hMapping = NULL;
            }
            if (hMutex != NULL)
            {
                CloseHandle(hMutex);
                hMutex = NULL;
            }
            break;
    }
    return TRUE;
}