Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic .NET (http://www.programmingforums.org/forum19.html)
-   -   writing a service in VB.NET (http://www.programmingforums.org/showthread.php?t=15562)

opa6x57 Apr 6th, 2008 1:14 PM

writing a service in VB.NET
 
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. :-( )

Jabo Apr 6th, 2008 6:52 PM

Re: writing a service in VB.NET
 
maybe download VB Express, for free, from Microsoft? They recently release the 2008 version so you may find what you're looking for there. It's free, so what have you got to lose really?

opa6x57 Apr 6th, 2008 9:58 PM

Re: writing a service in VB.NET
 
Only compatibility with the other programmers in the company - we have decided to remain on the 2003 edition of visual studio - at least for now - so I'm pretty stuck with that.

(I did download and install the c# freebie from Microsoft from VS 2008 - and it's pretty neat - but I'm not permitted to use that for code I have to check in. Alas.)

Dameon Apr 6th, 2008 11:42 PM

Re: writing a service in VB.NET
 
Is a complete rewrite necessary? You could wrap problematic bits in a C++ library project. It really depends on the "why" and "how much" of this rewrite effort.

opa6x57 Apr 7th, 2008 12:31 AM

Re: writing a service in VB.NET
 
Quote:

Originally Posted by Dameon (Post 143576)
Is a complete rewrite necessary? You could wrap problematic bits in a C++ library project. It really depends on the "why" and "how much" of this rewrite effort.

Hmmmmm - you ask a good question...

The department I currently program for - has had many programmers over the past several years. Each has brought his/her own unique strengths to the job.

Programs written during the tenure of Mr. A tended to be Gnu c++(g++). Written and compiled on various platforms - Linux/UNIX/Windows.

Programs written during the tenure of Mr. C tended to be ASP/ASP.NET.

Programs written during the tenure of Ms. D tended to be Gnu C (gcc). Written and compiled on LINUX platforms with libraries for Windows linked in when needed.

I am wanting to get all of these legacy programs updated into the Visual Studio language that's appropriate to each task - and appropriate to my own coding ability. And, in so doing - the code will be more maintainable. (There are only two programmers on staff that can really deal with this old/legacy c++ code. Everyone else is a .NET programmer of some kind. If I get this code into a mainstream source VB.NET, VC#, VC++ - then it will be more readily modifiable.

(I currently have 3 different versions of Gnu c++ compilers - and they don't co-exist very well - so I have them loaded on three different machines. For one of the applications - I have to use machine A ... for another application, Machine B, and so on...)

The bottom line - this function - the one I'm trying to get to work - I wrote a VC# project and this same function won't work from there, either.

I did not try this in VC++, though - I suppose I could.

melbolt Apr 15th, 2008 8:26 AM

Re: writing a service in VB.NET
 
Quote:

Originally Posted by opa6x57 (Post 143558)
(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. :-( )

as a matter of fact there is a process class in the 1.1 framework

check it out

http://msdn2.microsoft.com/en-us/lib...ss(VS.71).aspx


namespace it is under is system.diagnostics


All times are GMT -5. The time now is 2:20 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC