This may be the wrong forum to ask this, and if it is I apologize. I know this is a simple question and with a forum full of programmers I should be able to get this answered anyways.
I have the following
char y[x];
I want use 'test' in a function that used to be a main function for a command line program I wrote a while ago.
int main(int argc, char **argv);
I turned main into testtest.
int testtest(int argc, char **argv);
and want to use the variable 'y' as the 2nd argument in testest so I can use it as argv.
Any help would be appreciated. I setup a program using Win32 API and have a text window that I want to be able to type text into and run that text into the testtest function and use the code from my old program without much editing of my old program.
Thanks
-Nick
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This is actually what I have so far with what you gave me. It compiles but crashes.
I am not sure if that is even the correct code for getting the text from a window. From digging through some of the examples I have it looks like it should work, but it crashes.
If I force the arguments and add them one at a time to argv it compiles and runs fine if I run it through the testtest function, so I know it has to be something in this code here.
char *py;
int argc = 0;
char argv[256];
DWORD dwTextLength;
I'm sure there's a better way, but I think this would work (assuming y[x] is a single string, which doesn't quite match **argv which is more like an array of strings):
char y[10] = "Whatever";
char *py; // pointer to y
py=&y[0];
return_value = testtest(some_int,&py);
Then within testtest you can access your original string y using argv[0]:
cout << argv[0] << endl;
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hmm, still doesn't work. This is damn frustrating...It compiles now, but just plain isn't working.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-27
The first element of an argv array is always the command executable path/name, so you need ot dummy that to get the argument y in the right location. You cannot pass any only some_int - it has to be teh number of arguments (including the path/name).
Actually in my example you can pass an arbitrary some_int because his array didn't have two dimensions. He just had y[x] not y[x][x] so argc is no longer relevant. But if his testtest function was actually going to use argc and argv as they were originally intended as part of main, then yes you're right and the OP's definition of y was wrong. I was trying to make it work with his definition of y.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-27
testetest() was a rename of main() so it seemed reasonable that he wanted it to work unchanged.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
andybuonviri, yes I am using char y[x]; because that when using some example code for the Win32 API that is what is used to get the text from a text window. I want to try and do this without too much code editing so I figure the easiest way to do it would be to convert it to work in a function requiring char **argv
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-27
Does y contain only one argument, or a string of space separated arguments? If the latter you need:
Clifford's scheme should work for you. It's way better than what I suggested since it doesn't require you to change any code within testtest(). If it were me, I'd probably clean up testtest to accept the single string rather than argc/argv, but that's up to you.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This may be the wrong forum to ask this, and if it is I apologize. I know this is a simple question and with a forum full of programmers I should be able to get this answered anyways.
I have the following
char y[x];
I want use 'test' in a function that used to be a main function for a command line program I wrote a while ago.
int main(int argc, char **argv);
I turned main into testtest.
int testtest(int argc, char **argv);
and want to use the variable 'y' as the 2nd argument in testest so I can use it as argv.
Any help would be appreciated. I setup a program using Win32 API and have a text window that I want to be able to type text into and run that text into the testtest function and use the code from my old program without much editing of my old program.
Thanks
-Nick
This is actually what I have so far with what you gave me. It compiles but crashes.
I am not sure if that is even the correct code for getting the text from a window. From digging through some of the examples I have it looks like it should work, but it crashes.
If I force the arguments and add them one at a time to argv it compiles and runs fine if I run it through the testtest function, so I know it has to be something in this code here.
char *py;
int argc = 0;
char argv[256];
DWORD dwTextLength;
dwTextLength = GetWindowTextLength(GetDlgItem(hwnd, IDC_CHILD_EDIT));
argv[0] = "testtest";
argv[1] = strtok( LPSTR(GlobalAlloc(GPTR, dwTextLength + 1)) , "\t " );
while( argc < 256 && argv[argc] != 0 )
{
argc++ ;
argv[argc] = strtok( 0, "\t " );
}
argc--;
testtest( argc, argv );
I really appreciate your help guys. You have no idea. :) Thanks!
-Nick
"char argv[2][256] ;"
"argv[0] = "testtest" ;"
"argv[1] = y ;"
"testtest( 2, argv ) ;"
O_o
No. No! NO!
What do you think you are doing?
Re: Nick
You haven't actually put anything into the memory. There is nothing to parse.
Soma
Gosh.... did I type that!? What was I thinking?
I'm sure there's a better way, but I think this would work (assuming y[x] is a single string, which doesn't quite match **argv which is more like an array of strings):
char y[10] = "Whatever";
char *py; // pointer to y
py=&y[0];
return_value = testtest(some_int,&py);
Then within testtest you can access your original string y using argv[0]:
cout << argv[0] << endl;
Hmm, still doesn't work. This is damn frustrating...It compiles now, but just plain isn't working.
The first element of an argv array is always the command executable path/name, so you need ot dummy that to get the argument y in the right location. You cannot pass any only some_int - it has to be teh number of arguments (including the path/name).
char argv[2][256] ;
argv[0] = "testtest" ;
argv[1] = y ;
testtest( 2, argv ) ;
The array y MUST be a null terminated string.
Now if this continues to not work, you will have to post the code.
Actually in my example you can pass an arbitrary some_int because his array didn't have two dimensions. He just had y[x] not y[x][x] so argc is no longer relevant. But if his testtest function was actually going to use argc and argv as they were originally intended as part of main, then yes you're right and the OP's definition of y was wrong. I was trying to make it work with his definition of y.
testetest() was a rename of main() so it seemed reasonable that he wanted it to work unchanged.
andybuonviri, yes I am using char y[x]; because that when using some example code for the Win32 API that is what is used to get the text from a text window. I want to try and do this without too much code editing so I figure the easiest way to do it would be to convert it to work in a function requiring char **argv
Does y contain only one argument, or a string of space separated arguments? If the latter you need:
int argc = 0 ;
argv*[256] ;
argv[0] = "testtest" ;
argv[1] = strtok( y, "\t " ) ;
while( argc < 256 && argv[argc] != 0 )
{
argc++ ;
argv[argc] = strtok( 0, "\t " ) ;
}
argc-- ;
testtest( argc, argv ) ;
Clifford
Clifford's scheme should work for you. It's way better than what I suggested since it doesn't require you to change any code within testtest(). If it were me, I'd probably clean up testtest to accept the single string rather than argc/argv, but that's up to you.
Alright. I got it working. :) Thanks guys!
It was working all along but some other code from the example was causing problems the whole time.
Alright, so now the only thing I got to do is parse the commands and I am good to go. ;)
slocombe, your code makes it crash when I use it. Not sure why.
I think it has a couple of typos:
"argv[256];" should probably be "char argv[256];"
argv[0]="testtest"; is not quite right. It should be something like: char temp[]={"testtest"}; argv[0]=&temp[0];
argv is an array of pointers so it can't actually store the string "testtest", just the pointer to the array that does store it.
Yes. I should have warned you that it was illustrative only. Sorry.