Calling CreateProcessWithLogonW from asp.net

  • Thread starter Thread starter Arne
  • Start date Start date
A

Arne

Hi,
I'm working on an asp.net application that reads a users username and
password, and starts a process with the username read.

The problem is that the program returns value 2(ERROR_FILE_NOT_FOUND).

Here's my code:

public class Class1
{
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern bool CreateProcessWithLogonW(String lpszUsername,
String lpszDomain, String lpszPassword, int dwLogonFlags, string
applicationName, string commandLine, int creationFlags, IntPtr environment,
string currentDirectory, ref STARTUPINFO sui, out PROCESS_INFORMATION
processInfo);

[StructLayout(LayoutKind.Sequential)]
internal struct STARTUPINFO
{
internal int cb;
[MarshalAs(UnmanagedType.LPTStr)]
internal string lpReserved;
[MarshalAs(UnmanagedType.LPTStr)]
internal string lpDesktop;
[MarshalAs(UnmanagedType.LPTStr)]
internal string lpTitle;
internal int dwX;
internal int dwY;
internal int dwXSize;
internal int dwYSize;
internal int dwXCountChars;
internal int dwYCountChars;
internal int dwFillAttribute;
internal int dwFlags;
internal short wShowWindow;
internal short cbReserved2;
internal IntPtr lpReserved2;
internal IntPtr hStdInput;
internal IntPtr hStdOutput;
internal IntPtr hStdError;
}

[StructLayout(LayoutKind.Sequential)]
internal struct PROCESS_INFORMATION
{
internal IntPtr hProcess;
internal IntPtr hThread;
internal int dwProcessId;
internal int dwThreadId;
}

const int LOGON_NETCREDENTIALS_ONLY = 2;
const int CREATE_UNICODE_ENVIRONMENT = 0x00000400;
const int NORMAL_PRIORITY_CLASS = 0x00000020;
const int STARTF_USECOUNTCHARS = 0x00000008;


private void Page_Load(object sender, System.EventArgs e)
{

//Method where i keep the html code that all pages use
Webmethods met = new Webmethods();

met.startPage();

string file = Page.Request.Params.Get("filename");
string user = Page.Request.Params.Get("username");
string pw = Page.Request.Params.Get("password");
string domain = Page.Request.Params.Get("domain");
string cmdln = file +" param1 param2"; //file is c:\test.exe

PROCESS_INFORMATION processInfo;

STARTUPINFO startInfo = new STARTUPINFO();

startInfo.cb = Marshal.SizeOf(startInfo);
startInfo.lpTitle = "Command console";
startInfo.dwFlags = STARTF_USECOUNTCHARS;
startInfo.dwYCountChars = 50;

bool ret = CreateProcessWithLogonW(logonName, domain, password,
LOGON_NETCREDENTIALS_ONLY, null, cmdln, NORMAL_PRIORITY_CLASS |
CREATE_UNICODE_ENVIRONMENT, IntPtr.Zero, "C:\\", ref startInfo, out
processInfo);

if ( !ret )
{
Page.Response.Write("<h3><tr><td>Error:&nbsp"+ Marshal.GetLastWin32Error()
+"</td></tr></h3>");
Page.Response.Write("<h3><tr><td>ErrorHR:&nbsp"+
Marshal.GetHRForLastWin32Error()+"</td></tr></h3>");
}
else
Page.Response.Write("<h3><tr><td>ProcessID: {0}&nbsp" +
processInfo.dwProcessId+"</td></tr></h3>");
met.endPage();
}
}

This is what my page prints:

Error: 2
ErrorHR: -2147024894

Any help would be appreciated

Arne
 
Sorry, little mistake:

in original code, the CreateProcessWithLogonW method is called with
logonName = user and password = pw.
 
Back
Top