Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a problem on C# windows service, its showing an error - The array bounds are invalid

Note - If my dv.Count value is less than or equal to 1000 then its not showing error and works fine, but id dv.Count is more then 1000 then it generate the error....

Here is my code
C#
string[] arg = new string[dv.Count]; // here dv is nothing, just a dataview which contains id and items of product datatable

  for (int xx = 0; xx < dv.Count; xx++)
  {
  arg[xx] = dv[xx]["id"].ToString() + "," + dv[xx]["item"].ToString()
  }

    ServiceController sc = new ServiceController("Service1"); //Here Service1 is my child service name which i want to start from this parent service

  if (sc != null)
  {
  if (sc.Status.ToString().ToLower() == "stopped")
  {
  sc.Start(arg); // Error Occurred Here
  }
  }

Here is the full error details.
System.InvalidOperationException: Cannot start service xxxxx on computer '.'. ---> System.ComponentModel.Win32Exception: The array bounds are invalid<br />
  --- End of inner exception stack trace ---<br />
  at System.ServiceProcess.ServiceController.Start(String[] args)<br />
  at xxxxxx.Service1.Generate(Object sender, ElapsedEventArgs e)

Can someone please help on this...?
Posted
Updated 12-Nov-13 19:53pm
v4
Comments
Pheonyx 11-Nov-13 3:21am    
I suppose the first question is.. what is dv? How is it declared?
anurag.netdeveloper 13-Nov-13 2:32am    
dv is datview...
Timberbird 11-Nov-13 3:50am    
What is "Service1"? Could you show its start method?
Kornfeld Eliyahu Peter 11-Nov-13 4:15am    
We need the code in sc.Start as the error occurs in it!!!
anurag.netdeveloper 13-Nov-13 2:31am    
sc.Start(arg) is just a instance to start the child service - "Service1"...

This Service is my parent service which a invoker for child service....

This is what sc.Start(arg); does:
C#
public void Start(string[] args)
{
    // removed code (here) irrelevant to this issue
    IntPtr[] ptrArray = new IntPtr[args.Length];
    int index = 0;
    // removed code (here) irrelevant to this issue (build ptrArray...)
    GCHandle handle = new GCHandle();
    try
    {
        handle = GCHandle.Alloc(ptrArray, GCHandleType.Pinned);
        //This next line is where the trouble is!
        if (!UnsafeNativeMethods.StartService(serviceHandle, args.Length, handle.AddrOfPinnedObject()))
        {
            Exception innerException = CreateSafeWin32Exception();
            throw new InvalidOperationException(Res.GetString("CannotStart", new object[] { this.ServiceName, this.MachineName }), innerException);
        }
    }
    // removed code (here) irrelevant to this issue (misc cleanup...)
}

Considering where the problem appears to be (above), it looks like there's a 1000 element limit on the size of the array of args (OR perhaps a maximum total length of arguments) that can be passed to:
C#
[DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern bool StartService(IntPtr serviceHandle, int argNum, IntPtr argPtrs);

Consider wrapping your code in a try...catch and calling Marshal.GetLastWin32Error to determine exactly what went wrong.
See http://msdn.microsoft.com/en-us/windows/ms686321(v=vs.71).aspx[^] and http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.getlastwin32error(v=vs.100).aspx[^]
 
Share this answer
 
v2
try this and let us know your problem is solved or not.

C#
string[] arg = new string[dv.Count+1];
for (int xx = 0; xx < dv.Count; xx++)
{
arg[xx] = dv[xx]["id"].ToString() + "," + dv[xx]["item"].ToString()
}

  ServiceController sc = new ServiceController("Service1");
if (sc != null)
{
if (sc.Status.ToString().ToLower() == "stopped")
{
sc.Start(arg); // Error Occurred Here
}
}


This is just a work arround to test the code.
:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900