ShellExecute() and string problem

Hi all!

I have a weird problem that may involve pointer problems or incorrect termination, but I have almost given up now!!!

I use a Windows API32 function called "ShellExecute()"
to launch any application from within my application.

The application name is stored in a charbuffer just after the string "Execute ".

The idea is that by sending an email to my application, a user can launch any application he desires. The email generates the charbuffer.

Code:
char *pathpointer;
static char Temp[TEMP_SIZE];

///search string to see if an Execute email recieved?
if (strstr(Temp,"Execute") !=NULL)
{
////get a pointer to the location of the app to execute
pathpointer=strstr(Temp,"Execute")+8;
hInst = ShellExecute(0,"open",pathpointer,0,0,SW_SHOW);
}





If i harcode the pathpointer and write: "c:\winnt\notepad.exe"
it works fine, but If i store this string in the "Temp" buffer
the string is found but ShelExecute doesnt find the executable.....

Strangely if, I write a web adress like www.volvo.se , then ShellExecute launches the web browser as it is suppoes to.
It seems to have something to do with either termination or the path.


But I have tried to copy from the pointer to a new char buff, terminate it and then use it with ShellExecute, I have also tried the various path functions in API 32, but to no avail. And, as I said, when I hardcode the string, it works........

Anyones suggestions is extremly appreciated!!!

Henrik












Comments

  • [b][red]This message was edited by AsmGuru62 at 2003-5-21 7:5:11[/red][/b][hr]
    : Hi all!
    :
    : I have a weird problem that may involve pointer problems or incorrect termination, but I have almost given up now!!!
    :
    : I use a Windows API32 function called "ShellExecute()"
    : to launch any application from within my application.
    :
    : The application name is stored in a charbuffer just after the string "Execute ".
    :
    : The idea is that by sending an email to my application, a user can launch any application he desires. The email generates the charbuffer.
    :
    : Code:
    : char *pathpointer;
    : static char Temp[TEMP_SIZE];
    :
    : ///search string to see if an Execute email recieved?
    : if (strstr(Temp,"Execute") !=NULL)
    : {
    : ////get a pointer to the location of the app to execute
    : pathpointer=strstr(Temp,"Execute")+8;
    : hInst = ShellExecute(0,"open",pathpointer,0,0,SW_SHOW);
    : }
    :
    :
    :
    :
    :
    : If i harcode the pathpointer and write: "c:\winnt\notepad.exe"
    : it works fine, but If i store this string in the "Temp" buffer
    : the string is found but ShelExecute doesnt find the executable.....
    :
    : Strangely if, I write a web adress like www.volvo.se , then ShellExecute launches the web browser as it is suppoes to.
    : It seems to have something to do with either termination or the path.
    :
    :
    : But I have tried to copy from the pointer to a new char buff, terminate it and then use it with ShellExecute, I have also tried the various path functions in API 32, but to no avail. And, as I said, when I hardcode the string, it works........
    :
    : Anyones suggestions is extremly appreciated!!!
    :
    : Henrik
    :
    [blue]Check 'hInst' returned and you will see the reason, maybe system denied access to that file or permissions were violated?... What is the value of that return code?[/blue]


  • SephirothSephiroth Fayetteville, NC, USA
    : : pathpointer=strstr(Temp,"Execute")+8;
    That is kind of sloppy. You should check out scanf(). Assuming your execution line is "Execute C:BlahBlah.exe", it would work like this:
    [code]
    char Command[256];
    scanf(Temp, "Execute %s", &Command);
    ShellExecute(Command);
    [/code]
    fscanf(FILE*, *pattern, ...);
    scanf(*buffer, *pattern, ...);
    And there are so many other variants of "scanf" it isn't even funny. Check them out, it may fix your problem, and at the very least it can optimize and clean up your code.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

  • Hi!

    Thanks for the tips!
    But as soon as I run scanf the entire application locks up and doesnt do anything.....
    And strstr() works fine, it finds the "Execute" string but scanf does not. strange!

    Henrik
  • : Hi!
    :
    : Thanks for the tips!
    : But as soon as I run scanf the entire application locks up and doesnt do anything.....
    : And strstr() works fine, it finds the "Execute" string but scanf does not. strange!
    :
    : Henrik
    :


    Greetings
    Just thinking out loud, but hardcoding a path like that sounds risky. MS keeps changing the default Windows directory's name from WinNT (NT/2000) to Windows (Win9x/XP), & not everybody has Windows installed on C: (multiboot configs). So why not use [b]GetWindowsDirectory[/b] to fill the buffer then add notepad.exe to it, and send that to ShellExecute().

    Just a Thought
    Stoic Joker
  • SephirothSephiroth Fayetteville, NC, USA
    If you are not using a pointer or reference with scanf, that is why it locks up.
    [code]
    char String[32];
    char Message[128];
    int Number;

    memset(Message, '', sizeof(Message));
    memset(String, '', sizeof(String));
    sprintf(Message, "This is a string followed by a number!=10");

    //Proper way!
    scanf(Message, "%s=%i", &String, &Number);

    //Will do something off the wall
    scanf(Message, "%s=%i", String, Number);
    [/code]
    See? This function needs a memory address for the variable, not the variable itself.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories