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

 

posted @ 2022-10-07 21:59  FredGrit  阅读(73)  评论(0编辑  收藏  举报