Click here to Skip to main content
15,912,021 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
GeneralRe: SOAP Server Pin
Patje10-May-04 21:20
Patje10-May-04 21:20 
GeneralRe: SOAP Server Pin
Ryan Roberts11-May-04 6:54
Ryan Roberts11-May-04 6:54 
GeneralRe: SOAP Server Pin
Patje11-May-04 21:15
Patje11-May-04 21:15 
GeneralXPathDocument vs. XmlDocument Pin
Paul Watson3-May-04 23:26
sitebuilderPaul Watson3-May-04 23:26 
General.net components site like boland delphi vcl sites(www.totty.net) Pin
Anonymous3-May-04 11:27
Anonymous3-May-04 11:27 
GeneralRebasing Pin
Ivan Fernandez3-May-04 3:18
Ivan Fernandez3-May-04 3:18 
GeneralRe: Rebasing Pin
ian mariano8-May-04 7:16
ian mariano8-May-04 7:16 
GeneralWindows Service Error "Access is denied" Pin
Christophe Hell2-May-04 23:10
Christophe Hell2-May-04 23:10 
Hello

I'm porting a Service administrator application written in C++ 6 to .Net C#.
This application install, uninstall, start, stop ... services written in .NET C#.
Everything is ok when installing a service.
My problem is on trying to start a service from my application or from the OS service agent, I get a error saying "Acces is denied".
When I use my old C++ 6 application I can install and start the same service without any problem.
Am I missing any security settings or have you any idea ?
Following a piece of the code I'm using:


[Flags]
public enum ServiceControlAccessRights : int
{
StandardRightsRequired = 0xf0000,
Connect = 0x00001,
CreateService = 0x00002,
EnumerateService = 0x00004,
Lock = 0x00008,
QueryLockStatus = 0x00010,
ModifyBootConfig = 0x00020,
AllAccess = 0xf003F

}

[Flags]
public enum ServiceControlType
{
OwnProcess = 0x010,
ShareProcess = 0x020,
KernelDriver = 0x001,
FileSystemDriver = 0x002,
InteractiveProcess = 0x100
}

[Flags]
public enum ServiceStartType
{
BootStart = 0x00,
SystemStart = 0x01,
AutoStart = 0x02,
DemandStart = 0x03,
Disabled = 0x04
}

[Flags]
public enum ServiceErrorType
{
ErrorIgnore = 0x00,
ErrorNormal = 0x01,
ErrorSevere = 0x02,
ErrorCritical = 0x03
}

[Flags]
public enum ServiceSpecificAccessType
{
QueryConfig = 0x0001,
ChangeConfig = 0x0002,
QueryStatus = 0x0004,
EnumerateDependents = 0x0008,
Start = 0x0010,
Stop = 0x0020,
PausContinue = 0x0040,
Interrogate = 0x0080,
UserDefinedControl = 0x0100,
AllAccess = 0xf01FF
}

#region DLLImport
[DllImport( "kernel32.dll", EntryPoint = "GetLastError" )]
public static extern int GetLastError();

[DllImport( "advapi32.dll", EntryPoint = "OpenSCManager" )]
public static extern int OpenSCManager( string machineName, string databaseName, ServiceControlAccessRights desiredAccess );

[DllImport( "advapi32.dll", EntryPoint = "CloseServiceHandle" )]
public static extern bool CloseServiceHandle( int hSCObject );

[DllImport( "advapi32.dll", EntryPoint = "CreateService" )]
public static extern int CreateService( int hSCManager, string serviceName, string displayName,
ServiceControlAccessRights desiredAccess, ServiceControlType serviceType, ServiceStartType startType, ServiceErrorType errorControl, string pathName,
string loadOrderGroup, string tagId, string dependencies, string serviceStartName, string password );

[DllImport( "advapi32.dll", EntryPoint = "OpenService" )]
public static extern int OpenService( int hSCManager, string serviceName, ServiceControlAccessRights desiredAccess );

