Windows service caveats with Process.Start

Feb 29
2016

These days, I was working on a webservice that worked directly with http.sys.
This service was planned to wait for requests, and launch programs depending on the request.
Firstly, I planned to use the local system account, and launch the external programs as specific user. This program should be started from a network share, using UNC paths.
Unfortunately, that does not work, Process.Start cannot do that:
http://blogs.msdn.com/b/winsdk/archive/2013/11/12/runas-verb-process-start-doesn-t-work-from-a-localsystem-net-service.aspx

My next decision was to add a user for this purpose (to run the Windows service). Unfortunately, Process.Start ignores the essential settings for a Windows service

oInfo:UseShellExecute := false
oInfo:CreateNoWindow := true
oInfo:ErrorDialog := false
oInfo:WindowStyle := ProcessWindowStyle.Hidden

The page from MSDN (https://msdn.microsoft.com/library/0w4h05yb%28v=vs.100%29.aspx) states:
“If the UserName and Password properties of the StartInfo instance are set, the unmanaged CreateProcessWithLogonW function is called, which starts the process in a new window even if the CreateNoWindow property value is true or the WindowStyle property value is Hidden.”
At the moment I’m not changing the user from the service, but executing the external program under the service account.

I had also another small issue: since I was reading the password from an ini file, I thought the StartInfo:PasswordInClearText property was enough. On my development machine it worked, but crashed on the production server, an SBS 2011 (based on Windows Server 2008 R2) because with the .NET Framework 4 this property simply does not exists.

Another issue: if you plan to start a process as another user from a service: this user needs local logon rights (on a server OS this is a manual change)

Event sample

Feb 21
2016

Today, trying to understand also events in Vulcan.NET/X#, I wrote the following sample that should give an idea how events work in Vulcan.NET and X#.

#using EventSample

begin namespace EventSample

function Start( ) as void
  local oClass2 as Sample2Class

  System.Console.WriteLine(“Start of the SampleEvent application”)

  oClass2 := Sample2Class{}
  oClass2:RegisterEvent()

return

public delegate SampleHandle( cParameter as string ) as void

class SampleClass
  public event oSampleEvent as SampleHandle

constructor()

  return

method CallSampleEvent() as void

  oSampleEvent( “Hi there” )

  return

end class

class Sample2Class

constructor()

  return

method RegisterEvent() as void
  local oClass as SampleClass

  oClass := SampleClass{}
  oClass:oSampleEvent += self:method2
  oClass:CallSampleEvent()

  return

public method method2( cParameter as string ) as void

  System.Console.WriteLine( “method2:” + cParameter )

  return

end class

end namespace

Delegates sample

Feb 21
2016

Hi,

when working with X# and Vulcan.NET, I have created this particular sample. It should help understand how delegates work (and that they are immutable):

#using DelegateSample

begin namespace DelegateSample

public delegate SampleDelegate( cParameter as string ) as void

function Start( ) as void
  local oSample as SampleClass
  local oSample2 as Sample2Class
  local oDelegate as SampleDelegate
  local oDelegate2 as SampleDelegate

  System.Console.WriteLine(“Start SampleDelegate”)

  oSample := SampleClass{}
  oSample2 := Sample2Class{}

  oDelegate := SampleDelegate{ oSample:Method1 }
  oDelegate += oSample:Method2
  oDelegate += oSample2:Method3

  oDelegate( “Hi” )
  oDelegate2 := oDelegate

  oDelegate -= oSample:Method2
  oDelegate -= oSample2:Method3

  oDelegate( “Hott” )
  oDelegate2( “Hups” )

  return

class SampleClass

  constructor()

  return

  public method Method1( cParameter as string ) as void

    System.Console.WriteLine( “method1, parameter ” + cParameter )

    return

  public method Method2( cParameter as string ) as void

    System.Console.WriteLine( “method2, parameter ” + cParameter )

    return

end class

class Sample2Class

  public method Method3( cParameter as string ) as void

    System.Console.WriteLine( “method3, Sample2Class, parameter ” + cParameter )

    return

end class

end namespace