0

So i want to make a sort of texteditor and want to use a dialogwindow to get the filepath/file from the user. Now here is the problem. I get an error in one project that i does not get in another even though i have not changed a setting that would let me conclude that there might be an issue.

#include <Windows.h>
#include <fstream>
#include <Windows.h>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <commdlg.h>

using std::cout;



void getfile() {

    OPENFILENAME NameOfFile;
    ZeroMemory(&NameOfFile, sizeof(NameOfFile));
    NameOfFile.lStructSize = sizeof(NameOfFile);   
    LPWSTR szFile{};
    NameOfFile.lpstrFile = szFile;                 
    NameOfFile.nMaxFile = sizeof(szFile);          
    NameOfFile.Flags = OFN_ALLOWMULTISELECT;
    if (GetOpenFileName(&NameOfFile))
    {
        std::cout << szFile;
    }

}
int main() {

    getfile();

}

When i do this in one project the error "Severity Code Description Project File Line Suppression state Error (active) E0513 A value of type ""LPWSTR"" cannot be assigned to an entity of type ""LPSTR"""

Picture of the working project

Picture of the working project

Here the settings of the project its not working

Here the settings of the project its not working

That is kinda understandable and i tried multiple workarounds with LPTSTR or triying .c_str() to convert but it wont work. And as i said the literally completly same code compiles and runs fine in another project

burnsi
  • 6,194
  • 13
  • 17
  • 27
morigan
  • 23
  • 9
  • It depends on the character set configured in your project. One is using single-byte characters, the other is using multi-byte. – paddy Jan 17 '23 at 09:00
  • both use single byte. still only works in one of them. Even more wierd is that now pragma at the start of the header file is apperently not defined??? – morigan Jan 17 '23 at 09:06
  • Try defining `szFile` as a `LPTSTR` instead of `LPWSTR`. – paddy Jan 17 '23 at 09:07
  • i tried that but LPTSTR does not open the dialog window – morigan Jan 17 '23 at 09:09
  • Read the Microsoft documentation for `GetOpenFileName`. I'm pretty sure you need to supply a valid buffer, not a NULL pointer. Something like `TCHAR buffer[MAX_PATH];` and then set `nMaxFile` to the buffer size. Can't really recall. Been a while. – paddy Jan 17 '23 at 09:20
  • It's not that your dialog is not displaying, you have an error in your code that would prevent it compiling. Change `std::cout << szFile;` to `std::wcout << szFile;` – user20716902 Jan 17 '23 at 09:40
  • it compiled and works correctly tho. yes i never chose a file through the dialog windows so no clue it the cout works or your wcout would be the option but the dialog window works in the code i posted above. also i didnt really wanted to cout the path as i just want to get the filepath from "NameOfFile.lpstrFile" that saves it in a variable (u can see that in the screenshots i posted here – morigan Jan 17 '23 at 09:56
  • Please show/provide the compile options for "All Options" command line, for the Configuration: Release, Platform: x64 found in Configurtion Properties->C/C++->Command Line – user20716902 Jan 17 '23 at 10:28
  • @user20716902 there are none. just "" – morigan Jan 17 '23 at 10:39
  • You are seeing because you have "Alle Konfigurationen" and "Alle Plattformen" selected, change these to "Release" and "x64" then check the command line option again – user20716902 Jan 17 '23 at 10:46
  • /permissive- /ifcOutput "x64\Release\" /GS /GL /W3 /Gy /Zc:wchar_t /Zi /Gm- /O2 /sdl /Fd"x64\Release\vc143.pdb" /Zc:inline /fp:precise /D "_CRT_SECURE_NO_WARNINGS" /errorReport:prompt /WX- /Zc:forScope /std:c17 /Gd /Oi /MD /std:c++20 /FC /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Fp"x64\Release\passt.pch" /diagnostics:column @user20716902 – morigan Jan 18 '23 at 07:16
  • There would appear to be something wrong with your config, as your screenshot appears to show that you have Unicode set for all configurations BUT it is not appearing in your command line options, to fix this you can add `/D "_UNICODE" /D "UNICODE"` to the Additional Options section. – user20716902 Jan 18 '23 at 10:11
  • could you pls add this as an complete answer so that i can close this post? I found another way to fix the problem with using multiybte characters i will post as an aswer aswell – morigan Jan 18 '23 at 12:36
  • @morigan That's great that you've got it working, I've updated with a complete answer. – user20716902 Jan 19 '23 at 12:03

2 Answers2

0
LPWSTR FILE{};
OPENFILENAME NameOfFile;
ZeroMemory(&NameOfFile, sizeof(NameOfFile));
NameOfFile.lStructSize = sizeof(NameOfFile);
TCHAR szFile[MAX_PATH];
NameOfFile.lpstrFile = szFile;
NameOfFile.nMaxFile = sizeof(szFile);
NameOfFile.Flags = OFN_ALLOWMULTISELECT;
if (GetOpenFileName(&NameOfFile))
{
    FILE = (LPWSTR)szFile;
}

This code will open a dialog window with the purpose to "open" a file. It will save the name of the file and the path int the variable FILE.

The answer to my problem was the variable szFile that can't be an LPWSTR but needed to be for the dialog window to work. In the end the process does this (I skipped some steps):

  1. I make a "buffer" variable szfile (array with 260 characters bc MAX_PATH resolves to that)

  2. It makes a sort of reference (similar to a pointer) that points to the array [that is the "NameOfFile.lpstrFile"]

  3. Its calls the function in the if statement and opens the dialog window

  4. After choosing the function "GetOpenFileName" saves the file name and the path in szFile (the TCHAR - array) and converts that one back to LPWSTR [the code writen in the if statement]

  5. You got your stuff and can continue

thanks for everyone's help

Narish
  • 607
  • 4
  • 18
morigan
  • 23
  • 9
0

If this project was being compiled as UNICODE we would expect to see that NameOfFile.lpstrFile would be of type LPWSTR.

However, as seen by the error message it is actually of type LPSTR.

The error E0513 A value of type ""LPWSTR"" cannot be assigned to an entity of type ""LPSTR""" is therefore highlighting that the two types don’t match.

Interestingly the screenshot does show that Unicode has been selected for All builds.

Whilst the exact cause of this issue is not known, the solution here is to add /D "_UNICODE" /D "UNICODE" to the Configuration Properties->C/C++->Additional Options section.

This will ensure that all builds are in fact being compiled using the Unicode option.

user20716902
  • 861
  • 1
  • 14