[DllImport( "advapi32.dll", EntryPoint = "LockServiceDatabase" )]
public static extern int LockServiceDatabase( int handle );

[DllImport( "advapi32.dll", EntryPoint = "UnlockServiceDatabase" )]
public static extern bool UnlockServiceDatabase( int handle );

[DllImport( "advapi32.dll", EntryPoint = "ChangeServiceConfig" )]
public static extern bool ChangeServiceConfig( int hService,
ServiceControlType dwServiceType, int dwStartType, int dwErrorControl,
string lpBinaryPathName, string lpLoadOrderGroup, IntPtr lpdwTagId,
string lpDependencies, string lpServiceStartName, string lpPassword,
string lpDisplayName );
#endregion DLLImport


public bool InstallService( string svcPath, string svcName, string svcDispName )
{
try
{
int hScm = OpenSCManager( null, null, ServiceControlAccessRights.AllAccess );

if( hScm == 0 )
return false;

int hSvc = CreateService( hScm, svcName, svcDispName,
ServiceControlAccessRights.StandardRightsRequired |
ServiceControlAccessRights.CreateService |
ServiceControlAccessRights.ModifyBootConfig,
ServiceControlType.OwnProcess | ServiceControlType.InteractiveProcess,
ServiceStartType.DemandStart,
ServiceErrorType.ErrorNormal,
svcPath,
null, null, null, "LocalSystem", null );


if( hSvc == 0 )
{
int err = GetLastError();
CloseServiceHandle( hSvc );
CloseServiceHandle( hScm );
return false;
}
else
{
CloseServiceHandle( hSvc );
CloseServiceHandle( hScm );
return true;
}
}
catch( Exception e )
{
throw e;
}
}


Thanks
QuestionCan we use .NET assemblies in ATL Pin
Tarundeep Singh Kalra2-May-04 22:52
Tarundeep Singh Kalra2-May-04 22:52 
AnswerRe: Can we use .NET assemblies in ATL Pin
ian mariano8-May-04 7:18
ian mariano8-May-04 7:18 
Generalstatus bar color Pin
Dpriya2-May-04 20:57
Dpriya2-May-04 20:57 
GeneralRe: status bar color Pin
Jeff Martin4-May-04 5:46
Jeff Martin4-May-04 5:46 
Generalmultiline column headers in a windows forms datagrid Pin
ricardojb1-May-04 15:06
ricardojb1-May-04 15:06 
GeneralWeb Service From Java Pin
Member 1697730-Apr-04 19:32
Member 1697730-Apr-04 19:32 
GeneralRe: Web Service From Java Pin
Colin Angus Mackay3-May-04 0:07
Colin Angus Mackay3-May-04 0:07 
GeneralRe: Web Service From Java Pin
ian mariano13-May-04 18:35
ian mariano13-May-04 18:35 
GeneralBack to the beginning Pin
RickZ28-Apr-04 18:22
RickZ28-Apr-04 18:22 
GeneralSystem.EntryPointNotFoundException Pin
taylo28-Apr-04 5:01
taylo28-Apr-04 5:01 
GeneralRe: System.EntryPointNotFoundException Pin
Mike Dimmick28-Apr-04 6:19
Mike Dimmick28-Apr-04 6:19 
GeneralRe: System.EntryPointNotFoundException Pin
taylo28-Apr-04 6:33
taylo28-Apr-04 6:33 
GeneralRe: System.EntryPointNotFoundException Pin
Mike Dimmick28-Apr-04 7:22
Mike Dimmick28-Apr-04 7:22 
GeneralRe: System.EntryPointNotFoundException Pin
taylo28-Apr-04 8:18
taylo28-Apr-04 8:18 
GeneralRe: System.EntryPointNotFoundException Pin
taylo3-May-04 9:52
taylo3-May-04 9:52 
GeneralUsing System.Data.SqlClient in MSDE install. Pin
ProffK28-Apr-04 0:59
ProffK28-Apr-04 0:59 
Generalword and power point support in .net Pin
seemeen27-Apr-04 20:47
seemeen27-Apr-04 20:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.