Results 1 to 9 of 9

Thread: system(command)

  1. #1

    Thread Starter
    Addicted Member bataeu's Avatar
    Join Date
    Nov 2000
    Location
    Walla Walla, Washington
    Posts
    144

    system(command)

    I am confused

    if i type "set moo=moosvalue" at the command prompt it changes the value of moo to moosvalue. However if I use "system("set moo=moosvalue");" it doesnt change moo. Does anyone know why? and if so could you please tell me.

    I have been playing with the system(command) thing and this is the only thing I cant get to work. if I do "system("set m");" it shows me the variables starting with m. ect.

    Thanks for any help.
    C, C++ and none of that MCF crap either!

  2. #2
    jim mcnamara
    Guest
    When you call system() you are callingexec() - make a new child process. The environment variables you set, in this case "moo' only last for the duration of the child process, they are not available to any other process.

    If you need to set environment variables for the duration of your current process
    Code:
    long i = SetEnvironmentVariable( _T "MOO", _T "MOOSVILLE");
    This will last as long as your .EXE is running.

  3. #3

    Thread Starter
    Addicted Member bataeu's Avatar
    Join Date
    Nov 2000
    Location
    Walla Walla, Washington
    Posts
    144
    Okay that works but I need to place one into the environment variables that doesnt go away when my program ends. Anyone know how to do that.

    PS Thanks for the help so far
    C, C++ and none of that MCF crap either!

  4. #4

    Thread Starter
    Addicted Member bataeu's Avatar
    Join Date
    Nov 2000
    Location
    Walla Walla, Washington
    Posts
    144
    hmm evendently this is not a good way to go about setting a variable from C for dos. Here I will give you a little broader idea of what I am doing maybe there is a better way .

    I want to call my exe from a batch file. My exe will then prompt the user for information and then I had hoped place it in a variable for the batch file to use. Is this possible? It wouldnt have to be placed in a variable so long as there was some way for the batch file to use it. I would be okay if it disapeared after the batch file stoped. Thanks for any help
    C, C++ and none of that MCF crap either!

  5. #5
    jim mcnamara
    Guest
    Do you know about %1 %2 ,etc. ?


    Batchfile named myfile.bat:
    c:\Myexe %1 %2

    Call the batchfile from ShellExecute:
    Code:
    #include <windows.h>
    char *parameters = "c:\\myfile.bat parameter1 parameter2";
    long i = ShellExecute (this,
            NULL,
            "COMMAND.EXE",  // change for NT to CMD.EXE
              parameters, 
             "C:\\", 
              SW_SHOWNORMAL);

  6. #6

    Thread Starter
    Addicted Member bataeu's Avatar
    Join Date
    Nov 2000
    Location
    Walla Walla, Washington
    Posts
    144
    that is actualy what i am doing right now but i was hoping to call the exe from a batch file and either have it return a value(which i dont think is posible in a batch file) or to have it set the value of a variable that i pass to my app as a parameter. I was thinking maybe I could make it a global enviornment variable(or whatever it is called) but i dont really know how to go about it.
    C, C++ and none of that MCF crap either!

  7. #7
    jim mcnamara
    Guest
    I finally get what you want. Sorry.

    What you may want to use is an atom. It is a string stored in kernel memory, that is referenced by an integer. Any process can read the value.

    Of course, you could also just store the intermediate result in a file.

    GlobalAddAtom
    GlobalDeleteAtom
    GlobalFindAtom
    GlobalGetAtomName

    Go into MSDN and look up these api calls. A file is much easier to implement. Fair warning.

  8. #8

    Thread Starter
    Addicted Member bataeu's Avatar
    Join Date
    Nov 2000
    Location
    Walla Walla, Washington
    Posts
    144
    hmm how would i read an atom from a batch file? Let me give you the whole picture on what i am trying to do.

    At my work all network loging in is done in dos batch files. I know not the most up to date and secure way to do it but it is old and I didnt start it. Basicly we have been using strings.com(a pc magazine app) to ask the user for input in dos. Well we just got a laptop for the office and it came with windows 2k. In windows 2k strings.com doesnt work so we need to come up with a new way of doing things. Unfortunatly we still are going to do them in DOS for the time being but what i would like to do is just code an app that could replace the funtionality of strings.com but work in 2k and 98. With strings.com you pass it a variable name and the prompt you want it to display and then it places the information the user types into the variable you give it. My boss has already said he doesnt want to use a file as the medium for transfering the information. Once the information is in the variable the batch file logs the user in and then fills the variable with junk so no one can get the password out of the enviornment(again i know not very secure). But that is what I have to work with. Thanks for any more help and thanks for everything so far jim.
    C, C++ and none of that MCF crap either!

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It is possible to return integer values from an exe like this:
    int main()
    {
    return rand() % 5; // random number form 0 to 4
    }

    batch file:
    @echo off
    myexe
    if errorlevel 4 goto handle4
    if errorlevel 3 goto handle3
    ...
    handle4:
    dosomething
    goto end
    handle3:
    dosomethingelse
    goto end
    ...
    end:
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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