Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 什么';这是C++;Guid.NewGuid()的版本?_C++_Windows_Winapi_Guid - Fatal编程技术网

C++ 什么';这是C++;Guid.NewGuid()的版本?

C++ 什么';这是C++;Guid.NewGuid()的版本?,c++,windows,winapi,guid,C++,Windows,Winapi,Guid,我需要在非托管Windows C++项目中创建 GUID < /代码>。我习惯了C#,在这里我会使用Guid.NewGuid()。Win32 API中的(非托管窗口)C++版本是什么?有完全相同的效果。但是,您需要传递将接收生成值的变量的地址: UUID newId; UuidCreate( &newId ); 我相信Guid.NewGuid()只是在.NET运行时内映射到它。我想这就是您想要的。例如: GUID gidReference; HRESULT hCreateGuid =

我需要在非托管Windows C++项目中创建<代码> GUID < /代码>。我习惯了C#,在这里我会使用
Guid.NewGuid()
。Win32 API中的(非托管窗口)C++版本是什么?

有完全相同的效果。但是,您需要传递将接收生成值的变量的地址:

UUID newId;
UuidCreate( &newId );
我相信Guid.NewGuid()只是在.NET运行时内映射到它。

我想这就是您想要的。例如:

GUID gidReference;
HRESULT hCreateGuid = CoCreateGuid( &gidReference );

以下是获取生成的GUID的结果字符串值的代码片段:

// For UUID
#include <Rpc.h>
#pragma comment(lib, "Rpcrt4.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    // Create a new uuid
    UUID uuid;
    RPC_STATUS ret_val = ::UuidCreate(&uuid);

    if (ret_val == RPC_S_OK)
    {
        // convert UUID to LPWSTR
        WCHAR* wszUuid = NULL;
        ::UuidToStringW(&uuid, (RPC_WSTR*)&wszUuid);
        if (wszUuid != NULL)
        {
            //TODO: do something with wszUuid

            // free up the allocated string
            ::RpcStringFreeW((RPC_WSTR*)&wszUuid);
            wszUuid = NULL;
        }
        else
        {
            //TODO: uh oh, couldn't convert the GUID to string (a result of not enough free memory)
        }
    }
    else
    {
        //TODO: uh oh, couldn't create the GUID, handle this however you need to
    }

    return 0;
}
//用于UUID
#包括
#pragma注释(lib,“Rpcrt4.lib”)
int _tmain(int argc,_TCHAR*argv[]
{
//创建一个新的uuid
UUID-UUID;
RPC_STATUS ret_val=::UuidCreate(&uuid);
如果(ret_val==RPC_S_OK)
{
//将UUID转换为LPWSTR
WCHAR*wszUuid=NULL;
::UuidToStringW(&uuid,(RPC_WSTR*)&wszUuid);
如果(wszUuid!=NULL)
{
//TODO:使用wszUuid做些什么
//释放分配的字符串
::RpcStringFreeW((rpcwstr*)&wszUuid);
wszUuid=NULL;
}
其他的
{
//TODO:噢,无法将GUID转换为字符串(由于可用内存不足)
}
}
其他的
{
//TODO:噢,无法创建GUID,请按需要处理此问题
}
返回0;
}
API参考:


在windows中生成新的guid并以字符串形式获取结果值

#include <string>
#include <sstream>
#include <iostream>
#include <windows.h>
#include <iomanip>

int main()
{
    GUID guid;
    CoCreateGuid(&guid);

    std::ostringstream os;
    os << std::hex << std::setw(8) << std::setfill('0') << guid.Data1;
    os << '-';
    os << std::hex << std::setw(4) << std::setfill('0') << guid.Data2;
    os << '-';
    os << std::hex << std::setw(4) << std::setfill('0') << guid.Data3;
    os << '-';
    os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[0]);
    os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[1]);
    os << '-';
    os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[2]);
    os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[3]);
    os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[4]);
    os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[5]);
    os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[6]);
    os << std::hex << std::setw(2) << std::setfill('0') << static_cast<short>(guid.Data4[7]);

    std::string s(os.str());
    std::cout << s << std::endl;
}
该文件指出了其实施方式:

这是一个方便的静态方法,您可以调用它来获取新的。该方法包装对Windows函数的调用

因此,与
Guid.NewGuid()
相对应的本机等价物是
CoCreateGuide()


调用,以生成GUID。不过,两个API调用略有不同:虽然
UuidCreate
返回一个UUID,该UUID保证对创建它的计算机是唯一的,但
CoCreateGuid
生成一个绝对唯一的GUID

如果您需要决定使用哪种API,以下是文档中的相关部分

:

出于安全原因,通常需要防止网络上的以太网地址在公司或组织外部可用。UuidCreate函数生成一个无法跟踪到生成它的计算机的以太网地址的。它也不能与在同一台计算机上创建的其他UUID关联

:

CoCreateGuid函数调用RPC函数,该函数创建一个GUID,一个全局唯一的128位整数。当您需要一个绝对唯一的数字作为分布式环境中的持久标识符时,请使用CoCreateGuid


在新的WinRT api中,您可以使用
WinRT::Windows::Foundation::GuidHelper::CreateNewGuid()
,它返回
WinRT::guid
类型的结构。您可以稍后将其传递到
winrt::tohstring
以获取
winrt::hstring
类型的结构,然后将其传递到
winrt::to_string
以获取
std::string
类型。

以获取Windows上std::string中的guid

#include <Windows.h>
#include <array>

std::string getGUID()
{
    std::string result{};
    GUID guid;

    if (S_OK == CoCreateGuid(&guid))
    {
        std::array<char, 36> buffer{};   //32 characters of guid + 4 '-' in-between

        snprintf(buffer.data(), buffer.size(), "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
                guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
        result = std::string(buffer.data());
    }

    return result;
} 
#包括
#包括
std::string getGUID()
{
std::字符串结果{};
GUID;
如果(S_OK==CoCreateGuid(&guid))
{
std::数组缓冲区{};//32个字符,guid+4'-'之间
snprintf(buffer.data(),buffer.size(),“{%08x-%04x-%04x-%02x%02x-%02x%02x%02x}”,
guid.Data1、guid.Data2、guid.Data3、guid.Data4[0]、guid.Data4[1]、guid.Data4[2]、guid.Data4[3]、guid.Data4[4]、guid.Data4[5]、guid.Data4[6]、guid.Data4[7];
结果=std::string(buffer.data());
}
返回结果;
} 

这构成了UUID结构,而不是GUID结构。它们是相同的,只是不同的typedef名称!是的,但是guid只声称是全球唯一的;合同规定的UUID具有普遍唯一性!因此,如果您希望您的代码在多个星球上运行,那么显然您应该使用后者!(不幸的是,如果/当量子计算流行起来,许多宇宙的解释证明是正确的,即使是后者也可能被证明是不够的…)事实并非如此。调用(这反过来调用UuidCreate,可能不止一次)。区别在于,CoCreateGuid在唯一性方面提供了更强的保证,因此区别很重要。@IInspectable听起来不错。为什么不添加另一个解释所有细节的答案呢?它在内部调用UuidCreate()。@sharptooth:这只是事实的一半。虽然
CoCreateGuid
确实调用了
UuidCreate
,但它在唯一性方面提供了更强的保证:“当您需要一个绝对唯一的数字,您将使用它作为分布式环境中的持久标识符时使用COCCREATEGUID。”它是否需要对CONNEXIALIZE()的一个预先调用?C++没有定义一个。您必须依赖于第三方库或特定于平台的操作系统。您可能希望了解Boost UUID
#include <Windows.h>
#include <array>

std::string getGUID()
{
    std::string result{};
    GUID guid;

    if (S_OK == CoCreateGuid(&guid))
    {
        std::array<char, 36> buffer{};   //32 characters of guid + 4 '-' in-between

        snprintf(buffer.data(), buffer.size(), "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
                guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
        result = std::string(buffer.data());
    }

    return result;
}