I have an existing c++ (actually, g++) program that registers as a service - and then - while running - reacts to the presence of a text file. Depending on the content of the lines in the text file - the service will do one of two things:
1) start a new process - running a batch file created by the internet user via an ASP page.
--Or--
2) send a small TCP/IP packet to a UNIX machine which has a companion daemon listening for the imcoming packet.
I'm trying to port this existing code to VB.NET.
The two winapi functions that the existing service uses are these:
if (LogonUser((LPTSTR)UserName, (LPTSTR)Domain, (LPTSTR)Passwd,
LOGON32_LOGON_SERVICE, LOGON32_PROVIDER_DEFAULT, &hUserHandle)) { --and--
if (CreateProcessAsUser(hUserHandle, NULL, (CHAR*)line.c_str(),
NULL, NULL, FALSE, (DWORD)NULL, NULL, NULL, &si, &pi)) {
The problem is - I cannot get the second function to work in VB.NET - I've tried just about every variation on this system call - searched the web for examples - and haven't found anything that will get this last function to work.
Here's one example of the VB function declaration and the call that I've tried. To no avail.
Public Declare Auto Function CreateProcessAsUser Lib "advapi32.dll" ( _
ByVal hToken As IntPtr, _
ByVal strApplicationName As String, _
ByVal strCommandLine As String, _
ByRef lpProcessAttributes As SECURITY_ATTRIBUTES, _
ByRef lpThreadAttributes As SECURITY_ATTRIBUTES, _
ByVal bInheritHandles As Boolean, _
ByVal dwCreationFlags As Integer, _
ByVal lpEnvironment As IntPtr, _
ByVal lpCurrentDriectory As String, _
ByRef lpStartupInfo As STARTUPINFO, _
ByRef lpProcessInformation As PROCESS_INFORMATION) As Boolean
Public Structure SECURITY_ATTRIBUTES
Public nLength As Integer
Public lpSecurityDescriptor As IntPtr
Public bInheritHandle As Boolean
End Structure
Public Structure PROCESS_INFORMATION
Public hProcess As IntPtr
Public hThread As IntPtr
Public dwProcessId As Integer
Public dwThreadId As Integer
End Structure
Public Structure STARTUPINFO
Public cb As Integer
Public lpReserved As IntPtr
Public lpDesktop As IntPtr
Public lpTitle As IntPtr
Public dwX As Integer
Public dwY As Integer
Public dwXSize As Integer
Public dwYSize As Integer
Public dwXCountChars As Integer
Public dwYCountChars As Integer
Public dwFillAttribute As Integer
Public dwFlags As Integer
Public wShowWindow As Short
Public cbReserved2 As Short
Public lpReserved2 As IntPtr
Public hStdInput As IntPtr
Public hStdOutput As IntPtr
Public hStdError As IntPtr
End Structure Then - in the sub main...
Dim saProc As SECURITY_ATTRIBUTES = New SECURITY_ATTRIBUTES
Dim saThread As SECURITY_ATTRIBUTES = New SECURITY_ATTRIBUTES
saProc.nLength = Marshal.SizeOf(saProc)
saProc.lpSecurityDescriptor = IntPtr.Zero
saThread.nLength = Marshal.SizeOf(saThread)
saThread.lpSecurityDescriptor = IntPtr.Zero
If CreateProcessAsUser( _
hToken, _
String.Empty, _
strCmdLine, _
saProc, _
saThread, _
False, _
0, _
IntPtr.Zero, _
String.Empty, _
si, _
pi) Then
(We're still using vb.net from VS 2003 - so the new 'process class' that came out with the 2.0 framework isn't available to me for this project. :-( )