0

How to store and retrieve the hwnd type data into a flat file and get it back into hwnd type variable ?

I am writing as follows :

HWND g_hWndApp, g_hWndApp1;
ofstream myfile;
myfile.open ("D:\\sri.txt");
myfile << g_hWndApp;
myfile.close();
int nCmdShow = 6;
ShowWindow(g_hWndApp,nCmdShow);

and reading as follows :

ifstream myReadFile;
char output[100];
myReadFile.open("D:\\sri.txt");
if (myReadFile.is_open()) {
       myReadFile >> output;
}
myReadFile.close();
g_hWndApp1 = (HWND) output;

however the values of g_hWndApp and g_hWndApp1 are not the same, where am I going wrong ?

Note : I am using kinect to control maximize and minimize of a window based on Z depth so i need to know which window is minimized ,so that to maximize the same on certain condition..

7
  • 1
    Every time you create a window, it gets a new HWND. It will never be the same as previous. Why would you want to store HWND in a file?
    – LightBulb
    Nov 27, 2011 at 1:17
  • 2
    What is your end goal? HWND is just a handle to the window, it is only useful until your program closes, so why would you write it to a file?
    – SoapBox
    Nov 27, 2011 at 1:17
  • Why don't you read the input into a HWND, mirroring the output operation?
    – Kerrek SB
    Nov 27, 2011 at 1:18
  • I am using kinect to control maximize and minimize of a window based on Z depth so i need to know which window is minimized ,so that to maximize the same on certain condition...
    – Nohsib
    Nov 27, 2011 at 1:27
  • That doesn't explain why you need to save a window handle to a file. Nov 27, 2011 at 1:41

2 Answers 2

1

You're casting a pointer to a string into an integer. That obviously won't work.

You need to just read the integer from the file directly into a variable:

int hwnd;
myReadFile >> hwnd;
g_hWndApp1 = (HWND)hwnd;

Take note that the HWND of your window will not be the same when you close your program and restart it and recreate the window. There's really no point to storing it in a file, unless you are communicating with another program or something.

1
  • I am using kinect to control maximize and minimize of a window based on Z depth so i need to know which window is minimized ,so that to maximize the same on certain condition..
    – Nohsib
    Nov 27, 2011 at 1:29
0

To store arbitrary binary data one usually does this:

ofstream myfile; 
myfile.open ("D:\\sri.txt", std::ios_base::binary); 
myfile.write(&g_hWndApp, sizeof(g_hWndApp));

...

ifstream myReadFile("D:\\sri.txt", ios_base::binary); 
if (myReadFile.is_open()) { 
       myReadFile.read(&g_hWndApp1, sizeof(g_hWndApp1));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.