Click to See Complete Forum and Search --> : Using putenv
jkepler
May 13th, 2009, 03:48 AM
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
VictorN
May 13th, 2009, 04:01 AM
What error?
jkepler
May 13th, 2009, 04:46 AM
VB 6 simply crashes, for example.
Regards,
jkepler
VictorN
May 13th, 2009, 04:55 AM
Perhaps, your function declaration or parameter type is not compatible with what VB expects? :confused:
jkepler
May 13th, 2009, 06:02 AM
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
VictorN
May 13th, 2009, 06:15 AM
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...
jkeplerThe 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.
void my_file(char *filename)
{
char *var = "MYPATH=";
strcat(var,filename);
int putenv(const char * var);
}What does the last line mean? It looks like a declaration!? :confused:
jkepler
May 13th, 2009, 09:02 AM
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
VictorN
May 13th, 2009, 09:10 AM
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.
jkepler
May 13th, 2009, 09:43 AM
I apologise....but can you post the code?
Kind regards,
jkepler
VictorN
May 13th, 2009, 10:06 AM
No, I don't know VB good enough.
ptaminh
May 14th, 2009, 06:46 AM
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:
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);
}
jkepler
May 14th, 2009, 11:16 AM
Thank you very much for your kindness and explanations. It works fine.
Kind regards,
jkepler
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.