Thread: struct in function

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    struct in function

    I made this script to start a program:

    Code:
    #include <iostream.h>
    #include <windows.h>
    
    using namespace std;
    
    struct exec
    {
      char path[];
      char par[];
      char dir[];
    };
    
    void execute (exec *x)
    {
    
      HINSTANCE hRet = ShellExecute(
        HWND_DESKTOP, //Parent window
        "open",       //Operation to perform
        x->path,       //Path to program
        x->par,         //Parameters
        x->dir,         //Default directory
        SW_SHOW);     //How to open
    
      if((LONG)hRet <= 32)
      {
        cout << "Wrong directory!!" << endl;
      }
    }
    
    int main()
    {
      cout << "Starting firefox..." << endl;
      exec pars = {"C:\\Program Files\\Mozilla Firefox\\firefox.exe", "", ""};
      execute(&pars);
      system("PAUSE");
    
      return 0;
    }
    No errors (Win XP, Borland), but when I execute the file, it couts:
    "Wrong directory!". What am I doing wrong??
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Couldn't tell you. Works fine for me.

    The only thing I'd say is, why use the std namespace if you're using depreciated libraries? And why not use the std::string object instead of character arrays? My compiler had problems until I changed it to strings.

    And lastly, don't call it a script, call it source code. C++ is not a scripting language.
    Last edited by SlyMaelstrom; 03-02-2006 at 01:12 PM.
    Sent from my iPadŽ

  3. #3
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    22 errors!

    I think ShellExecute() only accepts C strings?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ofcourse it does, which is why you'd want to call the c_str() function on your std::string objects.

    Code:
      HINSTANCE hRet = ShellExecute(
        HWND_DESKTOP, //Parent window
        "open",       //Operation to perform
        x->path.c_str(),       //Path to program
        x->par.c_str(),         //Parameters
        x->dir.c_str(),         //Default directory
        SW_SHOW);     //How to open
    
      if((LONG)hRet <= 32)
      {
        cout << "Wrong directory!!" << endl;
      }
    By the way, if I had to guess. I'd say your problem lies with the fact that you're casting to a LONG not a long and your compiler might not like that, but I could be wrong.
    Last edited by SlyMaelstrom; 03-02-2006 at 01:12 PM.
    Sent from my iPadŽ

  5. #5
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Thx for replying! I did it like this:

    Code:
    #include <iostream.h>
    #include <windows.h>
    
    using namespace std;
    
    struct exec
    {
      string path;
      string par;
      string dir;
    };
    
    void execute (exec *x)
    {
    
      HINSTANCE hRet = ShellExecute(
        HWND_DESKTOP, //Parent window
        "open",       //Operation to perform
        x->path.c_str(),       //Path to program
        x->par.c_str(),         //Parameters
        x->dir.c_str(),         //Default directory
        SW_SHOW);     //How to open
    
      if((long)hRet <= 32)
      {
        cout << "Wrong directory!!" << endl;
      }
    }
    
    int main()
    {
      cout << "Starting firefox..." << endl;
      exec pars;
      pars.path = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
      execute(&pars);
      system("PAUSE");
    
      return 0;
    }
    It didn't accept
    Code:
    exec pars = {"C:\\Program Files\\Mozilla Firefox\\firefox.exe", "", ""};
    so I changed it to
    Code:
    pars.path = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
    Is there a better/faster way to do that?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    If by 'faster' you mean essentially to declare and set the struct members in a single line then give it a constructor taking 3 strings, eg
    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    struct exec
    {
      string path;
      string par;
      string dir;
      
      exec(){};
      exec(const string& p,const string& a,const string& d):path(p),par(a),dir(d){}
    };
    
    
    void execute (exec *x)
    {
    
      HINSTANCE hRet = ShellExecute(
        HWND_DESKTOP, //Parent window
        "open",       //Operation to perform
        x->path.c_str(),       //Path to program
        x->par.c_str(),         //Parameters
        x->dir.c_str(),         //Default directory
        SW_SHOW);     //How to open
    
      if((long)hRet <= 32)
      {
        cout << "Wrong directory!!" << endl;
      }
    }
    
    int main()
    {
      cout << "Starting firefox..." << endl;
      exec pars("C:\\Program Files\\Mozilla Firefox\\firefox.exe","","");
      //pars.path = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
      execute(&pars);
      system("PAUSE");
    
      return 0;
    }
    And use <iostream>, not <iostream.h> as SlyMaelstrom suggested.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Can you give an explanation about

    Code:
      exec(){};
      exec(const string& p,const string& a,const string& d):path(p),par(a),dir(d){}
    I haven't examined constructors yet.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You have to include <string>. This compiles for me:
    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    using namespace std;
    
    struct exec
    {
      string path;
      string par;
      string dir;
    };
    
    void execute (exec *x)
    {
    
      HINSTANCE hRet = ShellExecute(
        HWND_DESKTOP, //Parent window
        "open",       //Operation to perform
        x->path.c_str(),       //Path to program
        x->par.c_str(),         //Parameters
        x->dir.c_str(),         //Default directory
        SW_SHOW);     //How to open
    
      if((int)hRet <= 32)
      {
        cout << "Wrong directory!!" << endl;
      }
    }
    
    int main()
    {
      cout << "Starting firefox..." << endl;
      exec pars = {"C:\\Program Files\\Mozilla Firefox\\firefox.exe", "", ""};
      execute(&pars);
      system("PAUSE");
    
      return 0;
    }
    Sent from my iPadŽ

  9. #9
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    I use Borland compiler and it gives 16 errors
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Such as? I'm thinking about modern Borland compilers and 0 of them comes to mind.
    Sent from my iPadŽ

  11. #11
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    } expected in function main()

    cannot convert char* to exec

    Declaration missing ; in function main()

    Unexpected }

    Type name expected

    Illegal initialisation
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    If I had to guess, it's because your compile is ancient and probably doesn't know std::string. This is also probably why you're still using old libraries.
    Sent from my iPadŽ

  13. #13
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by SlyMaelstrom
    I'm thinking about modern Borland compilers and 0 of them comes to mind.
    Which versions are you referring to? Bcc v5.6.4(cbuilderx; I'm fairly certain this is the same version used by bcb6, too) gives the same errors with the same code as does bcc551(the version used by Ideswa) - which is partly why I made my earlier suggestion.
    Quote Originally Posted by SlyMaelstrom
    your compile is ancient
    That's a fair point - the compiler is getting old (2001/2002), so, perhaps, a change to a more modern compiler might not be a bad idea.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> why use the std namespace if you're using depreciated libraries?
    BTW, the word is deprecated (depreciated means something different), and <iostream.h> isn't deprecated anyway, it is completely non-standard.

  15. #15
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    I'm getting errors in source codes that compile excellent in Borland!
    I have to write them all over again!!!
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM