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.