WinAPI - CreateProcessW fails when C:\Program file exists - c

I run the following C code to create a process. The exe file is located at C:\Program Files\Exes\Start\process1.exe
CreateProcessW(NULL, (char*) exePath,
NULL, NULL, TRUE,
flags,
NULL, NULL, &startupInfo, &processInformation);
Now some computers randomly have this file called Program located at C:\Program which causes create process to fail with the error:
%1 is not a valid Win32 application.
Is there a way to fix this problem apart from renaming the file because there are a few dozen Windows Vms on which this C code is executed. This error randomly happens on a few machines.

How does this code even compile? The second parameter to CreateProcessW is defined as a LPWSTR which means it should only accept a wide string.
The next problem: as a result of the cast it's not possible to determine what the source of exePath is. The reason this is of consequence is that the 2nd parameter is defined as a LPWSTR, not a LPCWSTR (i.e. a const wide string) for a reason: CreateProcessW may write to the buffer.
Then the 3rd problem - your exe path had a space in it. When passed as the 2nd parameter (lpCommandLine) CreateProcess has some derpy logic to guess where the exe name ends and the command line begins. Which necessitates quote encoding the exe path part of the command line.
When you have the full path to the exe - and no parameters - the simplest / safest thing to do is to just pass it as the lpApplicationName parameter. This IS a const parameter which avoids any potential undefined behaviour that could result if your command line source was a constant string literal etc. And is simply used as the path to the exe to execute, so does not (and can not) have any quoting requirements.
CreateProcess(exePath,NULL,...);
Aside: Using both parameters to CreateProcess essentially lets you set argv[0] of the launched application to be anything you want. So you could run an application from a particular path / exe name, but make argv[0] point at some other path or exe name.
To pass a parameter to the exe, rather than passing the full (quote enclosed) path you could do something simple like this:
WCHAR cmdLine[] = TEXT("console1.exe --version");
CreateProcessW(exePath,cmdLine,...);
Edits Credit to Paul Sanders and the other comments for pointing out the unquoted exe path, and also caused me to completely break my original answer which had solved that without me realising it.
Credit to RbMm for spotting I had broken my answer, and credit to eryksun for showing me that I've been reading the documentation wrong for almost 20 years now.

