1

I have a Windows NT Service and a Third Party exe which i want to run as a child process of NT Service, such that as soon as my NT service process crashes, this child process is also killed

For this i found the way of doing this using Kill child process when parent process is killed

I tried to implement it and worked fine using a normal parent process, but when i do the same in my NT Service as the parent SetInformationJobObject method return false and a Exception with Error Code 0

Exception: _COMPlusExceptionCode = -532462766

What is the difference between a Normal process and a NT Service process which is causing this exception ?

i am using Win2k8 R2 Server machine and C#

[EDIT1] Exception: GenericParameterAttributes = '(((System.Reflection.RuntimeConstructorInfo)(ex._exceptionMethod)).ReflectedType).GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException' {"Method may only be called on a Type for which Type.IsGenericParameter is true."}

EDIT 2: Because SetLastError was not set to true in the function defination using DLLImport, so last error was wrong Correct ErrorCode is 24 which say bad lench of the structure, so what should be the correct structure ?

Edit 3: Correct Length Expected in the case of a NT Servce for my 64 bit win2k8R2 server machine seems to be 144 as compared to 112 as defined in the above post

Edit 4: Is this the only way of doing this, what are other options of doing same ?

Community
  • 1
  • 1
  • The NT Service is probably running under a different user than the child process? Any permissions issues to check? – Chris O Aug 19 '13 at 17:12
  • It bombs with an unhandled managed exception, exception code 0xe0434352. At a minimum you should implement an event handler for AppDomain.CurrentDomain.UnhandledException and log e.ExceptionObject.ToString() so you know why your code is crashing. – Hans Passant Aug 19 '13 at 17:15
  • FYI, 0xE0434352 is the general CLR exception code, that status value is used under the covers during SEH (structured exception handling) when any CLR Exception is thrown. – josh poley Aug 19 '13 at 17:59
  • Chris - its failing in the Job constructor itself :(, even if i remove the child process code it is throwing exception HansPassant: Added Exception Details, will see if i can figure out something from this exception – Sunny Agrawal Aug 19 '13 at 18:14

1 Answers1

0

Problem is in the structure declaration used by me as defined in thread Kill child process when parent process is killed

Structure defined in the above thread are valid for 32 bit applications and
My NT Service was a 64 bit application and for which it requires some changes

Changes Made

  • First change the DLL Import of SetInformationJobObject to

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool SetInformationJobObject(IntPtr hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, uint cbJobObjectInfoLength);

    As SetLastError Flag was not set it was not giving me right error Code, after getting the right error code only i was able to get that it is expecting a 144 byte length structure

  • Then Chnage the JOBOBJECT_BASIC_LIMIT_INFORMATION and JOBOBJECT_EXTENDED_LIMIT_INFORMATION structures to following

    [StructLayout(LayoutKind.Sequential)]
    struct JOBOBJECT_BASIC_LIMIT_INFORMATION
    {
        public Int64 PerProcessUserTimeLimit;
        public Int64 PerJobUserTimeLimit;
        public Int32 LimitFlags;
        public UInt64 MinimumWorkingSetSize;
        public UInt64 MaximumWorkingSetSize;
        public Int32 ActiveProcessLimit;
        public Int64 Affinity;
        public Int32 PriorityClass;
        public Int32 SchedulingClass;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct JOBOBJECT_EXTENDED_LIMIT_INFORMATION
    {
        public JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation;
        public IO_COUNTERS IoInfo;
        public UInt64 ProcessMemoryLimit;
        public UInt64 JobMemoryLimit;
        public UInt64 PeakProcessMemoryUsed;
        public UInt64 PeakJobMemoryUsed;
    }

Basically as per JOBOBJECT_EXTENDED_LIMIT_INFORMATION structure and JOBOBJECT_BASIC_LIMIT_INFORMATION structure we should use data type as per application SIZE_T = UInt64 vs UInt32 DWORD = Int32

After the changes it worked Fine.

Community
  • 1
  • 1