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

Thread: Using putenv

  1. #1
    Join Date
    May 2009
    Posts
    29

    Using putenv

    Hi,

    I'm building a DLL in Visual C/C++ 6; one of the functions I want, is to create an environment variable. This DLL is to be used both in in VSC 6 as in VB 6, and all the functions are working...except this one.

    I created the following routine that, when compiled, does not give any error, but when called - by VB 6 for example - gives an error:

    void my_file(char *filename)
    {
    char *var = "MYPATH=";
    strcat(var,filename);
    int putenv(const char * var);
    }

    Can someone help me out?

    jkepler

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Using putenv

    What error?
    Victor Nijegorodov

  3. #3
    Join Date
    May 2009
    Posts
    29

    Re: Using putenv

    VB 6 simply crashes, for example.

    Regards,

    jkepler

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Using putenv

    Perhaps, your function declaration or parameter type is not compatible with what VB expects?
    Victor Nijegorodov

  5. #5
    Join Date
    May 2009
    Posts
    29

    Re: Using putenv

    I don't think so...I'm trying to trace the problem. I've tryed the following routine:

    void my_file(char *filename, char *out)
    {

    char var[20];
    strcpy (var,"MYPATH=.\\");
    strcat (var,filename);
    *out= *var;
    }

    and VB6 still crashes! This should be simple. The problem is in the string's operations, I think...

    jkepler

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Using putenv

    Quote Originally Posted by jkepler View Post
    I've tryed the following routine:
    Code:
    void my_file(char *filename, char *out)
    {
    
        char var[20];
        strcpy (var,"MYPATH=.\\");
        strcat (var,filename);
        *out= *var;
    }
    and VB6 still crashes! This should be simple. The problem is in the string's operations, I think...

    jkepler
    The code looks like buggy:
    1. What if the length of you filename would be more than 10?
    2. The last line does not make any sense, since it only copies the first character from var array to the first position of out array.

    Quote Originally Posted by jkepler View Post
    void my_file(char *filename)
    Code:
    {
    	char *var = "MYPATH=";
    	strcat(var,filename);
    	int putenv(const char * var);
    }
    What does the last line mean? It looks like a declaration!?
    Victor Nijegorodov

  7. #7
    Join Date
    May 2009
    Posts
    29

    Re: Using putenv

    The filename is always less than 10; how can I copy the entire var to out (out must be *out to the value be returned)?

    jkepler

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Using putenv

    Quote Originally Posted by jkepler View Post
    how can I copy the entire var to out (out must be *out to the value be returned)?
    Just get rid of var and use out instead.
    Note that the char* out array, the calling side passing in, must have enough size to contain the whole string.
    Victor Nijegorodov

  9. #9
    Join Date
    May 2009
    Posts
    29

    Re: Using putenv

    I apologise....but can you post the code?

    Kind regards,

    jkepler

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Using putenv

    No, I don't know VB good enough.
    Victor Nijegorodov

  11. #11
    Join Date
    May 2009
    Posts
    15

    Re: Using putenv

    Quote Originally Posted by jkepler View Post
    Hi,

    I'm building a DLL in Visual C/C++ 6; one of the functions I want, is to create an environment variable. This DLL is to be used both in in VSC 6 as in VB 6, and all the functions are working...except this one.

    I created the following routine that, when compiled, does not give any error, but when called - by VB 6 for example - gives an error:

    void my_file(char *filename)
    {
    char *var = "MYPATH=";
    strcat(var,filename);
    int putenv(const char * var);
    }

    Can someone help me out?


    jkepler
    char * strcat ( char * destination, const char * source );

    strcat is a function that is append a copy of source string (is filename) to the destination string (is var).

    because var is pointer that point to constant char* "MYPATH=". it is read only memory so you can not append or change it. It compile with no error but runtime error will appear when it run.

    To set environment variable:

    Code:
    void my_file(char *filename)
    {
           char* key = "MYPATH=";
    
           char* exp = (char*) malloc(strlen(filename)  + strlen(key) + 1); 
           
     	strcpy(exp , key  ); 
    	strcat(exp ,filename);
    
    	putenv(exp);
    
            free(exp);
    }

  12. #12
    Join Date
    May 2009
    Posts
    29

    Re: Using putenv

    Thank you very much for your kindness and explanations. It works fine.

    Kind regards,

    jkepler

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

Featured