Windows c++ generate uuid via rpcrt4.lib and free RPC_CSTR via RpcStringFreeA

// ConsoleApplication3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#pragma comment(lib, "rpcrt4.lib") 
#include <windows.h>
#include <rpcdce.h>
#include <chrono>
#include <ctime>
#include <fstream>
#include <iostream> 
#include <sstream> 

using namespace std;

string getUuid()
{
    UUID newUUID;
    RPC_CSTR uuidStr;
    string uuidValue;
    if (UuidCreate(&newUUID) != RPC_S_OK)
    {
        cout << "Couldn't create uuid " << GetLastError() << endl;
    }

    if (UuidToStringA(&newUUID, &uuidStr) != RPC_S_OK)
    {
        cout << "Couldn't convert uuid to string " << GetLastError() << endl;
    }
    uuidValue = (char*)uuidStr;
    RpcStringFreeA(&uuidStr);
    return uuidValue;
}

void logFile(string fileName, int loops)
{
    fstream wFile(fileName, ios::app);
    if (!wFile.is_open())
    {
        cout << "Create or open " << fileName << " failed!" << endl;
        return;
    }

    uint64_t num = 0;
    stringstream ss;
    chrono::time_point<chrono::high_resolution_clock> startTime, endTime;
    for (int i = 0; i < loops; i++)
    {
        startTime = chrono::high_resolution_clock::now();
        ss = stringstream();
        for (int j = 0; j < 1000000; j++)
        { 
            ss << ++num << "," << getUuid() << endl;
        }
        wFile << ss.str();
        ss.str();
        if (!wFile.good())
        {
            cout << num << ",write failed!" << endl;
            break;
        } 
        endTime = chrono::high_resolution_clock::now();
        cout << num << ","
            << chrono::duration_cast<chrono::seconds>(endTime - startTime).count() << " seconds,"
            << chrono::duration_cast<chrono::milliseconds>(endTime - startTime).count() << " milliseconds,"
            << chrono::duration_cast<chrono::milliseconds>(endTime - startTime).count() << " microseconds,"
            << chrono::duration_cast<chrono::nanoseconds>(endTime - startTime).count() << " nanoseconds!!!" << endl << endl;
    }

    wFile.close();
    cout << num << ",finished in " << __FUNCTION__ << endl;
}

int main()
{
    logFile("Log.txt", 10000000);
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

 

原文链接: https://www.cnblogs.com/Fred1987/p/16767292.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    Windows c++ generate uuid via rpcrt4.lib and free RPC_CSTR via RpcStringFreeA

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401315

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:12
下一篇 2023年4月19日 上午9:12

相关推荐