CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Dec 2008
    Posts
    6

    ShellExecuteEx()

    hello,

    i keep getting errors when i run the code from
    http://www.codeguru.com/forum/showthread.php?t=302501

    i added the appropiate header files, so my code is

    Code:
    #include <windows.h>
    
    int main() {
    	SHELLEXECUTEINFO ExecuteInfo;
        
        memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
        
        ExecuteInfo.cbSize       = sizeof(ExecuteInfo);
        ExecuteInfo.fMask        = 0;                
        ExecuteInfo.hwnd         = 0;                
        ExecuteInfo.lpVerb       = "open";                      // Operation to perform
        ExecuteInfo.lpFile       = "c:\\windows\\notepad.exe";  // Application name
        ExecuteInfo.lpParameters = "c:\\example.txt";           // Additional parameters
        ExecuteInfo.lpDirectory  = 0;                           // Default directory
        ExecuteInfo.nShow        = SW_SHOW;
        ExecuteInfo.hInstApp     = 0;
        
        if(ShellExecuteEx(&ExecuteInfo) == FALSE)
          // Could not start application -> call 'GetLastError()'
    
    	return 0;
    }
    the errors i get are:
    1>------ Build started: Project: aaa, Configuration: Debug Win32 ------
    1>Compiling...
    1>aaa.cpp
    1>c:\documents and settings\z\desktop\c++\aaa\aaa\aaa.cpp(12) : error C2440: '=' : cannot convert from 'const char [5]' to 'LPCWSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>c:\documents and settings\z\desktop\c++\aaa\aaa\aaa.cpp(13) : error C2440: '=' : cannot convert from 'const char [23]' to 'LPCWSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>c:\documents and settings\z\desktop\c++\aaa\aaa\aaa.cpp(14) : error C2440: '=' : cannot convert from 'const char [15]' to 'LPCWSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>Build log was saved at "file://c:\Documents and Settings\z\Desktop\c++\aaa\aaa\Debug\BuildLog.htm"
    1>aaa - 3 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


    what is wrong with my code?
    and what do i have to do to fix it?

    im also using windows XP


    thanks for help in advance, zac

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: ShellExecuteEx()

    The problem is that you are using ANSI strings, but are compiling in UNICODE.

    Include <tchar.h> and use the _T() macro.

    For example
    Code:
    ExecuteInfo.lpFile       = _T("c:\\windows\\notepad.exe");
    Not related but you don't need to use memset anymore. Instead you can let the compiler initialize a structure.

    Code:
    SHELLEXECUTEINFO ExecuteInfo = { 0 };

  3. #3
    Join Date
    Dec 2008
    Posts
    6

    Re: ShellExecuteEx()

    [SOLVED]

    thanks for the help =)

  4. #4
    Join Date
    Aug 2010
    Posts
    13

    Re: ShellExecuteEx()

    hi all,

    I had a weired behavior in my application. In my application there is dialog box and on which there is a link and when i clicked on that a Google page gets opened with help of shellexecuteEx Function.But when my dialogue is on desktop and when i clicked on link then It does not open.But when my dialog gets closed then Google search page gets opened. Can anyone help me???i am trying it from last four days

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: ShellExecuteEx()

    Quote Originally Posted by varun.choudhary View Post
    hi all,

    I had a weired behavior in my application. In my application there is dialog box and on which there is a link and when i clicked on that a Google page gets opened with help of shellexecuteEx Function.But when my dialogue is on desktop and when i clicked on link then It does not open.But when my dialog gets closed then Google search page gets opened. Can anyone help me???i am trying it from last four days
    If might help us if you posted some relavent code using code tags.

  6. #6
    Join Date
    Aug 2010
    Posts
    13

    Re: ShellExecuteEx()

    SHELLEXECUTEINFO ShExecInfo;
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = L"open";
    ShExecInfo.lpFile = m_bExplore ? L"explorer.exe" : L"c:\\Program Files\\Internet Explorer\\iexplore.exe";
    ShExecInfo.lpParameters = m_cstrURL;
    ShExecInfo.lpDirectory = NULL;
    ShExecInfo.nShow = SW_SHOWNORMAL;
    ShExecInfo.hInstApp = NULL;

    ShellExecuteEx(&ShExecInfo);

    if(ShExecInfo.hProcess)
    CloseHandle(ShExecInfo.hProcess);

  7. #7
    Join Date
    Aug 2010
    Posts
    13

    Re: ShellExecuteEx()

    Sry for being late.
    i am still getting the same problem.
    help me..


    SHELLEXECUTEINFO ShExecInfo;
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = L"open";
    ShExecInfo.lpFile = m_bExplore ? L"explorer.exe" : L"c:\\Program Files\\Internet Explorer\\iexplore.exe";
    ShExecInfo.lpParameters = m_cstrURL;
    ShExecInfo.lpDirectory = NULL;
    ShExecInfo.nShow = SW_SHOWNORMAL;
    ShExecInfo.hInstApp = NULL;

    ShellExecuteEx(&ShExecInfo);

    if(ShExecInfo.hProcess)
    CloseHandle(ShExecInfo.hProcess);

  8. #8
    Join Date
    Aug 2010
    Posts
    13

    Re: ShellExecuteEx()

    hi all,

    I had a weired behavior in my application. when run any exe on desktop and dialog box of my application appears on which a hyperlink is there and when clicked on hyperlink google page gets opened .But problem is that google page does not get open till i do not close the dialog.and when i close the dialog google page gets opened easily.
    And suppose if i create the shorcut of same exe on desktop.then google page gets opened easily without closing the dialog.
    Please help me.

    SHELLEXECUTEINFO ShExecInfo;
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = L"open";
    ShExecInfo.lpFile = m_bExplore ? L"explorer.exe" : L"iexplore.exe";
    ShExecInfo.lpParameters = m_cstrURL;
    ShExecInfo.lpDirectory = NULL;
    ShExecInfo.nShow = SW_SHOWNORMAL;
    ShExecInfo.hInstApp = NULL;

    ShellExecuteEx(&ShExecInfo);

    if(ShExecInfo.hProcess)
    CloseHandle(ShExecInfo.hProcess);

  9. #9
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: ShellExecuteEx()

    You better would open your own thread rather than adding to a solved question.

    Also please post your code into code blocks so that we easier can read them.

    If you compare your code with that of zaac you'll will see that you didn't initialize the SHELLEXECUTEINFO structure. As Arjay already explained it simply could be done by

    Code:
    SHELLEXECUTEINFO ShExecInfo = { 0 };
    I don't know whether it solves your issue but surely you should try it cause starting form Visual Studio would invoke the debugger which causes all memory to be initialized while starting from desktop might not do it that way.

  10. #10
    Join Date
    Aug 2010
    Posts
    13

    Re: ShellExecuteEx()

    Thanks for help.
    Actually i am new to code guru.I dont how to open a new thread thats why i am doing like this.
    Please tell me how to open a new thread.

    I had initialized structure to zero also but it does not work.
    Code is like this.

    SHELLEXECUTEINFO ShExecInfo = {0};

    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = L"open";
    ShExecInfo.lpFile = L"iexplore.exe";
    ShExecInfo.lpParameters = m_cstrURL;
    ShExecInfo.lpDirectory = NULL;
    ShExecInfo.nShow = SW_SHOWNORMAL;
    ShExecInfo.hInstApp = NULL;

    ShellExecuteEx(&ShExecInfo);

    if(ShExecInfo.hProcess)
    CloseHandle(ShExecInfo.hProcess);

  11. #11
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: ShellExecuteEx()

    Quote Originally Posted by varun.choudhary View Post
    Thanks for help.
    Actually i am new to code guru.I dont how to open a new thread thats why i am doing like this.
    Please tell me how to open a new thread.

    I had initialized structure to zero also but it does not work.
    Code is like this.

    SHELLEXECUTEINFO ShExecInfo = {0};

    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = L"open";
    ShExecInfo.lpFile = L"iexplore.exe";
    ShExecInfo.lpParameters = m_cstrURL;
    ShExecInfo.lpDirectory = NULL;
    ShExecInfo.nShow = SW_SHOWNORMAL;
    ShExecInfo.hInstApp = NULL;

    ShellExecuteEx(&ShExecInfo);

    if(ShExecInfo.hProcess)
    CloseHandle(ShExecInfo.hProcess);
    I am not sure what you mean by not working try to use GetLastError() if function get failed .Which will help you to understand the reason of your problem.

    Thanks

  12. #12
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: ShellExecuteEx()

    Quote Originally Posted by varun.choudhary View Post
    Thanks for help.
    Actually i am new to code guru.I dont how to open a new thread thats why i am doing like this.
    Please tell me how to open a new thread.
    Use all your powers & all your skills as a 'computer programmer' & try to figure it out. If you can't...well....good luck with programming is all I can say!

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: ShellExecuteEx()

    Quote Originally Posted by Martin O View Post
    Use all your powers & all your skills as a 'computer programmer' & try to figure it out. If you can't...well....good luck with programming is all I can say!
    Hint1: you can't do it from the thread page, but can from the forum page.
    Hint2: see the 'New Thread' button.

  14. #14
    Join Date
    Aug 2010
    Posts
    13

    Re: ShellExecuteEx()

    Quote Originally Posted by humptydumpty View Post
    I am not sure what you mean by not working try to use GetLastError() if function get failed .Which will help you to understand the reason of your problem.
    Thanks..
    Hi..
    I had tried GetLastError(),Shellexecuteex is executing sucessfully.

  15. #15
    Join Date
    Aug 2010
    Posts
    13

    Re: ShellExecuteEx()

    Quote Originally Posted by Arjay View Post
    Hint1: you can't do it from the thread page, but can from the forum page.
    Hint2: see the 'New Thread' button.
    Hi..
    GM :-)
    I go to forum page.But from there i am not getting the new thread button.

Page 1 of 2 12 LastLast

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