Why isn't this code running my hello world c executable
#include <windows.h>
#include <string>
int main()
{
// path for hello world compiled program
std::string app_path = "C:\\Users\\test\\Desktop\\a.exe";
BOOL inherit_handles = true;
PROCESS_INFORMATION pi;
STARTUPINFO si;
CreateProcessA(app_path.c_str(),
NULL,
NULL,
NULL,
inherit_handles,
CREATE_NEW_CONSOLE|CREATE_PROTECTED_PROCESS|DETACHED_PROCESS,
0,
NULL,
&si,
&pi);
return 0;
}
I am clueless to why not output is given, even when I use > out.txt, although I do see some cpu usage spikes in process hacker
I also tried to use calc.exe instead of a.exe but this also didn't help
STARTUPINFO needs to be initialized:
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
Don't inherit handles unless you need to.
Don't use CREATE_PROTECTED_PROCESS.
Don't use DETACHED_PROCESS unless you want to hide stdout.
Check the return value of CreateProcess!
Related
I am using VSCode 0.10.6 for C++ programming. I am trying to launch a program prior to debugging. The program, OpenOCD, is what GDB connects to. If I manually open and close it through a terminal, it works fine, but it seems like there should be an easy way to get VSCode to just start it for me.
I have played with tasks.json and it appears you need to use some ugly bat/sh files to accomplish this in combination with preLaunchTasks in launch.json.
Currently the answer is that you must indeed use preLaunchTasks to have a chance of making this work. I would have been happy to use an ugly script, if that indeed would work - but it doesn't. In my case, I needed one or more executables to be ran in the background, allowing VSCode to continue into debugging.
Unfortunately, each executable I tried to launch (via start) was not actually running as a "detached" process, and so VSCode would wait for each executable to finish running before it would finish the preLaunchTasks and start debugging. Not what I wanted.
I found an article by someone having a similar "detached process" problem with subversion, and I used his C++ code to solve this same issue with Visual Studio Code. I found a bug or two in that code, which I fixed. Here is what I'm currently using:
// dstart.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
//http://stackoverflow.com/questions/1536205/running-another-program-in-windows-bat-file-and-not-create-child-process
//http://svn.haxx.se/users/archive-2008-11/0301.shtml
int _tmain()
{
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms683156(v=vs.85).aspx
LPWSTR pCmd = ::GetCommandLine();
// skip the executable
if (*pCmd++ == L'"')
while (*pCmd++ != L'"');
else
while (*pCmd != NULL && *pCmd != L' ') ++pCmd;
while (*pCmd == L' ') pCmd++;
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
BOOL result = CreateProcess
(
NULL, // No module name (use command line)
pCmd, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set bInheritHandles to FALSE
CREATE_NEW_CONSOLE, // Detach process
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure (returned)
);
if (result) return 0;
wchar_t msg[2048];
FormatMessage
(
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
msg, sizeof(msg),
NULL
);
fputws(msg, stderr);
_flushall();
return -1;
}
Once compiled, you can use it similarly to how the start command works at a DOS prompt. Place that in the script you attach to your preLaunchTasks
in Visual Studio Code.
I am trying to run a batch script within visual studio 2010. I am following the code found here as well as the MSDN document.
I have the running.bat file I created in both the Release and Debug folders when my .exe is created (both methods of compiling fail during runtime). However my program crashes with error code 2 every time:
main.c - a command line application
#include "windows.h"
#include "stdio.h"
int main()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
if( !CreateProcess(NULL,
L"cmd.exe /c running.bat",
NULL,
NULL,
TRUE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi )
)
{
printf( "CreateProcess failed (%d)\n", GetLastError() );
return FALSE;
}
return 0;
}
STARTUPINFO is an In parameter for the CreateProcess function, but in your code is passing in junk.
STARTUPINFO si = {0};
si.cb = sizeof(STARTUPINFO);
etc...
From the MSDN Documentation:
Important The caller is responsible for ensuring that the standard handle fields in STARTUPINFO contain valid handle values. These fields are copied unchanged to the child process without validation, even when the dwFlags member specifies STARTF_USESTDHANDLES. Incorrect values can cause the child process to misbehave or crash.
As for the Exit Code 2, Add the full path of the batch file to the command. You can retrieve your application's current directory using args or GetModuleFileName.
I am new to VC++
I build a simple Win32 Application via default templet.
when i run it it shows a normal window with a menubar.
I added a system call that does a curl operation in InitInstance function:
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
//EDITED THIS WILL CREATE A FILE.HTML
system("curl www.google.co.in > file.html");
return TRUE;
}
all works fine.
But the problem is when ever i call system it opens in console window.
I want it to be a hidden process that does not shows that command prompt window..
help me with this.
The system function always shows a console window. If you want a hidden console window, you need to call CreateProcess directly. You can then ask for a DETACHED_PROCESS which runs without a console. Note that CreateProcess does not wait for the process to exit, so you will need to perform the wait yourself.
I put code in separate function, with url and filename as parameters.
#include <tchar.h>
#include <windows.h>
BOOL DownloadWithoutConsole(TCHAR* pszURI, TCHAR* pszFileName)
{
//here we will calculate our command line length in bytes
DWORD dwLen = (lstrlen(pszURI) + lstrlen(pszFileName) + 20)*sizeof(TCHAR);
//memory allocation for the command line
TCHAR* pszCmdLine = (TCHAR*)HeapAlloc(GetProcessHeap(), 0, dwLen);
ZeroMemory(pszCmdLine, dwLen);
//making command line
_stprintf_s(pszCmdLine,dwLen/sizeof(TCHAR), TEXT("cmd /c curl %s > %s"), pszURI, pszFileName);
//preparing arguments for CreateProcess
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
//creating new process
BOOL bResult = CreateProcess(NULL, pszCmdLine, NULL, NULL, FALSE, CREATE_NO_WINDOW,
NULL, NULL, &si, &pi);
if(bResult)
{
//waiting for process termination
WaitForSingleObject(pi.hProcess, INFINITE);
//cleanup
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
//freeing memory
HeapFree(GetProcessHeap(), 0, pszCmdLine);
return(bResult);
}
int __cdecl _tmain(void)
{
DownloadWithoutConsole(TEXT("stackoverflow.com"), TEXT("test.txt"));
return(0);
}
Set nCmdShow to SW_HIDE(0) (ShowWindow). This way u will run it without he cmd prompt
#include <windows.h>
#include <stdio.h>
#include <Userenv.h>
#include <Wtsapi32.h>
int main() {
DWORD err;
err=GetLastError();
printf( "err001:%d\n",err);
HANDLE hTokenThis = NULL;
HANDLE hTokenDup = NULL;
HANDLE hThisProcess = GetCurrentProcess();
OpenProcessToken(hThisProcess, TOKEN_ALL_ACCESS, &hTokenThis);
err=GetLastError();
printf( "err002:%d\n",err);
DuplicateTokenEx(hTokenThis, MAXIMUM_ALLOWED,NULL, SecurityIdentification, TokenPrimary, &hTokenDup);
err=GetLastError();
printf( "err003:%d\n",err);
DWORD dwSessionId = WTSGetActiveConsoleSessionId();
WTSQueryUserToken(dwSessionId, hTokenDup);
//DWORD dwSessionId = 1;
SetTokenInformation(hTokenDup, TokenSessionId, &dwSessionId, sizeof(DWORD));
err=GetLastError();
printf( "err004:%d\n",err);
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(STARTUPINFO));
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
si.cb = sizeof(STARTUPINFO);
si.lpDesktop = "WinSta0\\Default";
LPVOID pEnv = NULL;
DWORD dwCreationFlag = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE;
CreateEnvironmentBlock(&pEnv, hTokenDup, FALSE);
err=GetLastError();
printf( "err005:%d\n",err);
CreateProcessAsUser(
hTokenDup,
NULL,
(char *)"notepad",
NULL,
NULL,
FALSE,
dwCreationFlag,
pEnv,
NULL,
&si,
&pi);
printf("here we go\n");
err=GetLastError();
printf( "err006:%d\n",err);
return 0;
}
Compile:
gcc -o session.exe session.c c://Windows/System32/kernel32.dll c://Window
s/System32/wtsapi32.dll -lUserenv
Running Result:
session.exe
err001:126
err002:126
err003:126
err004:1314
err005:203
here we go
err006:87
gcc version 4.5.2 (GCC) from mingw.
btw, just ignore the error 126.
My question is :
Why got error 1314?
I want to start a program in the interactive desktop from service by using CreateProcessAsUser without knowing the logon user and password.
Error 1314 is "A required privilege is not held by the client".
From the WTSQueryUserToken() docs (http://msdn.microsoft.com/en-us/library/aa383840.aspx):
To call this function successfully, the calling application must be running within the context of the LocalSystem account and have the SE_TCB_NAME privilege
Also your call to WTSQueryUserToken() should look like:
WTSQueryUserToken(dwSessionId, &hTokenDup);
And you'll need appropriate privileges for SetTokenInformation() enabled as well.
Bottom line is that you're trying to do something that Windows reserves for highly privileged processes, so you'll need to make sure your process is configured to run appropriately (maybe as a service that talks to a regular non-privileged process for user interaction).
I'm trying to install my ActiveX plugin, packaged in nsi in a cab file, and encountered a problem.
The log is
Code Download Error: (hr = 80070005) Access is denied.
ERR: Run Setup Hook: Failed Error Code:(hr) = 80070005, processing: msiexec.exe /package "%EXTRACT_DIR%\TempR.msi"
I think is the same as this one:
http://social.msdn.microsoft.com/Forums/en-US/ieextensiondevelopment/thread/3d355fb6-8d6a-4177-98c2-a25665510727/
I want to try the solution that is suggested there, but has no idea how to
create a small bootstrap EXE, which
does nothing but to launch MSIEXEC.EXE
and then wait for its completion.
Can someone provide any help?
Thanks!!
Here is a simple wrapper that calls msiexec.exe to quietly install the msi passed in the first command line parameter.
It's written as a Visual C++ command line application:
// InstallMSI.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#include <string>
int wmain(int argc, wchar_t* argv[])
{
if(argc < 2) {
printf("Usage: installmsi.exe <full path to msi file>\n\n");
printf("Package will be installed using msiexec.exe with the /qn (quiet install) flags.\n");
return 1;
}
std::wstring args;
args = L"msiexec.exe /i \"";
args += argv[1];
args += L"\" /qn";
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if(!CreateProcess(NULL, (LPWSTR)args.c_str(),
NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi)) {
printf("CreateProcess failed (%d).\n", GetLastError());
return 2;
}
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
Hope that helps.
Take a look at dotNetInstaller - prewritten bootstrapper program that does lots more than what you need, but can do just exactly what you're asking.