How to set pointer for environment variable

This is a discussion on How to set pointer for environment variable within the C++ Programming forums, part of the General Programming Boards category; I have 2 loaders (loader1 and loader2). I also have a Pinger which depends on the call i give will ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    How to set pointer for environment variable

    I have 2 loaders (loader1 and loader2). I also have a Pinger which depends on the call i give will execute either loader1 or loader2. I have problem that the pointer strloaderPath. The problem like you see below if i call loader1 this program will execute loader by getting the environment variable. So the first time I call loader1 strloaderPath will get the env variable which will be c:\program Files\ then it will use strcat to add loader1.exe to execute it. Now if i want to execute loader2 when strloaderPath get the environment variable it will get it wrong it will get c:\program files\loader1.exe which is wrong because it should only get c:\program files then if i do strcat it should show c:\program files\loader2.exe and not c:\program files\loader1.exe\loader2.exe

    switch (Status)
    {
    case RFX_SUCCESS:
    char strLog[20];
    char strLoader[256];
    char *pFind;
    int lLength;
    char *strLoaderPath;

    strLoaderPath = getenv("BSI_CF_DIR");
    if (Request.APIType == RFX_USE_RF_API) {
    pFind = strstr(strLoaderPath, "loader1.exe");
    if (pFind == NULL){
    strcat(strLoaderPath, "\\loader1.exe ");}
    }
    else {
    pFind = strstr(strLoaderPath, "loader2.exe");
    if (pFind == NULL){
    strcat(strLoaderPath, "\\loader2.exe ");}
    }
    itoa(Request.RequestNumber,strLog,10);
    strcat(strLoader, strLog);
    strcat(strLoader, " ");
    strcat(strLoader, Request.DataSetID);
    STARTUPINFO si;
    ::ZeroMemory(&si, sizeof(STARTUPINFO));
    si.cb = sizeof(STARTUPINFO);
    PROCESS_INFORMATION pi;
    lLength = CreateProcess(NULL,strLoader,NULL,NULL,FALSE,CREAT E_NEW_CONSOLE,NULL,NULL,&si, &pi);
    if (! lLength){
    error("CreateProcess failed");}
    break;
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    USE CODE TAGS
    Last edited by SlyMaelstrom; 08-18-2006 at 02:07 PM.
    Sent from my iPadŽ

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Good reply Sly!! I hope you like the appending to my sig, and suggest you copy! The colors really bring out your eyes!

    spiky1 - lots of people won't look at your code unless you wrap code tags around it -

    Code:
    switch (Status)
    {
    case RFX_SUCCESS:
    char strLog[20];
    char strLoader[256];
    char *pFind;
    int lLength;
    char *strLoaderPath;
    
    strLoaderPath = getenv("BSI_CF_DIR");
    if (Request.APIType == RFX_USE_RF_API) {
    pFind = strstr(strLoaderPath, "loader1.exe");
    if (pFind == NULL){
    strcat(strLoaderPath, "\\loader1.exe ");}
    }
    else {
    pFind = strstr(strLoaderPath, "loader2.exe");
    if (pFind == NULL){
    strcat(strLoaderPath, "\\loader2.exe ");}
    }
    itoa(Request.RequestNumber,strLog,10);
    strcat(strLoader, strLog);
    strcat(strLoader, " ");
    strcat(strLoader, Request.DataSetID);
    STARTUPINFO si;
    ::ZeroMemory(&si, sizeof(STARTUPINFO));
    si.cb = sizeof(STARTUPINFO);
    PROCESS_INFORMATION pi;
    lLength = CreateProcess(NULL,strLoader,NULL,NULL,FALSE,CREAT E_NEW_CONSOLE,NULL,NULL,&si, &pi);
    if (! lLength){
    error("CreateProcess failed");}
    break;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >lots of people won't look at your code unless you wrap code tags around it
    And the same amount of people won't look at unformatted code even if you wrap code tags around it.

    [edit]
    But since this is a relatively simple problem, just make a copy of the environment variable before changing it.
    [/edit]
    My best code is written with the delete key.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> And the same amount of people won't look at unformatted code even if you wrap code tags around it.

    I can't do everything for the OP

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >strLoaderPath = getenv("BSI_CF_DIR");
    What you want to do is strcpy() the string returned by getenv() to a buffer, then strcat() to that:
    Code:
    //Make strLoaderPath a buffer, not a pointer
    char strLoaderPath[256];
    char *p = getenv("BSI_CF_DIR");
    if (p == NULL)
    {
       //Error could not find environment variable
    }
    //Now strcpy() to a buffer
    strcpy(strLoaderPath, p);
    >itoa(Request.RequestNumber,strLog,10);
    >strcat(strLoader, strLog);
    >strcat(strLoader, " ");
    >strcat(strLoader, Request.DataSetID);
    Here you could make use of sprintf() or a stringstream if you want to do it the c++ way.
    Code:
    //Include <cstdio> at the top of your program.
    .
    .
    sprintf(strLoader, "%d %s", Request.RequestNumber, Request.DataSetID);

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I'd recommend using <string>s. It makes strcatting etc much easier, IMO.


    >strcat(strLoader, strLog);
    >strcat(strLoader, " ");
    >strcat(strLoader, Request.DataSetID);

    then changes to -
    stdLoader += strLog + " " + Request.DataSetID;

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    2
    Thank you so much. I tried what you suggested and it did work. The only change I did is the way the pointer p because it gave me an error with the initialization so I did it in 2 steps and it did work.

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 01:39 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Replies: 2
    Last Post: 11-28-2003, 10:50 AM
  5. eh, help?
    By The_Nymph in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21