Hello!

I am writing a program to execute another process. But the argment string has " ) ( ; / \ symbols. So it does not allow me to start the program with the true string (argument)...

Here is the program: the program is on the user agent string:

#include <unistd.h>
#include <stdio.h>

int main()
{
        char *args[] = { "--user-agent="Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0"", NULL };
        execv("C:\\Users\\userl\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe", args);         
        return 1;
}

To have a slash or quote in a string give like this "abc \" " if you give printf("abc \" "); will display abc "

Change you string accordingly. Perhaps like this,

char *args[] = { "--user-agent=\"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0\" ", NULL };

Use code tags

To have a slash or quote in a string give like this "abc \" " if you give printf("abc \" "); will display abc "

Change you string accordingly. Perhaps like this,

char *args[] = { "--user-agent=\"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)\" ", NULL };

Use code tags

I tried it. But it does not work. Chrome works with with command properly from command line : chrome.exe --user-agent="Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"

But this way you told me does not work.

Then print your command instead of trying to execute it and see where the differences lie between your manual command and the generated one.

But the argment string has " ) ( ; / \ symbols. So it does not allow me to start the program with the true string (argument)...

I meant the way you have initialized args[]. It will not work because you didn't add \ after "user-agent=" in the string. You should have got compiler errors for the way you have initialized

I try it with print function and i see it must work. That means execv does not sends the strings as arguments.. :( Which function i should use from standart C libary ?

-----------

I found it :) This is the solution :

char *args[] = {"chrome.exe", "--user-agent=\"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/99.0)\" ", NULL};
execv("C:\\Users\\userl\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe", args);

Thank you for your helping...

There is one function system(). It will call the command and execute command lines that is given within the parenthesis. You need to include stdlib.h or cstdlib.h
eg:
system("DIR");

But its usage is not recommended as it involves calling the os etc..

There is one function system(). It will call the command and execute command lines that is given within the parenthesis. You need to include stdlib.h or cstdlib.h
eg:
system("DIR");

But its usage is not recommended as it involves calling the os etc..

Hmm.. system function works better. I mean it is easy. I will write very simple C code to start 2-3 softwares automatically(just for windows 7). Is it problem for me, on this situation to use system("dir") function?(because you write me it is not recomended)

Also my old code which worked, gives me an error from IDE : 11 C:\Users\user\Desktop\Untitled1.c [Warning] passing arg 2 of `execv' from incompatible pointer type

The system(dir) funciton starts the new process but command line stays always background... Can we solve this problem ?

For execv, the args[0] is a process name. You need to initialize it as

char *args[] = ["chrome", "--user-agent=blah", 0];

The actual value of process name is highly irrelevant, but it must be there.

Here's a link

For execv, the args[0] is a process name. You need to initialize it as

char *args[] = ["chrome", "--user-agent=blah", 0];

The actual value of process name is highly irrelevant, but it must be there.

It gives me the same error. I try also your parametres with NUll at the end but i face the same problem.

(Also in your code line [ symbol is a sytax error. I use { instead of it... )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.