CreateProcess to run VBS Script

Hello All,

I have a createprocess that runs a bat file which then runs a vbs script. Although it would appear when I run it that all is good it does not edit the registry. When its run it asks me for a username, password, and domain for conducting autologins on reboot. However, when it reboots it still asks for credentials. Here is the kicker, if I run the bat file manually and go through the motions it works perfectly fine. Does anyone have any ideas as to what's going on? Both ways of exectuting it appears the vbs runs properly but through my program it does not edit the registry.

1
2
3
4
5
6
7
8
if(LOGINFlag.State == Unchecked){                          
CreateProcess(TEXT("E:\\Programs\\LogonScript\\Enable.bat"),
	                          NULL,NULL,NULL,FALSE,
                                         CREATE_NEW_CONSOLE,
                                         NULL,NULL,&si,&pi);                                                                        						  
				  WaitForSingleObject( pi.hProcess, INFINITE );
						   LOGINFlag.State = Checked;    
                                   }
Last edited on
Ok well there haven't been any replies but I am hoping maybe this new code can shed some light on it. Again the VBS will run, ask for username, password, and domain but it will not write to the registry. It will only do that if I run it manually.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
BOOL    result;
LPTSTR cmdname = "C:\\Windows\\System32\\wscript.exe";
LPTSTR cmdargs = "/C E:\\Programs\\LogonScript\\AutoLogonEnable.bat";
if(LOGINFlag.State == Unchecked){
result = CreateProcess(cmdname,cmdargs,
		       NULL,NULL,FALSE,
		       CREATE_NEW_CONSOLE,
		       NULL,NULL,&si,&pi);
WaitForSingleObject( pi.hProcess, INFINITE );
LOGINFlag.State = Checked;

if(result == 0){
	DWORD Error = GetLastError();
 	LPVOID lpMsgBuf;FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,NULL,Error,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
 	(LPTSTR) &lpMsgBuf,0,NULL);
 							
 		MessageBox(NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
 							
 			LocalFree( lpMsgBuf );
 		}   
            }       


For some reason i would be led to believe it is some kind of permission issue. However I have run system() and ShellExecute() and neither of those have worked either. I origionally had a bat file open the vbs and I experimented with the runas command and it still did not cause problems. I do not get any errors when running this.
Last edited on
ShellExecute will work fine. Please fill in the correct working directory, do NOT pass NULL !

ShellExecute is used internally by windows explorer itself.


Do not comment on the fact that why the hell you mix C++, batch and vbs scripts in the same program when C++ can do it all.
ShellExecute will not work and I have found out why. There are permission issues for editing that part of the registry so now I am attempting to find out how to embed a manifest within the program which is fun all in itself.
Pass "runas" verb to ShellExecute if you need administrator privileges.


As for embedding a manifest, use Visual Studio option or some tools like Resource Hacker :)
http://msdn.microsoft.com/en-us/library/bb384691.aspx
http://www.codeproject.com/Articles/17968/Making-Your-Application-UAC-Aware
I will give that a shot, if i can't make it work by running this VBScript I think I am screwed. From my understanding if I edit the registry through my program it will not edit in the same area as the vbs because this is a 32 bit program. I am not a registry expert so I don't know if that is still doable or not but I guess I am going to exhaust all of my options first.

*EDIT* As for the manifest, I actually ditched Dev C++ and went with Visual Studio Express 2013 and was able to create a manifest but it will not push the elevated privileges to the vbs :(
Last edited on
So the application is intended to run on 64 bit windows ? If so create a X64 executable that does exactly that: write to the registry. You can detect from your main 32 bit application if you are running on 64 bit by calling IsWow64Process() API and then and only then spawn your 64 bit binary.
Yeah it runs on Windows 7 64 bit and Windows 8.1. Well from what I am reading Visual Studio Express 2013 is not able to create a 64 bit application, although I could be wrong. As for the ShellExecute I had created the following code but with two problems likely due to something I am doing wrong. 1. A command line popup comes up quickly and dissappears so it does not appear to run properly. 2. Is there some reason why the waitforsingleobject does not work properly on this? It just skips to the next instance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SHELLEXECUTEINFO shExecInfo;

				shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);

				shExecInfo.fMask = NULL;
				shExecInfo.hwnd = NULL;
				shExecInfo.lpVerb = "runas";
				shExecInfo.lpFile = "C:\\Windows\\System32\\cscript.exe";
				shExecInfo.lpParameters = NULL;
				shExecInfo.lpDirectory = "E:\\Programs\\LogonScript\\AutoLogonEnable.vbs";
				shExecInfo.nShow = SW_MAXIMIZE;
				shExecInfo.hInstApp = NULL;

				ShellExecuteEx(&shExecInfo);
				
				WaitForSingleObject(pi.hProcess, INFINITE);


*EDIT* Disregard the WaitForSingleObject issue, I figured that one out (head slap)
Last edited on
If you don't want to write a 64 bit EXE then disable registry redirection while running under WOW64. See this article for how to do t:
http://www.codeproject.com/Articles/14200/Registry-Redirector-in-x-IA


And please do not use that vbs script anymore.
I figured you might find this interesting. so I was researching the link you had given me and out of curiousity I wanted to look at the default place it stores keys for 32 bit applications and low and behold I find the autoadmin login registry keys. It turns out that because I was running a 32 bit application to start this vbs that it would store those credentials in the Wow6432Node. so this whole time it was storing the values, just not where I was expecting them to store.
Btw even though it was writing to Wow6432Node it would not reboot automatically. I was able to find out how to write to the registry without doing a redirect. I had to use the KEY_WOW64_64KEY command, to easy!
Topic archived. No new replies allowed.