1

When I feed _wfopen_s the exact same string, one a literal, one set at runtime, the literal succeeds, the string set at runtime returns error 2 (File or Directory not found)

When I look at "buff" and "bufftwo" in the visual studio debugger, they contain the exact same values, the only thing different are the addresses.

The order of 2 buffers does not change the outcome . It is not file, directory or drive specific.

I have tried removing const from my methods and variables, error still occurs.

wchar_t* loadFileDialog() { //https://learn.microsoft.com/en-us/windows/desktop/learnwin32/example--the-open-dialog-box

    wchar_t buffer[255];

    HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); 
    if (SUCCEEDED(hr))
    {
        IFileOpenDialog *pFileOpen;

        // Create the FileOpenDialog object.
        hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
            IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));

        if (SUCCEEDED(hr))
        {
            // Show the Open dialog box.
            hr = pFileOpen->Show(NULL);

            // Get the file name from the dialog box.
            if (SUCCEEDED(hr))
            {
                IShellItem *pItem;
                hr = pFileOpen->GetResult(&pItem);
                if (SUCCEEDED(hr))
                {
                    PWSTR pszFilePath;
                    hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);

                    // Display the file name to the user.
                    if (SUCCEEDED(hr))
                    {
                        lstrcpyW(buffer, pszFilePath); //Convert PWSTR into wchar array

                        CoTaskMemFree(pszFilePath);
                    }
                    pItem->Release();
                }
            }
            pFileOpen->Release();
        }
        CoUninitialize();
    }
    return buffer;
}
/*Loads a file into memory.*/
char* loadFile(const wchar_t* filePath) { 
    FILE *f;
    BOOL err = _wfopen_s(&f, filePath, L"rb");
    if (err == 0) {
        fseek(f, 0, SEEK_END);
        long fsize = ftell(f);
        fseek(f, 0, SEEK_SET);

        char *buff= (char *)malloc(fsize + 1);
        fread(buff, fsize, 1, f);
        fclose(f);

        buff[fsize] = 0;

        return buff;
    }
    else {
        //Outputs Err2, File not found. 
        throw std::runtime_error("error"); //Just here as a breakpoint until I make a better solution
    }
}
int main() {
    const wchar_t* bufftwo = loadFileDialog(); //Open Dialog Box
    const wchar_t* buff = L"C:\\Windows\\notepad.exe";

    if (lstrcmpW(bufftwo, buff) != 0) { //Here for testing, ensures both strings are identical.
        throw std::runtime_error("STRINGS ARE NOT THE SAME");
    }

    const char* myFile;

    myFile= loadFile(bufftwo);
    myFile= loadFile(buff);
}

I expected loadFile(bufftwo) and loadFile(buff) to return the same value, as they have identical inputs, however loadFile(bufftwo) errors, whilst loadFile(buff) does not.

Ben
  • 49
  • 2
  • 7
  • 2
    The code in `loadFileDialog` would not happen to return the address of a stack variable, would it? – Botje Jan 28 '19 at 08:30
  • The important function to show in your [mcve] would be the `loadFileDialog` function. Which unfortunately you don't. – Some programmer dude Jan 28 '19 at 08:32
  • By the way, what is the reason you use C-style file, strings using pointers and `malloc`, instead of the more natural C++ string and stream classes? If you use it then you would not have resource leaks (memory and files). – Some programmer dude Jan 28 '19 at 08:33
  • `_wfopen_s` does not return a `BOOL`. But that's not your problem. Probably what Botje said. – selbie Jan 28 '19 at 08:36
  • @Someprogrammerdude Coming from a C# background, I wanted a file.loadallbytes equivalent, I'm not really sure how a file stream works, and that method works so why not use it. – Ben Jan 28 '19 at 08:45
  • Now when you show the `loadFileDialog` function, we can say that the problem is exactly what @Botje said. The life-time of local variables ends with the function. – Some programmer dude Jan 28 '19 at 08:45
  • Except that, as I mentioned, you have resource leaks. C++ isn't garbage-collected, what you create or allocate you must explicitly destroy and free. If you want to learn a new language, please learn it properly. I recommend you [get a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read. – Some programmer dude Jan 28 '19 at 08:46
  • Also sorry, I thought that if it returned the expected value, there's no need to include it. – Ben Jan 28 '19 at 08:49

0 Answers0