Chris makes a lot of good points there and you should take them on board, but the real issue, as immibis says, is that you are using the CreateProcess API incorrectly. The way you've done it requires exepath to be quoted, if it contains any whitespace.
So, instead, after you've fixed all the issues that Chris has raised, do this:
CreateProcessW (exePath, NULL,
NULL, NULL, TRUE,
creation_flags,
NULL, NULL, &startupInfo, &processInformation);
i.e. just swap the first and second parameters around (and perhaps use a more descriptive name for flags, as I've done here).
You can pass any command line arguments you might need in that second parameter, if that ever becomes a requirement. And please, read the docs.
Edit (to get my rep back :)
Also, as Chris says, exepath needs to be a wide string here, so I took out your (redundant) cast which would in fact generate a compiler error (C++) or warning (C) (so, a warning, guess, since your post is tagged As C, thank you #Barmak).
Anyway, if you now don't get one, then exepath must in reality already be a wide string so all is well. If not, then you obviously need to fix that, but I imagine this whole issue is just a typo in your post since you clearly did have your code working in order to report the behaviour you are observing.

Related

using GDB with arguments

For a class assignment we needed to write a compiler. This includes an optimizer portion. In other words, we take in a file with some "code". An output file is generated. In the second step we take in the outputted code and remove any "dead" code and re-output to a second file. I have some problems with the optimizer portion and would like to use gdb. But I can't get gdb to operate properly with the input and output files arguments. The way we would normally run the optimizer is:
./optimize <tinyL.out> optimized.out
where tinyL.out is the file outputted in the first step and optimized.out is the file I want to output with the new optimized and compiled code.
I have searched Google for the solution and the tips I have found do not seem to work for my situation. Most people seem to want to only accept an input file and not output a separate file as I need to do.
Any help is appreciated (of course)
I'm not exactly sure what you're asking. But since I'm not yet able to comment everywhere, I write this answer with a guess and edit/delete if necessary.
When GDB is started and before you start the program you wish to debug, set the arguments you want to use with set args.
A reference to the documentation.
You just need to do the file redirection within gdb.
gdb ./optimize
(gdb) run < tinyL.out > optimized.out
https://stackoverflow.com/a/2388594/5657035

How to know the path of current binary file? [duplicate]

This question already has answers here:
Finding current executable's path without /proc/self/exe
(14 answers)
Closed 7 years ago.
Is there a way in C/C++ to find the location (full path) of the current executed program?
(The problem with argv[0] is that it does not give the full path.)
To summarize:
On Unixes with /proc really straight and realiable way is to:
readlink("/proc/self/exe", buf, bufsize) (Linux)
readlink("/proc/curproc/file", buf, bufsize) (FreeBSD)
readlink("/proc/self/path/a.out", buf, bufsize) (Solaris)
On Unixes without /proc (i.e. if above fails):
If argv[0] starts with "/" (absolute path) this is the path.
Otherwise if argv[0] contains "/" (relative path) append it to cwd
(assuming it hasn't been changed yet).
Otherwise search directories in $PATH for executable argv[0].
Afterwards it may be reasonable to check whether the executable isn't actually a symlink.
If it is resolve it relative to the symlink directory.
This step is not necessary in /proc method (at least for Linux).
There the proc symlink points directly to executable.
Note that it is up to the calling process to set argv[0] correctly.
It is right most of the times however there are occasions when the calling process cannot be trusted (ex. setuid executable).
On Windows: use GetModuleFileName(NULL, buf, bufsize)
Use GetModuleFileName() function if you are using Windows.
Please note that the following comments are unix-only.
The pedantic answer to this question is that there is no general way to answer this question correctly in all cases. As you've discovered, argv[0] can be set to anything at all by the parent process, and so need have no relation whatsoever to the actual name of the program or its location in the file system.
However, the following heuristic often works:
If argv[0] is an absolute path, assume this is the full path to the executable.
If argv[0] is a relative path, ie, it contains a /, determine the current working directory with getcwd() and then append argv[0] to it.
If argv[0] is a plain word, search $PATH looking for argv[0], and append argv[0] to whatever directory you find it in.
Note that all of these can be circumvented by the process which invoked the program in question. Finally, you can use linux-specific techniques, such as mentioned by emg-2. There are probably equivalent techniques on other operating systems.
Even supposing that the steps above give you a valid path name, you still might not have the path name you actually want (since I suspect that what you actually want to do is find a configuration file somewhere). The presence of hard links means that you can have the following situation:
-- assume /app/bin/foo is the actual program
$ mkdir /some/where/else
$ ln /app/bin/foo /some/where/else/foo # create a hard link to foo
$ /some/where/else/foo
Now, the approach above (including, I suspect, /proc/$pid/exe) will give /some/where/else/foo as the real path to the program. And, in fact, it is a real path to the program, just not the one you wanted. Note that this problem doesn't occur with symbolic links which are much more common in practice than hard links.
In spite of the fact that this approach is in principle unreliable, it works well enough in practice for most purposes.
Not an answer actually, but just a note to keep in mind.
As we could see, the problem of finding the location of running executable is quite tricky and platform-specific in Linux and Unix. One should think twice before doing that.
If you need your executable location for discovering some configuration or resource files, maybe you should follow the Unix way of placing files in the system: put configs to /etc or /usr/local/etc or in current user home directory, and /usr/share is a good place to put your resource files.
In many POSIX systems you could check a simlink located under /proc/PID/exe. Few examples:
# file /proc/*/exe
/proc/1001/exe: symbolic link to /usr/bin/distccd
/proc/1023/exe: symbolic link to /usr/sbin/sendmail.sendmail
/proc/1043/exe: symbolic link to /usr/sbin/crond
Remember that on Unix systems the binary may have been removed since it was started. It's perfectly legal and safe on Unix. Last I checked Windows will not allow you to remove a running binary.
/proc/self/exe will still be readable, but it will not be a working symlink really. It will be... odd.
On Mac OS X, use _NSGetExecutablePath.
See man 3 dyld and this answer to a similar question.
For Linux you can find the /proc/self/exe way of doing things bundled up in a nice library called binreloc, you can find the library at:
http://autopackage.org/docs/binreloc/
I would
1) Use the basename() function: http://linux.die.net/man/3/basename
2) chdir() to that directory
3) Use getpwd() to get the current directory
That way you'll get the directory in a neat, full form, instead of ./ or ../bin/.
Maybe you'll want to save and restore the current directory, if that is important for your program.

Matlab / M - pass a cell array into a function as input [duplicate]

