rsk82
rsk82

Reputation: 29397

why lpstrFile is returning address in memory instead of char array in OPENFILENAMEW struct?

This code should compile without errors you should link -lcomdlg32 for dialogs. The program returns: 0x22fcd8 or 68 if I use pointer. And should (I think) return the name of the file user types in dialog box.

#include <windows.h>
#include <iostream>

int main() {
  wchar_t szFileName[MAX_PATH] = {0};
  OPENFILENAMEW ofn;
  ZeroMemory(&ofn, sizeof(ofn));
  ofn.lStructSize = sizeof(OPENFILENAME);
  ofn.nMaxFile = MAX_PATH;
  ofn.lpstrFile = szFileName;
  GetSaveFileNameW(&ofn);

  using namespace std;
  cout << szFileName << endl;
  cout << *szFileName << endl; // also a number not a string
}

Upvotes: 0

Views: 457

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 597051

cout does not support wchar_t or wchar_t*, but it does support int and void*. The compiler ends up converting the wchar_t into an int, and degrading the wchar_t* to a void*. That is why you see cout print the numeric value of the wchar_t and the memory address of the wchar_t*.

Use wcout instead. It supports wchar_t and wchar_t*, so it can print the actual data instead:

wcout << szFileName << endl;
wcout << *szFileName << endl;

Upvotes: 2

Puppy
Puppy

Reputation: 146968

You have to use wcout if you want to output an array of wchar_t as a null-terminated wide string.

Upvotes: 6

Related Questions