CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2007
    Posts
    44

    ShellExecute() problem

    Hi,
    Some who read this, may remember that I wrote a program called remote. It was a remote control that executed different apps when the user presses its associated button.

    Now I wish to add user defined buttons, with the button name and its action, loaded from a text file. I find myself hit with yet another stumbling block..

    Basically, I can not find a way to execute the app named in the text file. For example, in the text file:

    Paint, C:\\WINDOWS\\system32\\mspaint.exe

    Before the comma is the button name, after is the app to run.

    My code is as follows:

    Code:
    case ID_USER1:  //  First user button
    {
      if (ButtonInfo[0][1] != "null") // null is no action
      {
        PlaySound("C:\\WINDOWS\\Media\\Windows Information Bar.wav",NULL,SND_FILENAME);
        
        string temp = ButtonInfo[0][1];
    
        MessageBox (hwnd, temp.c_str(), "Button properties", MB_OK);
    
        ShellExecute(NULL, "open", temp.c_str(), NULL, NULL, SW_SHOWNORMAL);
      }
    }
    break;
    ButtonInfo is a string array and after transferring to a single string, Im trying to convert to the char type in ShellExecute. MessageBox is there for debugging and displays the correct data.

    It just don’t work… If I change ShellExecute to this:

    Code:
    ShellExecute(NULL, "open", “C:\\WINDOWS\\system32\\mspaint.exe”, NULL, NULL, SW_SHOWNORMAL);
    Paint will now load, but obviously is not able to be changed by the text file. Can anyone tell me what Im doing wrong???

    Thanks in advance…

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: ShellExecute() problem

    Well, according to the MDN for SHellExecute, if the call fails, the return value will be less then 32, and be one of a number of error codes. What is the error?

    Viggy

  3. #3
    Join Date
    Jan 2007
    Posts
    38

    Re: ShellExecute() problem

    Well I don't know anything about string objects because I always just use TCHAR's, but I just googled for an explanation of it, and it appears that the c_str member function returns a pointer to a char array. Now, correct me if I'm wrong, but if you are compiling for UNICODE, doesn't that string need to be converted to widechars first?

    I think the mbstowcs function could help you with that.

    Good luck.
    I am a beginning C++ Win32 programmer with LOTS of questions. Hopefully matching answers can be found here...

  4. #4
    Join Date
    Feb 2007
    Posts
    44

    Re: ShellExecute() problem

    Firstly, may I thank both Viggy & Jehjoa for your replies.

    Im not sure more conversion is required Jehoja. Though I have a few c++ books (not api related), the only resource for info that I have is Win32.hlp (from the win32 source destruction kit). It gives descriptions for both MessageBox and ShellExecute.

    The text parameter for messagebox is of type LPCTSTR and I know this works with c_str(), as it gives the correct data from the file. The file parameter of shellexecute is also of type LPCTSTR. Surely, if messagebox recieves the correct data, shell execute should as well.

    Viggy said:
    Well, according to the MDN for SHellExecute, if the call fails, the return value will be less then 32, and be one of a number of error codes.
    Win32.help says the same as msdn. I did the following:
    Code:
    HINSTANCE MyInst;
    MyInst = ShellExecute(NULL, "open", temp.c_str(), NULL, NULL, SW_SHOWNORMAL);
    ok, MyInst should now contain the return value and msdn says cast to an int. however, I can find no info on how to actually cast HINSTANCE to int or anything else. I wish I had more than just a referance guide to learn api from, but at the moment I can not afford any more books, even if I knew a good one...

    Thanks for trying to help anyway

  5. #5
    Join Date
    Jan 2007
    Posts
    38

    Re: ShellExecute() problem

    Quote Originally Posted by Y0rkieP
    Surely, if messagebox recieves the correct data, shell execute should as well.
    Hmm you're right about the LPCTSTR thing. Sorry for sidetracking you there.

    however, I can find no info on how to actually cast HINSTANCE to int or anything else.
    Code:
    HINSTANCE MyInst = NULL;   // always initialize your variables
    int iInstance = 0;
    MyInst = ShellExecute(NULL, "open", temp.c_str(), NULL, NULL, SW_SHOWNORMAL);
    iInstance = (int)MyInst;
    That should do it. Google for "c++ typecasting" if you want to learn more. You don't necessarily have to cast it to an int to check it's return value though... Just use your debugger and set a breakpoint.

    I wish I had more than just a referance guide to learn api from, but at the moment I can not afford any more books, even if I knew a good one...
    Don't forget you have internet access. Lots of good tutorials and stuff around. There's even an ebook version of Charles Petzolds Programming Windows floating around on the web. And many, many books on C++. Just use Google.
    I am a beginning C++ Win32 programmer with LOTS of questions. Hopefully matching answers can be found here...

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640

    Re: ShellExecute() problem

    Code:
    HINSTANCE hInst = ShellExecute(NULL, "open", temp.c_str(), NULL, NULL, SW_SHOWNORMAL);
    if (reinterpret_cast<int>(hInst) < 32)
    {
       // ERROR!!!!!
    }
    Viggy

  7. #7
    Join Date
    Feb 2007
    Posts
    44

    Re: ShellExecute() problem

    I can now safely say that ShellExecute cannot find the app that is passed to the filename parameter. the error code is 2 & matching this with ERROR_FILE_NOT_FOUND verifys the fact. After loads of websearch, Im still no closer to a solution though.

    say I wanted the first user button to be notepad. My first line of the text file for button config would be:

    Notepad, c:\windows\notepad.exe

    When I run my app, the button displays "Notepad", so far, so good. When I click the button it starts off ok, a messagebox stating that the button properties is "c:\windows\notepad.exe", then things go bad and the same error (file not found).

    ive tryed using "\\" instead of "\" in the text file and tryed "/" too, all to no avail. As the filename parameter is of LPCTSTR, a long pointer to c type string, I also tryed:

    Code:
    const char* lpFile = temp.c_str();
    hMyInst = ShellExecute(NULL, "open", lpFile, NULL, NULL, SW_SHOWNORMAL);
    nothing seems to work. Maybe shellexecute is the wrong function to use. just seems that the deeper I dig, the more I burry myself...

  8. #8
    Join Date
    Feb 2002
    Posts
    4,640

    Re: ShellExecute() problem

    Have you stepped through this code in the debugger, and verified that 'temp' contains the correct path?

    Viggy

  9. #9
    Join Date
    Jan 2007
    Posts
    38

    Re: ShellExecute() problem

    Hmm very strange. The only other thing I can think of is that maybe something is wrong with the function that gets the file path from the text file... Maybe you start reading directly after the comma so the string actually holds
    Code:
    " c:\windows\notepad.exe"
    (empty space before "c"). That space might be hard to notice in a messagebox.

    If
    Code:
    ShellExecute(NULL, "open", “C:\\WINDOWS\\system32\\mspaint.exe”, NULL, NULL, SW_SHOWNORMAL);
    works then there's nothing wrong with ShellExecute.
    I am a beginning C++ Win32 programmer with LOTS of questions. Hopefully matching answers can be found here...

  10. #10
    Join Date
    Feb 2007
    Posts
    44

    Re: ShellExecute() problem

    Yep, I had completely forgot to read the extra space between the comma and the filename. Havving recently writing similar code, I should have remembered this. Its caused me so much bother, by not reading/dumping the extra space. You was spot on the mark there Jehjoa. Thanky for that, much appreciated..

    Just thought it would be nice to say thanks to those that tryed to help...

  11. #11
    Join Date
    Jan 2007
    Posts
    38

    Re: ShellExecute() problem

    Great that you got it working.
    I am a beginning C++ Win32 programmer with LOTS of questions. Hopefully matching answers can be found here...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured