Calling CreateProcessWithLogonW

Discussion in 'ASP .Net' started by Benjamin Bittner, Jul 1, 2004.

  1. Hallo NG,
    I have problems with calling the CreateProcessWithLogonW() function. I tried
    converting an VB6 example
    (http://support.microsoft.com/default.aspx?scid=kb;en-us;285879) and some
    snippets i found around the web. but i cant get it to work. It tries to
    start the application, but it fails. In my eventlog i can see a new info
    message, everytime i execute my code. i try to translate it, cause i have a
    german windows installed, so it maybe wont be the exact words of the
    original english info message.

    Application popup: some.exe - Error in application: the application couldnt
    be initialized properly (0xc0000142). Click "OK" to exit.
    event-id: 26

    I tried launching notepad.exe, cmd.exe, cscript.exe but everytime the same
    error. What im tryin to achieve is, to execute the vbscript file
    makew3site.vbs from IIS > AdminScripts.

    This is my code:

    Option Strict Off
    Option Explicit On
    Imports System.Runtime.InteropServices

    Public Class WebForm2
    Inherits System.Web.UI.Page

    Protected WithEvents lblTest As System.Web.UI.WebControls.Label

    #Region "Structs"
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure PROCESS_INFORMATION
    Dim hProcess As System.IntPtr
    Dim hThread As System.IntPtr
    Dim dwProcessId As Integer
    Dim dwThreadId As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure STARTUPINFO
    Dim cb As Integer
    Dim lpReserved As System.IntPtr
    Dim lpDesktop As System.IntPtr
    Dim lpTitle As System.IntPtr
    Dim dwX As Integer
    Dim dwY As Integer
    Dim dwXSize As Integer
    Dim dwYSize As Integer
    Dim dwXCountChars As Integer
    Dim dwYCountChars As Integer
    Dim dwFillAttribute As Integer
    Dim dwFlags As Integer
    Dim wShowWindow As Short
    Dim cbReserved2 As Short
    Dim lpReserved2 As System.IntPtr
    Dim hStdInput As System.IntPtr
    Dim hStdOutput As System.IntPtr
    Dim hStdError As System.IntPtr
    End Structure

    #End Region

    #Region "APIINFO"
    Private Const LOGON_NETCREDENTIALS_ONLY As Integer = &H2
    Private Const NORMAL_PRIORITY_CLASS As Integer = &H20
    Private Const CREATE_DEFAULT_ERROR_MODE As Integer = &H4000000
    Private Const CREATE_NEW_CONSOLE As Integer = &H10
    Private Const CREATE_NEW_PROCESS_GROUP As Integer = &H200
    Private Const LOGON_WITH_PROFILE As Integer = &H1

    Private Declare Unicode Function CreateProcessWithLogon Lib "Advapi32"
    Alias "CreateProcessWithLogonW" _
    (ByVal lpUsername As String, _
    ByVal lpDomain As String, _
    ByVal lpPassword As String, _
    ByVal dwLogonFlags As Integer, _
    ByVal lpApplicationName As String, _
    ByVal lpCommandLine As String, _
    ByVal dwCreationFlags As Integer, _
    ByVal lpEnvironment As System.IntPtr, _
    ByVal lpCurrentDirectory As System.IntPtr, _
    ByRef lpStartupInfo As STARTUPINFO, _
    ByRef lpProcessInfo As PROCESS_INFORMATION) As Integer

    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As
    System.IntPtr) As Integer
    #End Region

    #Region " Vom Web Form Designer generierter Code "

    'Dieser Aufruf ist für den Web Form-Designer erforderlich.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: Diese Methode ist für den Web Form-Designer erforderlich
    'Verwenden Sie nicht den Code-Editor zur Bearbeitung.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    Dim szApp As String = "C:\winnt\notepad.exe"
    Dim szCmdLine As String = ""
    Dim szUser As String = "user"
    Dim szPass As String = "password"
    Dim szDomain As String = "TEST"
    Dim siStartup As STARTUPINFO
    Dim piProcess As PROCESS_INFORMATION

    siStartup.cb = Marshal.SizeOf(siStartup)
    siStartup.dwFlags = 0

    Dim ret As Integer = CreateProcessWithLogon(szUser, szDomain,
    szPass, LOGON_WITH_PROFILE, szApp, szCmdLine, _
    CREATE_DEFAULT_ERROR_MODE, _
    IntPtr.Zero, IntPtr.Zero, siStartup, piProcess)

    If ret = 0 Then
    lblTest.Text = New
    System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()).Message
    End If

    CloseHandle(piProcess.hProcess)
    CloseHandle(piProcess.hThread)
    End Sub

    End Class

    I tried many combinations for szApp and szCmdLine, like tryin to give szApp
    a reference to the exe, and szCmdLine the arguments, or leaving szApp empty
    and passing everything woth szCmdLine like this:

    Dim szCmdLine As String = "C:\Winnt\notepad.exe /put params here"

    Also i tried to pass 0& as an integer in szApp, and szCmdLine like the above
    one.
    What i want to do is:

    Dim szApp As String = "C:\Inetpub\AdminScripts\mkw3site.vbs"
    Dim szCmdLine As String = "-r C:\Inetpub\wwwroot\client -t client.test.de -o
    8080"

    Could plz someone help me, im stuck with this for quite some days now, and
    its drivin me nuts.
    thx in advance
    regards benni
    Benjamin Bittner, Jul 1, 2004
    #1
    1. Advertising

  2. Hallo Alstersjo
    "Alstersjo" <> schrieb im Newsbeitrag
    news:...
    > Hello Benjamin
    > Thanks for your answer regarding my case.
    > I have got it to work running under w2k workstation but not under w3k

    server.
    > There I get the same error as you
    > Can it have something with rights to do.
    > If I find out this I'll post it here, will you do the same?
    > Regards Henrik
    >

    yeah for sure.
    regards benni
    Benjamin Bittner, Jul 1, 2004
    #2
    1. Advertising

  3. Hallo Alstersjo
    "Alstersjo" <> schrieb im Newsbeitrag
    news:...

    im startin to belive, that theres some kind of bug or something like that.
    because as you say it works on a w2k workstation, but doesnt work on a win2k
    server. i searched through many forums and other every resource i could
    think of (msdn,kb,newsgroups). everytime the CreateProcessWithLogonW
    function is called the same (some little differences, but i tried them all).
    You see people thanking the author for his great job and posting how
    incredebly well the code works (little sarcastic sorry), but it wont work
    for us. so i assume theres something wrong with the systems were running.
    but im out of ideas and resources. is there any microsoft email adress or
    other resource i could post to?

    regards benni
    Benjamin Bittner, Jul 2, 2004
    #3
  4. Hello Bittner
    I'm starting to believe that to. And I am starting to run out of ideas. Do you have any ideas about workarounds?
    Are you in an AD environment?
    Regards Alstersjo

    "Benjamin Bittner" wrote:

    > Hallo Alstersjo
    > "Alstersjo" <> schrieb im Newsbeitrag
    > news:...
    >
    > im startin to belive, that theres some kind of bug or something like that.
    > because as you say it works on a w2k workstation, but doesnt work on a win2k
    > server. i searched through many forums and other every resource i could
    > think of (msdn,kb,newsgroups). everytime the CreateProcessWithLogonW
    > function is called the same (some little differences, but i tried them all).
    > You see people thanking the author for his great job and posting how
    > incredebly well the code works (little sarcastic sorry), but it wont work
    > for us. so i assume theres something wrong with the systems were running.
    > but im out of ideas and resources. is there any microsoft email adress or
    > other resource i could post to?
    >
    > regards benni
    >
    >
    >
    =?Utf-8?B?QWxzdGVyc2pv?=, Jul 5, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Wieder

    ASPNET and CreateProcessWithLogonW

    Matthew Wieder, Aug 18, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    2,448
    Matthew Wieder
    Aug 20, 2003
  2. Ryan Ternier
    Replies:
    4
    Views:
    3,957
    Cowboy \(Gregory A. Beamer\) [MVP]
    Jul 29, 2004
  3. =?Utf-8?B?VG9kZCBC?=

    CreateProcessWithLogonW

    =?Utf-8?B?VG9kZCBC?=, Aug 1, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    653
    =?Utf-8?B?VG9kZCBC?=
    Aug 1, 2005
  4. Benjamin Bittner

    Calling CreateProcessWithLogonW

    Benjamin Bittner, Jul 2, 2004, in forum: ASP .Net Security
    Replies:
    18
    Views:
    717
    Alstersjo
    Jul 16, 2004
  5. Todd B

    CreateProcessWithLogonW

    Todd B, Jul 26, 2005, in forum: ASP .Net Security
    Replies:
    1
    Views:
    404
    Todd B
    Aug 1, 2005
Loading...

Share This Page