how to use structs in a call to an unmanaged funtion

  • Thread starter Bruno van Dooren
  • Start date
B

Bruno van Dooren

Hi,

i need to create a .NET class that will provide the same functionality as
the windows 'run as' command.
it seems that this code is not available in the .NET framework.

I have found an example that will logon he desired creadentials to get a
security token.

the only thing i need to do now is to import the function

BOOL CreateProcessAsUser(
HANDLE hToken,
LPCTSTR lpApplicationName,
LPTSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCTSTR lpCurrentDirectory,
LPSTARTUPINFO lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation
);

i know how to pass simple things like integers, handles etc, but that
function call uses a number of pointers to win32 structures to set and get
information. how do i need to pass those? i assume that i have to create
equivalent structs?

I have lookde into the .NET framework, but it does not seem to support
strting a process as another user?
I know i can impersonate another user, but would that not affect the
application that i am starting the new process from?
 
N

Nicholas Paldino [.NET/C# MVP]

Bruno,

Starting a process as another is not going to affect your current
process. It will affect the process being run.

For the call to CreateProcessAsUser, you can declare the
LPSECURITY_ATTRIBUTES as IntPtr and pass along IntPtr.Zero to represent
null. I say that only because I don't typically see people use these
attributes.

However, the last structure needs marshaling. The structure would be
declared like this:

[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
[MarshalAs(UnmanagedType.U4)]
public int dwProcessId;
[MarshalAs(UnmanagedType.U4)]
public int dwThreadId;
}

You could then declare the last parameter as a "ref" parameter of this
type.

Hope this helps.
 
B

Bruno van Dooren

Starting a process as another is not going to affect your current
process. It will affect the process being run.

and if i use impersonation to impersonate another user?
does impersonation mean that everything i do in code until i undo the
impersonation
is executed with the impersonized credentials? i ask this because in that
case i could use
Process.Start() to start a new process, which would be easier that doing an
unmanaged call.

this also brings back my question about the credentials of secondary threads
in my current process.
(i an relatively new to windows security. you might have guessed :) )

anyway i really appreciate the code that you sent me. i will give it a try.

kind regards,
Bruno.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top