0

Here is my code:

if (system("C:\WINDOWS\System32\netsh.exe interface ip set address name="Net" static 169.254.216.78 255.255.255.252 none >nul"))
{
  printf("Error is %d.\n", GetLastError());
}

In my case error is 2. I've read about this article which said that error may indicate DLL required by the executable is not available. But it didn't say how can I find exact DLL. It only specified that process explorer could be used for that, but didn't mention instructions how to do it. I searched for a while and could not find anything that would help me. Could you please tell me how to debug and find the exact DLL which is not loaded? Thanks in advance.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Rasty
  • 303
  • 3
  • 14
  • This is the wrong way to do this. Use the API. And FWIW you can't use GetLastError like that since you don't know what other API calls gave been made. – David Heffernan Jan 15 '17 at 19:10
  • so you suggest to check `errno` instead of calling `GetLastError`? – Rasty Jan 15 '17 at 20:18
  • No not at all. You can't get a meaningful error code from the code in the question. Use the API – David Heffernan Jan 15 '17 at 20:20
  • Sorry for misunderstanding but as I understand you are saying that `netsh` may use some API calls that have failed and I can't identify what is the exact problem? – Rasty Jan 15 '17 at 20:47
  • You certainly can't because that's all running in a different process. Again, use the API to do this. – David Heffernan Jan 15 '17 at 20:48
  • One more quesiton please, As you noticed I'm using winapi, if I use `CreateProcess` instead of `system` I will have the exit code of process right? and would it be the error code that crashed netsh? I'm sorry if question is silly, I'm new with winapi. – Rasty Jan 16 '17 at 08:26
  • I don't think netsh crashed. You might get more info from CreateProcess. But why don't you use the API for this. I'm tired of telling you that. – David Heffernan Jan 16 '17 at 08:27
  • sorry for late answer. As for now, I'm trying to achieve the goal without using API, The problem is that its my college project, and I want to know exactly why does `system` doesn't work. I added infinite loop before system and opened `netsh.exe` and typed same command, and it gave me error saying `he filename, directory name, or volume label syntax is incorrect.` I checked interfaces with `interface show interface` and it doesn't seem to list my device there. But during creation of device I had no error. – Rasty Jan 16 '17 at 14:00
  • OK, well that doesn't hold any great interest to me. Good luck working it out. – David Heffernan Jan 16 '17 at 14:00

1 Answers1

1

String literals in C and C++ allow for so-called escape sequences to encode specific characters. In your case, you accidentally added escape sequences. If you want a string literal to contain the character \, you have to use \\. If you want to embed a double quote character, you need to type \":

system("C:\\WINDOWS\\System32\\netsh.exe interface ip set address name=\"Net\" "
       "static 169.254.216.78 255.255.255.252 none >nul")
IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • sorry for late response, its because time difference, I tried that too, but it gives me same error – Rasty Jan 17 '17 at 07:44
  • @Rasty: The error you got is meaningless, as was pointed out on numerous occasions. Other than that, this proposed answer addresses the only visible issue with the code you posted. If you need real help, post a real question (including a [mcve]). – IInspectable Jan 18 '17 at 13:13