This question already has answers here:
"Undefined function 'function_name' for input arguments of type 'double'."
(3 answers)
Closed 5 years ago.
I'm a new user of Matlab, can you please help:
I have the following code in an .M file:
function f = divrat(w, C)
S=sqrt(diag(diag(C)));
s=diag(S);
f=sqrt(w'*C*w)/(w'*s);
I have stored this file (divrat.M) in the normal Matlab path, and therefore I'm assuming that Matlab will read the function when it's starting and that this function therefore should be available to use.
However, when I type
>> divrat(w, C)
I get the following error
??? Undefined function or method 'divrat' for input arguments of type 'double'.
What is the error message telling me to do, I can't see any error in the code or the function call?
You get this error when the function isn't on the MATLAB path or in pwd.
First, make sure that you are able to find the function using:
>> which divrat
c:\work\divrat\divrat.m
If it returns:
>> which divrat
'divrat' not found.
It is not on the MATLAB path or in PWD.
Second, make sure that the directory that contains divrat is on the MATLAB path using the PATH command. It may be that a directory that you thought was on the path isn't actually on the path.
Finally, make sure you aren't using a "private" directory. If divrat is in a directory named private, it will be accessible by functions in the parent directory, but not from the MATLAB command line:
>> foo
ans =
1
>> divrat(1,1)
??? Undefined function or method 'divrat' for input arguments of type 'double'.
>> which -all divrat
c:\work\divrat\private\divrat.m % Private to divrat
As others have pointed out, this is very probably a problem with the path of the function file not being in Matlab's 'path'.
One easy way to verify this is to open your function in the Editor and press the F5 key. This would make the Editor try to run the file, and in case the file is not in path, it will prompt you with a message box. Choose Add to Path in that, and you must be fine to go.
One side note: at the end of the above process, Matlab command window will give an error saying arguments missing: obviously, we didn't provide any arguments when we tried to run from the editor. But from now on you can use the function from the command line giving the correct arguments.
The most common cause of this problem is that Matlab cannot find the file on it's search path. Basically, Matlab looks for files in:
The current directory (pwd);
Directly in a directory on the path (to see the path, type path at the command line)
In a directory named #(whatever the class of the first argument is) that is in any directory above.
As someone else suggested, you can use the command which, but that is often unhelpful in this case - it tells you Matlab can't find the file, which you knew already.
So the first thing to do is make sure the file is locatable on the path.
Next thing to do is make sure that the file that matlab is finding (use which) requires the same type as the first argument you are actually passing. I.el, If w is supposed to be different class, and there is a divrat function there, but w is actually empty, [], so matlab is looking for Double/divrat, when there is only a #(yourclass)/divrat. This is just speculation on my part, but this often bites me.
The function itself is valid matlab-code. The problem must be something else.
Try calling the function from within the directory it is located or add that directory to your searchpath using addpath('pathname').
The error code indicates the function definition cannot be found. Make sure you're calling the function from the same workspace as the divrat.m file is stored. And make sure divrat function is not a subfunction, it should be first function declaration in the file. You can also try to call the function from the same divrat.m file in order to see if the problem is with workspace selection or the function.
By the way, why didn't you simply say
s = sqrt(diag(C));
Wouldn't it be the same?
Also, name it divrat.m, not divrat.M. This shouldn't matter on most OSes, but who knows...
You can also test whether matlab can find a function by using the which command, i.e.
which divrat
I am pretty sure that the reason why this problem happened is because of the license of the toolbox (package) in which this function belongs in. Write which divrat and see what will be the result. If it returns path of the function and the comment Has no license available, then the problem is related to the license. That means, license of the package is not set correctly. Mostly it happens if the package (toolbox) of this function is added later, i.e., after installation of the original matlab. Please check and solve the license issue, then it will work fine.

Weird behavior with filename in /proc/pid/stat

