32/64 bits Issue with psr.exe - c

I currently have an issue with psr.exe. Actually, it works perfectly with the following command :
C:\windows\system32\psr.exe
C:\windows\syswow64\psr.exe
psr.exe (with system32\cmd.exe and syswow64\cmd.exe).
BUT, it does not work when my program (a 32 bits program running on a 64 bits windows 7) use this bat to start the application. Nothing happens.
My programm can open a notepad from a bat files, or execute whatever you want, but it does not work with psr.exe.
Can you give me some tips? Maybe it is not a 32/64 bits compatibility issue but I have other no idea about why this does not works...
EDIT :
The batch file content is:
C:\windows\system32\psr.exe
The program running this bat is written in C, it uses this function to start it:
CreateProcessAsUser(
hTokenDup,
NULL,
"C:\\toto\\mybat.bat",
NULL,
NULL,
FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
lpEnvironment,
NULL,
&sInfo,
&ProcessInfo)
It works perfectly if the .bat file contains C:\windows\system32\notepad.exe instead of C:\windows\system32\psr.exe.
If I double click on my bat files containing C:\windows\system32\psr.exe from my desktop, it works perfectly, but from my C program, it does not work.
(NB : My c program works with other .exe files like notepad for example)
Many thanks.
Johan

If a 32-bit program running on 64-bit windows OS tries to access a file C:\windows\system32\psr.exe, Windows redirects the request to file C:\windows\syswow64\psr.exe.
To start a 64-bit system application from 32-bit environment you need to execute C:\windows\sysnative\psr.exe which will be redirected to "real" C:\windows\system32\psr.exe.
To run the bat-file entirely in a 64-bit environment, execute C:\windows\sysnative\cmd.exe with following parameters: /c "C:\toto\mybat.bat"
However, this does not appear to be a problem in your case.
Could it be that the user you are trying to impersonate has a corrupted profile, home folder, environment, or does not have some sort of privilege required by psr.exe application?
Or maybe the app requires user profile to be loaded but you didn't load it by calling LoadUserProfile() beforehand?
Try testing by running your batch file like this:
c:\windows\syswow64\runas.exe /noprofile /env /user:INSERT_USERNAME_HERE "C:\toto\mybat.bat"
Does it work? Now try removing /env and/or replacing /noprofile with /profile. Does it now?

Related

Starting the Command Promt from a program using 'system(...)'

