Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3214034
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:00:47+00:00 2026-05-17T15:00:47+00:00

EDIT I added in some error handling to my .vbs file and it is

  • 0

EDIT

I added in some error handling to my .vbs file and it is indeed a permissions issue (I now get a “Permission Denied error”). However, supplying my credentials in the web.config <impersonate> tag does not seem to have any effect.

Also when trying to supply my credentials to the process via via

p.StartInfo.Password = Misc.CreateSecurityString("password");
p.StartInfo.UserName = "admin";

I get an new error:

cscript.exe – Application error

The application failed to initialize
properly (0xc0000142). Click on OK to
terminate the application.

Shout it out if you know what’s causing this. (Or just type it…)

Thanks for your help so far!


Background

I’m trying to execute a .vbs file from an custom handler (.ashx). The VBScript is setting up a web application in iis 5.1.

So far the following code executes without errors

string sToRun = "C:\CreateIISApplication.vbs" 
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cscript";
p.StartInfo.Arguments = sToRun;
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string sOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();

Problem

My problem is that the vbscript appears to not have run at all. When I check IIS, my application is not created.

When I run the script file directly from the command prompt everything works correctly and my application shows up in IIS.

Trouble Shooting

I decided to add some echo statements into the .vbs file so I could make sure it was running. On the command line, all the statements are outputted correctly. When checking the string sOutput I get the header message, but none of my subsequent messages.

From C# – contents of sOutput

Microsoft (R) Windows Script Host
Version 5.7 Copyright (C) Microsoft
Corporation. All rights reserved

From the Command Line

Microsoft (R) Windows Script Host
Version 5.7 Copyright (C) Microsoft
Corporation. All rights reserved

Hello

So I can prove (I think) that the .vbs file is not being evaluated, and cscript is being called. And if I call cscript without referencing a .vbs file, then I get the help documentation. So something is going wrong.

Any ideas? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:00:48+00:00Added an answer on May 17, 2026 at 3:00 pm

    Turns out I needed to skip using System.Diagnostics.Process and use kernel32.dll and advapi32.dll methods.

    “This is because in ASP.NET,
    impersonation is performed at the
    thread level and not at the process
    level.” source

    Also needed make my Anonymous Access account a member of “Replace a process level token” Control Panel -> Administrative Tools -> Local Security Settings. (You’ll need to restart for this to take effect.

    Here’s the adapted code from MSDN (http://support.microsoft.com/kb/889251).

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    using Microsoft.Win32;
    using System.IO;
    using System.Security.Principal;
    
    namespace UtilityLib
    {
        public class Win32Process
        {
            [StructLayout(LayoutKind.Sequential)]
            public struct STARTUPINFO
            {
                public int cb;
                public String lpReserved;
                public String lpDesktop;
                public String lpTitle;
                public uint dwX;
                public uint dwY;
                public uint dwXSize;
                public uint dwYSize;
                public uint dwXCountChars;
                public uint dwYCountChars;
                public uint dwFillAttribute;
                public uint dwFlags;
                public short wShowWindow;
                public short cbReserved2;
                public IntPtr lpReserved2;
                public IntPtr hStdInput;
                public IntPtr hStdOutput;
                public IntPtr hStdError;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct PROCESS_INFORMATION
            {
                public IntPtr hProcess;
                public IntPtr hThread;
                public uint dwProcessId;
                public uint dwThreadId;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct SECURITY_ATTRIBUTES
            {
                public int Length;
                public IntPtr lpSecurityDescriptor;
                public bool bInheritHandle;
            }
    
            [DllImport("kernel32.dll", EntryPoint = "CloseHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
            public extern static bool CloseHandle(IntPtr handle);
    
            [DllImport("advapi32.dll", EntryPoint = "CreateProcessAsUser", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public extern static bool CreateProcessAsUser(IntPtr hToken, String lpApplicationName, String lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,
                ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandle, int dwCreationFlags, IntPtr lpEnvironment,
                String lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
    
            [DllImport("advapi32.dll", EntryPoint = "DuplicateTokenEx")]
            public extern static bool DuplicateTokenEx(IntPtr ExistingTokenHandle, uint dwDesiredAccess,
                ref SECURITY_ATTRIBUTES lpThreadAttributes, int TokenType,
                int ImpersonationLevel, ref IntPtr DuplicateTokenHandle);
    
    
            public static void CreateProcess(string cmdline)
            {
                IntPtr Token = new IntPtr(0);
                IntPtr DupedToken = new IntPtr(0);
                bool      ret;
                //Label2.Text+=WindowsIdentity.GetCurrent().Name.ToString();
    
    
                SECURITY_ATTRIBUTES sa  = new SECURITY_ATTRIBUTES();
                sa.bInheritHandle       = false;
                sa.Length               = Marshal.SizeOf(sa);
                sa.lpSecurityDescriptor = (IntPtr)0;
    
                Token = WindowsIdentity.GetCurrent().Token;
    
                const uint GENERIC_ALL = 0x10000000;
    
                const int SecurityImpersonation = 2;
                const int TokenType = 1;
    
                ret = DuplicateTokenEx(Token, GENERIC_ALL, ref sa, SecurityImpersonation, TokenType, ref DupedToken);
    
                if (ret == false)
                {
                    throw new Exception("DuplicateTokenEx failed with " + Marshal.GetLastWin32Error());
                }
    
    
                STARTUPINFO si          = new STARTUPINFO();
                si.cb                   = Marshal.SizeOf(si);
                si.lpDesktop            = "";
    
                string commandLinePath = cmdline;
    
                PROCESS_INFORMATION pi  = new PROCESS_INFORMATION();
                ret = CreateProcessAsUser(DupedToken,null,commandLinePath, ref sa, ref sa, false, 0, (IntPtr)0, "c:\\", ref si, out pi);
    
                if (ret == false)
                {
                    throw new Exception("CreateProcessAsUser failed with " + Marshal.GetLastWin32Error() + ": if 1314, make sure user is a member 'Replace a process level token' Control Panel -> Administrative Tools -> Local Security Settings.");
                }
                else
                {
                    CloseHandle(pi.hProcess);
                    CloseHandle(pi.hThread);
                }
    
                ret = CloseHandle(DupedToken);
                if (ret == false)
                {
                    throw new Exception(Marshal.GetLastWin32Error().ToString());
                }
    
            }
    
        }
    }
    

    Using it is simple:

    string sToRun = @"cscript C:\CreateIISApplication.vbs"; //OR C:\myfile.bat arguments, or whatever else you want to run. 
    
    Win32Process.CreateProcess(sToRun);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: You can get the full source here: http://pastebin.com/m26693 Edit again: I added some
I was making an edit to a long existing project. Specifically I added some
EDIT: I have added Slug column to address performance issues on specific record selection.
EDIT: Modified title and added update. UPDATE : We no longer believe this is
in C# I'd like to invoke the label edit of a newly added item
With several forms of mine, I occasionally run into the following issue: I edit
We are using ASP.NET MVC with LINQ to SQL. We added some features and
Edit: This question was written in 2008, which was like 3 internet ages ago.
Edit: From another question I provided an answer that has links to a lot
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.