burstadmin
asked on
How do I call CreateProcessAsUser from Visual C#?
I am writing a scheduling service to replace one written in Delphi. Here, I need to create a process from the service application which will be invoked by a specified user on that user's desktop. Otherwise, they cannot see the application they have scheduled running and become especially ill tempered. (They are always testy...)
I found lovely code on Microsoft.com (http://support.microsoft.com/kb/q165194/) to do this, but it is written in C++, a language which I am loth to use for this purpose.
I do well until I run into code which uses heap_alloc() getting the logon sid of the user. I have no signature for the TOKEN_GROUPS struct. I have attempted to define it and plug it into the code, but I keep getting problems with C# seeing my struct as managed code.
The attached snippet holds both my definitions and the code to the point from which I am not able to proceed.
I know this is nasty, but I would appreciate even the smallest of hints...
Digga
I found lovely code on Microsoft.com (http://support.microsoft.com/kb/q165194/) to do this, but it is written in C++, a language which I am loth to use for this purpose.
I do well until I run into code which uses heap_alloc() getting the logon sid of the user. I have no signature for the TOKEN_GROUPS struct. I have attempted to define it and plug it into the code, but I keep getting problems with C# seeing my struct as managed code.
The attached snippet holds both my definitions and the code to the point from which I am not able to proceed.
I know this is nasty, but I would appreciate even the smallest of hints...
Digga
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr HeapAlloc(IntPtr hHeap, uint dwFlags, UIntPtr dwBytes);
...
unsafe public struct SID_AND_ATTRIBUTES
{
public IntPtr Sid;
public UInt32 Attributes;
}
unsafe public struct TOKEN_GROUPS
{
public UInt32 GroupCount;
public SID_AND_ATTRIBUTES[] Groups;
...
bool ObtainSid(IntPtr hToken, IntPtr psid)
{
bool bSuccess = false;
IntPtr ptg = IntPtr.Zero;
UInt32 dwIndex = 0;
uint dwLength = 0;
TOKEN_INFORMATION_CLASS tic = TOKEN_INFORMATION_CLASS.TokenGroups;
try
{
if (!GetTokenInformation(hToken, tic, ptg, 0, out dwLength))
{
if (Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER)
{
ptg = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwLength);
// more to come, if I can get the above to compile and work,
}
}
bSuccess = true;
Leave: ;
}
finally
{
}
return bSuccess;
}
Error 1 The best overloaded method match for 'SchedulerIIProofOfConcept.Form1.HeapAlloc(System.IntPtr, uint, System.UIntPtr)' has some invalid arguments C:\Documents and Settings\jjalbert\Desktop\Scheduler 2\SchedulerIIProofOfConcept\Form1.cs 422 31 SchedulerIIProofOfConcept
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER