Create a text file descriptor. HANDLE file = CreateFile(...); successfully working with him. So ... I can send this " file " to cout , and I will see in the console something like this: 00000000000002C0 . This is all clear.

But I can’t just as easily save this value in any variable, for example, in a string or an array of char characters. How to implement it?

Who can say about this?

    2 answers 2

     //1 вариант char str[64]; //про запас sprintf(str, "%p", file); //2 вариант std::stringstream ss; ss << static_cast<void*>(file); std::string value = ss.str(); 
    • thank you very much) this is what i was looking for! ) - Sanya
    • To thank the author of the answer, vote for the answer or mark it as a decision. - Mikalai Ramanovich

    You can search the header files and find there

     typedef void* HANDLE; 

    Those. HANDLE is nothing more than void* .

    You can use C ++ tools and display this type:

     cout << typeid(HANDLE).name() << endl; 

    You will see

     void * 

    Those. your HANDLE is just a pointer, and you can do everything with a regular pointer with it. Just knowing what and why :)

    You can store it in a string by translating it into the appropriate string representation. Type

     char s[20]; HANDLE h; sprintf(s,"%p",h); 

    Or even just as the original byte sequence:

     unsigned char s[4]; HANDLE h; memcpy(s,&h,sizeof(h)); 

    In variables of type int , of course, if the dimensions allow (in the 64-bit world, void* - 8 bytes) - using explicit type conversion reinterpret_cast

     HANDLE f; int g = reinterpret_cast<int>(f); 

    Only you need to understand what you are doing and why. Otherwise, you can get in trouble at the loving adventure point ...

    • What cout << typeid(HANDLE).name() << endl; depends on the implementation. gcc, for example , will give out Pv - yrHeTaTeJlb
    • @yrHeTaTeJlb Well, in principle, yes, you are right. I wrote about VC ++. - Harry
    • thanks for the info) - Sanya
    • Instead of int you should use special types: intptr_t or uintptr_t from <cstdint> . Since the pointer may well not fit in a normal int . - αλεχολυτ
    • @alexolut Yes, but the customer :) wants to post, if I understood him correctly, generally in arbitrary variables ... - Harry