I have a weird behavior with the value of filename in /proc/pid/stat.
My program's name is "test_dev", and when I execute it with "./test_dev" and i look on /proc/pid/stat, i see "pid (test) ....".
Same in /proc/pid/status.
I change "test_dev" to "testdev" to see if the underscore is the culprit of this mess, but the same thing appear again.
I printf argv[0], and I correctly see "test_dev" (or "testdev").
I wonder how the field in stat is set, and why it's incomplet, because the man of /proc say that is the filename of the executable.
I think a little, and I wonder if Eclipse can be the culprit.
This EDI gave me some surprise sometimes, and I won't be surprised if this is the case again, even if it really bug me that argv[0] and /proc/pid/stat have not the same value.
Is somebody have an explanation ?
thanks.
However, the real filename without "_dev" is 15 character long, and
the "comm" field seem to be limited by the kernel to 15 character long
... So, the filename is truncated in /proc/pid/stat, … Where can I
find this kind of documentation ?
Unfortunately, this seems to be not well documented where one would expect it, but there are some hints here and there on the proc man page.
/proc/[pid]/comm (since Linux 2.6.33)
… Strings longer than TASK_COMM_LEN (16) characters are silently truncated.
/proc/[pid]/stat
Status information about the process. This is used by ps(1).
It is defined in the kernel source file fs/proc/array.c.
TASK_COMM_LEN and the task_struct are defined in include/linux/sched.h:
#define TASK_COMM_LEN 16
…
struct task_struct {
…
char comm[TASK_COMM_LEN];
The difference between the 15 characters you observe and the 16 here is due to the terminating '\0'.
The field in /proc/pid/stat you mention does not show argv[0], but the executed command. Probably, eclipse compiles the file to an executable named "test" and executes it.
This example:
execl("foo", "bar", "baz", NULL);
has a comm of "foo" (the thing in /proc/pid/stat), an argv[0] of "bar" and "baz" as argv[1]
To double-check that, you might try to call your program "xxxxx" and see, if it is still named "test" in /proc/pid/stat

nsinstall: Bad file number error on Vista

I'm attempting to build Firefox on my Windows Vista Ultimate machine. I keep getting the following error:
nsinstall: Bad file number
I've read that this error is caused because of UAC in Vista. Here are the two articles that lead me to this conclusion. https://wiki.mozilla.org/Penelope_Developer_Page#Windows_Vista and http://www.kevinbrosnan.net/mozilla-build-error-nsinstall-bad-file-number
Using the standard "Run as Administrator", I've attempted to redo my build but I get the exact same error. I also started a normal command prompt as admin and then went to the batch file in mozilla-build (start-msvc8.bat) and ran it. Still, same error at the same point.
Any other insights on how I might either get around this error or perhaps something else is causing the error?
Note: I also posted something here in the hopes to get topic-specific help but I've not heard a peep... After I posted that I found the info on nsinstall. Anyway, I prefer SO so I thought I'd try here...
Update: I've attempted to completly disable UAC to correct the problem as is suggested by cnemelkasr. I've received the exact same error. This new knowledge is making me think that a file or folder is missing... Does anyone who has experience with NSInstall know what the given error -- Bad file number -- might mean? I figure it might be referring to a file handle...
If it really is a UAC error, you can try turning off UAC altogether. I've had to do this for several packages. There are numerous places on the web to get the instructions for doing that.
http://www.petri.co.il/disable_uac_in_windows_vista.htm is one of them.
I found the answer to my question. I'm posting the answer here to share the answer with others and to close this question.
After disabling the UAC, it was suggested that the directory depth was interfering with NSInstall. I moved the folder from c:/Users/Frank/Documents/hg-repos/firefox-src-hgRepo/mozilla-fv-expirement/ to C:/mozilla-fv-expirement/. Cleaned all previous build attempts and finally redid my build (with UAC off) and I received a working debug binary.
The suggestion was posted at: mozilla.dev.builds
The "Bad file number" message in the cases I have seen, is caused by too many arguments passed to execvp (command, argv) (or similar) function. But only from some programs. An old bash, sh or a Borland/Watcom program in your PATH is an likely candidate.
So when you shorten the name of the build directory, the total size of the command line (that eventually gets passed to CreateProcess()) gets shorter. I don't think UAC has anything to do with this since I've seen this on Win-XP too. But it's a bit strange Mozilla would not use relative paths while building. I guess it uses some directory prefix value in it's makefiles (I've never tried building it).
If you look at the documentation for _execvp():
http://msdn.microsoft.com/en-us/library/3xw6zy53.aspx
E2BIG is one of the possible errno values:
The space required for the arguments and environment settings exceeds 32 KB.
Now, here is the strange part.
Fact 1:
On Visual-C/MingW (any version), strerror(EBADF) doesn't return "Bad file number" .
(it return "Bad file descriptor").
Fact 2:
On Borland's CBuilder 5.6 and Watcom 1.9 (these do not use the MSVC runtime), strerror(EBADF) does indeed return "Bad file number".
Theory:
Is possible that Borland, Watcom (and other CRTs too?) mixes up the meaning of E2BIG and EBADF. Did that make any sense? Someone please correct me if you have a better theory.
I'm a bit confused myself...
Conclusion: Either shorten the size of your environment (easiest) or shorten the command-line (not always easy).
--gv

Resources