|
-
May 13th, 2009, 03:48 AM
#1
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
-
May 13th, 2009, 04:01 AM
#2
Re: Using putenv
Victor Nijegorodov
-
May 13th, 2009, 04:46 AM
#3
Re: Using putenv
VB 6 simply crashes, for example.
Regards,
jkepler
-
May 13th, 2009, 04:55 AM
#4
Re: Using putenv
Perhaps, your function declaration or parameter type is not compatible with what VB expects?
Victor Nijegorodov
-
May 13th, 2009, 06:02 AM
#5
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
-
May 13th, 2009, 06:15 AM
#6
Re: Using putenv
 Originally Posted by jkepler
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.
 Originally Posted by jkepler
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
-
May 13th, 2009, 09:02 AM
#7
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
-
May 13th, 2009, 09:10 AM
#8
Re: Using putenv
 Originally Posted by jkepler
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
-
May 13th, 2009, 09:43 AM
#9
Re: Using putenv
I apologise....but can you post the code?
Kind regards,
jkepler
-
May 13th, 2009, 10:06 AM
#10
Re: Using putenv
No, I don't know VB good enough.
Victor Nijegorodov
-
May 14th, 2009, 06:46 AM
#11
Re: Using putenv
 Originally Posted by jkepler
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);
}
-
May 14th, 2009, 11:16 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|