error C2677: binary '%': no global operator found which takes type 'std::string' (or there is no acceptable conversion)


1
2
WinExec("netsh advfirewall firewall add rule name="%name%" enable=no
action=block program="%path%" dir=in", SW_HIDE);


In the application i've also got string name;
and that all works, i just don't know how to get it to take what is saved in that string and use it for the rule name & path.

i tried using %name% but it just comes up with the error, and without either % it just calls it name.


i basically want the user to be able to input the name of the firewall rule.
Last edited on
C++ language knows nothing about enviroment variables.

you should store a path to string variable and then read from string.

psuedo code:
1
2
std::string PATH  = "input path here"
WinExec(std::string("netsh advfirewall firewall add rule name=") + PATH + "... ").c_str());
You may be able to use getenv() to get environment variables.
I don't know what i'm doing wrong, everything works fine except for the WinExec line.

 
WinExec(std::string("netsh advfirewall firewall add rule name=") + name + ("program=") + path + ("enable=no action=block dir=in"), SW_HIDE);


and above that
1
2
3
string a, specific, path, name;
std:string NAME = name;
std::string PATH = path;


What it gives as an error

error C2664: 'UINT WinExec(LPCSTR,UINT)': cannot convert argument 1 from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'LPCSTR'

message : No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

i've tried getenv() but i have no idea how to use it, tried something like.
WinExec("netsh advfirewall firewall add rule name=" + getenv(name) + "enable=no action=block dir=in", SW_HIDE);
error C2664: 'UINT WinExec(LPCSTR,UINT)': cannot convert argument 1 from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'LPCSTR'

friend, you misread my example code :)

what you need is to call c_str() at the end, see my example again.
basically, you construct a string inside paranteses and then call c_str(), like the following working code:

1
2
3
4
	string a, specific, path, name;
	std:string NAME = name;
	std::string PATH = path;
	WinExec((std::string("netsh advfirewall firewall add rule name=") + name + "program=" + path + "enable=no action=block dir=in").c_str(), SW_HIDE);


note that variables need to have valid path names, not empty strings.
for example (note the escape \ character):
string path = "C:\\path_to_my\\dir"

also, you will need to use, std::wstring for UNICODE strings

for getenv see:
http://www.cplusplus.com/reference/cstdlib/getenv/

example: (not sure what PATH contains btw! you may need to work on that)
1
2
3
	std:string NAME = "some name";
	std::string PATH = std::getenv("PATH");
	WinExec((std::string("netsh advfirewall firewall add rule name=") + name + "program=" + PATH + "enable=no action=block dir=in").c_str(), SW_HIDE);


also note that WinExec is deprecated API, and, CreateProcess is replacement.
Last edited on
Topic archived. No new replies allowed.