سؤال

i want my other c++ program to be run from another file so i am using shell execute. Mu code is :

#pragma comment(lib,"shell32.lib")  
#include "windows.h"
#include<Shellapi.h>

#include<stdio.h>
#include<iostream>
using namespace std;

class spwan{
public:
    //char szPath[] = "";
    void run(char path[]);
};

void spwan::run(char szPath[]){
HINSTANCE ShellExecute(HWND,  "open", szPath,"","",SW_SHOW);    
    cout<<"program executed";
}

int main ()
{
 spwan s;
 s.run("path to the file");
}

But i am having problem like expected a type specifier with "open" and i am not able to define the path using szPath. Anyhelp.

errors more specifically are: its giving me error for line : HINSTANCE ShellExecute(HWND, "open", szPath,"","",SW_SHOW); as syntax error : 'string'

when i am giving path like this :- C:\Users\saira\Documents\Visual Studio 2010\Projects\phase_1_solver\Debug\phase_1_solver.exe its giving errors like : warning C4129: 's' : unrecognized character escape sequence warning C4129: 'D' : unrecognized character escape sequence

هل كانت مفيدة؟

المحلول

In your code you have:

HINSTANCE ShellExecute(HWND,  "open", szPath,"","",SW_SHOW);

That is a declaration of a function. I presume that you actually meant to call the function:

HINSTANCE retval = ShellExecute(HWND,  "open", szPath,"","",SW_SHOW);

Now, that won't compile either. Since HWND is a type. I think you need:

HINSTANCE retval = ShellExecute(0, "open", szPath, NULL, NULL, SW_SHOW);

What's more, there's no need to actually specify a verb. The default verb for a path will suffice.

HINSTANCE retval = ShellExecute(0, NULL, szPath, NULL, NULL, SW_SHOW);

And it sounds as though you are passing strings like this:

s.run("C:\Users\saira\...\phase_1_solver.exe");

That's not good because the backslash is used as the escape character in C++. So you need to escape it:

s.run("C:\\Users\\saira\\...\\phase_1_solver.exe");

If you are not going to test the return value then you can simply write:

ShellExecute(0, NULL, szPath, NULL, NULL, SW_SHOW);

If you do want to check for errors on return from ShellExecute, then ShellExecute is a bad function to call. Its error handling is particularly weak. Use ShellExecuteEx instead. Raymond Chen discusses the error handling of ShellExecute in Why does ShellExecute return SE_ERR_ACCESSDENIED for nearly everything?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top