Michael
Michael

Reputation: 459

Winapi ShellExecuteEx does not opening the file

I'm new in WinApi, I want to open text file in my D: drive, but this code doesn't work. When I run the app, I have an error: "Windows can't found file 'lobalizarion\Sorting\sortdefaults.nls'".. And in the console I have message: "File was not found", but I have it on my drive.

In my code I'm using snippet from: How to get hWnd of window opened by ShellExecuteEx.. hProcess? to get HWND of created HINSTANCE.

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <vector>

std::wstring stringToLPCTSTR(const std::string& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
}

struct ProcessWindowsInfo
{
    DWORD ProcessID;
    std::vector<HWND> Windows;

    ProcessWindowsInfo(DWORD const AProcessID) : ProcessID(AProcessID) {}
};

BOOL __stdcall EnumProcessWindowsProc(HWND hwnd, LPARAM lParam)
{
    ProcessWindowsInfo *Info = reinterpret_cast<ProcessWindowsInfo*>(lParam);
    DWORD WindowProcessID;

    GetWindowThreadProcessId(hwnd, &WindowProcessID);

    if (WindowProcessID == Info->ProcessID)
        Info->Windows.push_back(hwnd);

    return true;
}

int main()
{
    SHELLEXECUTEINFOW sei;

    sei.cbSize = sizeof(SHELLEXECUTEINFO);
    sei.fMask = SEE_MASK_NOCLOSEPROCESS;
    sei.hwnd = NULL;
    sei.lpVerb = stringToLPCTSTR("open").c_str();
    sei.lpFile = stringToLPCTSTR("D:\\file.txt").c_str();
    sei.lpParameters = NULL;
    sei.lpDirectory = stringToLPCTSTR("D:\\").c_str();;
    sei.nShow = SW_SHOWNORMAL;
    sei.hInstApp = NULL;

    auto retval = ShellExecuteEx(&sei);

    if (retval == 0) //check errors
    {
        if ((int)sei.hInstApp == SE_ERR_FNF) 
            std::cerr << "File was not found\n";
        else 
            std::cerr << "Unexpected error occured\n";
    }
    else
    {
        WaitForInputIdle(sei.hProcess, INFINITE);

        ProcessWindowsInfo info(GetProcessId(sei.hProcess));

        EnumWindows((WNDENUMPROC)EnumProcessWindowsProc, reinterpret_cast<LPARAM>(&info));
    }

    std::cin.get();

    return 0;
}

Upvotes: 0

Views: 543

Answers (1)

Karsten Koop
Karsten Koop

Reputation: 2524

The std::wstring returned by stringToLPCTSTR() are temporaries which you use to assign the LPCTSTR members of the struct SHELLEXECUTEINFOW. This means the pointer returned by the respective calls to c_str() may no longer be valid at the time you call ShellExecuteEx(). You need to find a different solution for this, e.g. by using local variables for the std::wstring, but as long as you are only using string literals, you can just do

sei.lpVerb = L"open";
sei.lpFile = L"D:\\file.txt";
sei.lpDirectory = L"D:\\";

Upvotes: 4

Related Questions