If I start the Command Prompt the Standard" way, go to 'C:\Windows\System32' and do "dir m*.exe", I get "several screensful" of listing. Among others 'msg.exe' is there. And if I do 'msg /?' :
C:\WINDOWS\system32>msg /?
MSG {username | sessionname | sessionid | #filename | *}
[/SERVER:servername] [/TIME:seconds] [/V] [/W] [message]
e
tc.
And the 'msg' command works as expected.
But if I start the Command Prompt from a program, with the 'system(...)' function, i.e. like this (Console application in Visual Studio):
#include "stdafx.h"
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
system("cmd") ;
return 0;
}
'msg' does not work:
C:\WINDOWS\system32>msg /?
'msg' is not recognized as an internal or external command,
operable program or batch file.
And dir m*.exe in this Command Prompt gives only half a screen of listing. Especially, msg.exe is not there.
Somebody who can explain why? Somebody who knows a walk-around? Or (even better) how I can do 'msg' from a program? I.e.
system("msg * /server:undis "Directory 'expenses' synced") ;
I would embed it in a program run via "Program after / When changes are made" in SyncBack Pro (2BrightSparks).
As n.m. comments, most likely the problem is that you're building a 32-bit program so the cmd you run with system is the 32-bit cmd emulator and not the nomral win64 cmd program.
This is what leads to your problem -- whenever you try to access C:\WINDOWS\system32 in 32-bit mode, the system silently sends you to C:\WINDOWS\sysWOW64 instead. That's because on win64, system32 actually contains 64-bit programs and dlls, not 32-bit ones. The 32-bit ones are in sysWOW64.
Now normally this isn't an issue -- most of what you want in the 32-bit program is in sysWOW64, so everything is fine. The problem is that msg is not. So to get around that, you need to "escape" from the 32-bit sandbox. To do that, you need to go to C:\WINDOWS\sysnative. This directory (which is only available/visible in 32-bit mode) silently redirects to the (real, 64-bit) C:\WINDOWS\system32 directory. If you cd to that directory, you should see msg.exe and be able to run it. Or just run it directly from your system command:
system("C:\\WINDOWS\\sysnative\\msg * /server:undis "Directory 'expenses' synced") ;

Is it possible to embed an executable inside a batch file?

There are many programs which help me write more efficient code (NirCmd etc.), but they can't run properly if they aren't installed in the computer. So is there a way to, for example, in the temp folder, extract the program from a batch program and use it.
I tried reading the executables with a hex editor, putting the hex code into another file and saving it as an executable. But this failed. So, is there any way to efficiently store an executable inside a batch file, create it and then run it?
Yes, there is one that I know of and works perfectly.
It is a program called bhx.exe (link to its site here).
It can also embed other file types.
The usage is quite simple:
(optional) Create a cabinet (.cab) file from the original .exe using this command: makecab yourexe.exe yourexe.cab. For better compression you can use the /D switch in this way: makecab /D CompressionType=LZX yourexe.exe yourexe.cab
CD to the directory bhx is in and do this: bhx yourexe.cab. Other switches are described in the website.
There you go, the mybin.cmd file is generated.

StartService error 2

I've written a very simple batch file that bluescreens windows 7 (ultimate,enterprise,home) beyond normal repair, by making an attempt of loading a Win32 exe-file (the well known notepad.exe) into the operating system's kernel. Feel free to test it in a Virtual Machine, just open it as an administrator. Do NOT run this as administrator on your primary computer.
#echo off
sc create bluescreener binpath = C:/Windows/System32/notepad.exe type= filesys start= boot
sc start bluescreener
timeout /t 30
However: When I run this program on windows 10, it successfully creates the service, but it doesn't start. Instead it returns the following error:
[SC] StartService Failed 2: The system cannot find the file specified.
It is referring to the binpath I set in the batch file. I am sure this path exists, and that this is the exact path name. I don't understand why it can't find the file. I have tried using backslashes instead of forward-slashes, and that didn't work either. (I have forward-slashes because I have a python program that writes this batch file, and the line break in python uses a backslash, and that conflicts with the path.)
I tried the "sc qc bluescreener" test that shows information about the service I have created. This is what comes up. (It is the same thing that comes up when I test it on windows 7, where it works.)
[SC] QueryServiceConfig SUCCESS
SERVICE_NAME: bluescreener
TYPE : 2 FILE_SYSTEM_DRIVER
START_TYPE : 0 BOOT_START
ERROR_CONTROL : 1 NORMAL
BINARY_PATH_NAME : \SystemRoot\System32\notepad.exe
LOAD_ORDER_GROUP :
TAG : 0
DISPLAY_NAME : bluescreener
DEPENDENCIES :
SERVICE_START_NAME :
I can't find any explanation for why it can't find the file.
When I try to reboot windows 10 after creating the service, it can't boot, which means that the service is probably working and that the file was located. I just want to know why Windows 7 can find the file and immediately bluescreens, while Windows 10 can't find the file and needs to be rebooted for the service to run.
Any suggestions on how I might reproduce the same effect i get on Windows 7, but on Windows 10?

Running a shell script through Cygwin on Windows

I have a bunch of shell scripts that used to run on a Linux machine. Now, we've switched over to Windows, and I need to run these scripts there. I have Cygwin installed, but is there a way to make the script run using Cygwin, but the call is made from Windows batch?
Sure. On my (pretty vanilla) Cygwin setup, bash is in c:\cygwin\bin so I can run a bash script (say testit.sh) from a Windows batch file using a command like:
C:\cygwin\bin\bash testit.sh
... which can be included in a .bat file as easily as it can be typed at the command line, and with the same effect.
One more thing - if You edited the shell script in some Windows text editor, which produces the \r\n line-endings, cygwin's bash wouldn't accept those \r. Just run dos2unix testit.sh before executing the script:
C:\cygwin\bin\dos2unix testit.sh
C:\cygwin\bin\bash testit.sh
If you have access to the Notepad++ editor on Windows there is a feature that allows you to easily get around this problem:
Open the file that's giving the error in Notepad++.
Go under the "Edit" Menu and choose "EOL Conversion"
There is an option there for "UNIX/OSX Format." Choose that option.
Re-save the file.
I did this and it solved my problems.
Hope this helps!
Read more at http://danieladeniji.wordpress.com/2013/03/07/microsoft-windows-cygwin-error-r-command-not-found/
Just wanted to add that you can do this to apply dos2unix fix for all files under a directory, as it saved me heaps of time when we had to 'fix' a bunch of our scripts.
find . -type f -exec dos2unix.exe {} \;
I'd do it as a comment to Roman's answer, but I don't have access to commenting yet.
The existing answers all seem to run this script in a DOS console window.
This may be acceptable, but for example means that colour codes (changing text colour) don't work but instead get printed out as they are:
there is no item "[032mGroovy[0m"
I found this solution some time ago, so I'm not sure whether mintty.exe is a standard Cygwin utility or whether you have to run the setup program to get it, but I run like this:
D:\apps\cygwin64\bin\mintty.exe -i /Cygwin-Terminal.ico bash.exe .\myShellScript.sh
... this causes the script to run in a Cygwin BASH console instead of a Windows DOS console.
If you don't mind always including .sh on the script file name, then you can keep the same script for Cygwin and Unix (Macbook).
To illustrate:
1. Always include .sh to your script file name, e.g., test1.sh
2. test1.sh looks like the following as an example:
#!/bin/bash
echo '$0 = ' $0
echo '$1 = ' $1
filepath=$1
3. On Windows with Cygwin, you type "test1.sh" to run
4. On a Unix, you also type "test1.sh" to run
Note: On Windows, you need to use the file explorer to do following once:
1. Open the file explorer
2. Right-click on a file with .sh extension, like test1.sh
3. Open with... -> Select sh.exe
After this, your Windows 10 remembers to execute all .sh files with sh.exe.
Note: Using this method, you do not need to prepend your script file name with bash to run

How do we write a program in Command line development environment?

I have been writing my code in IDE,I just read that there also existed a Command Line Development Environment in which the code is written in DOS.I googled but found no results on how to use the command line development environment.My OS is Windows XP.I would be very thankful for your help me write the hello world program in DOS and also explain how to run it.
You simply use whatever text editor you like to create the C sourse file(s) then invoke the compiler command line(s) to compile and link the program (typically, an IDE is doing exactly that, but in a behind-the-scene manner). How the command line is invoked depends on the exact toolchain you're using.
You might also need to set up an environment for you particular compiler toolchain (the right paths and various other env variables might need set up).
For Visual C++ the environment might be set up using a batch file installed by Visual Studio:
vcvarsall x86
Invoking the compiler could be as simple as:
cl helloworld.c
or for C++ (for some reason it issues a non-fatal warning if you don't give it an option configuring details about how it should implement exceptions):
cl /EHsc helloworld.cpp
The particulars are very dependent on the compiler you're using - you should read the docs for that compiler.
Also, the options you use depend on your particular situation and needs. Scripts/batch files and/or makefile can help you manage the complexity of the options you might need to use.
DOS is not dead.... yet!
fahad
There are a number of methods by which you can enter code in DOS (see EDIT further on down).
(1) You can send keystrokes directly to a file
You do this by redirecting output to CON (the console) to a file. The only oddity of this method is that you end the 'session' by entering a CTRL-Z when you are finished.
It's basic, but this is how it goes.
Firstly, suppose you want to display "Hello World" on the screen, a simple batch file containing the following two lines is all that is required:
#echo off
echo Hello World
The '#echo off' is commonly found at the start of all batch files. It simply instructs the command interpretter NOT to display each command as it is being executed (or parsed).
One more thing before we start. Throughout this answer, I will assume your program is named 'helloworld.bat'.
Enter the following lines one after the other pressing the ENTER key at the end of each line:
copy con helloworld.bat
#echo off
echo Hello World
^Z
The '^Z' is displayed when you press the CTRL-Z key combination (don't forget to press the ENTER key as well).
When you press the ENTER key after CTRL-Z, DOS displays the familiar '1 File(s) copied' messege.
You can now execute the batch file program by simply entering the program's name like this:
helloworld
And DOS will display the following:
Hello World
It can't get any more basic than that.
(2) You can use DOS' EDIT program
This is a DOS based IDE retained from around the mid-90's. Simply enter the following command:
edit
And EDIT will open in the same DOS window. When you close EDIT, you are returned back to DOS again.
EDIT also works with your mouse.
Once EDIT opens, enter the following two lines of code:
#echo off
echo Hello World
Then, click on [File], [Save], type: 'helloworld.bat' in the "File Name" input field, use your mouse to change directories in the "Directories:" pane if you want to, then click [OK]. To return to DOS, click [File], [Exit].
EDIT version 4.5 (I think) was context-sensitive and displayed code using different colours to seperate key word, different data type, symbols etc.
(3) Use Windows' built-in Notepad
This is simple. At the command prompt, enter the following command:
notepad
And Notepad will fire up. It's a simple text editor and does the job when entering small programs.
(4) Use Notepad++. It's FREE!!
Notepad++ is the programmer's choice. It's free, full of useful features and customisable. Find it on the net by searching for "notepad++".
From your comment, "Just some knowledge so I can say that I know one way to do programming without IDE" I would say learn to write simple batch files. They can be run from Explorer but they exist as a holdover from the DOS days.
Start a command prompt window (Start->Run->'cmd'), this will open a window and show a prompt, most likely "c:\" or some other path.
Type the following command (followed by )
echo "Hello World"
You should see:
"Hello World"
c:\
Now, using whatever editor you'd like, create a text file with that command as the only line. Name the file "hello.bat". When you are at the command prompt you can execute the batch file like so:
c:\hello.bat
"Hello World"
c:\
You have now programmed using the DOS command line. For more commands and such, start with the help system.
c:\help
Which will display all the available commands for your batch file.
Microsoft has an online reference here.
DOS is dead for all practical purposes. Under Windows your options boil down to the following:
Use an IDE. Visual Studio is one example, Qt another. You can write programs for the commandline with an IDE.
Use a proper text editor, build tool and other helper tools. You might use gvim for editing code, make for building your project and git for version control. You might as well use the GNU coreutils for other helpers, or maybe even the entire cygwin package.
Bro, use gcc compiler for which write ur code in any text editor then compile ur code in windows shell or u can say command line environment.
Look!
Prompt:/> gcc source_file_name.c
This command compiles ur code,
If there is any error, u will get dispalyed with line numbers,
now, every program creates its exe file by the name a.exe by default.
To, get the output of ur program,
Prompt:/> a.exe
O/P
Hello World!
To change the name of the exe file there is also a command..

Resources