CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Dos SET command

  1. #1
    Join Date
    Apr 2001
    Posts
    488

    Dos SET command

    I was wondering if there is a command in C++ that will do the same as a dos set command? I need to set some global variables for an exe that I am spawning, is there a way to do this short of running a batch file?


  2. #2
    Join Date
    May 1999
    Location
    Germany
    Posts
    106

    Re: Dos SET command

    Hi,

    if you use CreateProcess to spawn your EXE you can fill the lpEnvironment-parameter with the needed global settings.

    Bye
    Peter


  3. #3
    Join Date
    Apr 2001
    Posts
    488

    Re: Dos SET command

    MSDN says that lpEnvironment is not supported. Can I just put set commands like SET TESTER=test,SET TESTER2=test2... in that parameter all in a string or how can I set 5 or 6 variables at once when I launch CreateProcess? How can I get more info on what I can and can't put in lpEnvironment parameter. Do you have some code that you could post, or a url you could send me? Thanks for the reply



  4. #4
    Join Date
    May 1999
    Location
    Germany
    Posts
    106

    Re: Dos SET command

    Hi,

    my MSDN doesn't say, this parameter would not be supported. Maybe you have an older version?

    <MSDN>
    lpEnvironment
    Pointer to an environment block for the new process. If this parameter is NULL, the new process uses the environment of the calling process.
    An environment block consists of a null-terminated block of null-terminated strings. Each string is in the form:

    name=value

    Because the equal sign is used as a separator, it must not be used in the name of an environment variable.

    If an application provides an environment block, rather than passing NULL for this parameter, the current directory information of the system drives is not automatically propagated to the new process. For a discussion of this situation and how to handle it, see the following Remarks section.

    An environment block can contain Unicode or ANSI characters. If the environment block pointed to by lpEnvironment contains Unicode characters, the dwCreationFlags field's CREATE_UNICODE_ENVIRONMENT flag will be set. If the block contains ANSI characters, that flag will be clear.

    Note that an ANSI environment block is terminated by two zero bytes: one for the last string, one more to terminate the block. A Unicode environment block is terminated by four zero bytes: two for the last string, two more to terminate the block.
    </MSDN>

    Here is a very simple example.

    This is the code which calls the exe. As you can see, you can pass more than one environment variable. Just delimit them by a null character:


    STARTUPINFO sui;
    GetStartupInfo(&sui);

    PROCESS_INFORMATION pi;

    CreateProcess(
    NULL,
    "c:\\showvar.exe",
    NULL,
    NULL,
    FALSE,
    NORMAL_PRIORITY_CLASS,
    "AnyOtherVariable=World\0MyVariable=Hello\0",
    "c:\\",
    &sui,
    &pi);




    This is the code of the showvar.exe, which only shows the value of the variable MyVariable (outputs "Hello").


    char buf[100];
    if(GetEnvironmentVariable("MyVariable",buf,100))
    {
    MessageBox(0,buf,"MyVariable",MB_OK);
    }
    else
    {
    MessageBox(0,"Variable not found","MyVariable",MB_OK);
    }




    Hope this helps

    Bye
    Peter


  5. #5
    Join Date
    Apr 2001
    Posts
    488

    Re: Dos SET command

    Thanks, that is very helpful, the only problem that I have is right after I run create process,if I pull up a dos window and type SET it will not show that those parameters have been set? Here is the code that i am using

    if ( !CreateProcess(
    pProcess, // pointer to name of executable module
    (char *) pCommandLine, // pointer to command line string
    NULL, // pointer to process security attributes
    NULL, // pointer to thread security attributes
    FALSE, // handle inheritance flag
    NULL, // creation flags
    "LOG_NAME=TEST.LOG\0MAX_JOB_MIN=210\0JURISDICTION=ID\0EMAIL_LOG_DIR=????\0BAT_VIEW_VERSION=03\0BAT_VIEW_NAME=CARRQ105\0", // pointer to new environment bloc
    NULL, // same drive/directory
    siStartInfo, // STARTUPINFO struct
    &proc_info ) )



    Any ideas why this won't work?


  6. #6
    Join Date
    May 1999
    Location
    Germany
    Posts
    106

    Re: Dos SET command

    If you call CreateProcess and pass an environment string, this environment will take effect only to the created process. Only the created process knows about the environment variables LOG_NAME, MAX_JOB_IN ... and so on.
    If you want to create a environment variable that is available for all application use

    SetEnvironmentVariable("LOG_NAME","TEST.LOG");





    Bye
    Peter


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)