I am accessing a third party DLL that drives a USB device. The first time I access the DLL I can receive the results of the activity on the device without a problem. If I run the same test again I receive a null value and GetLastError is 998 (error_noaccess).

The first time I run the test after a reboot I am consistently able to access the variable - the next time I run the test I cannot. Rebooting appears to be the only way to resolve the issue - restarting the application or the development environment do not resolve this issue.

I am using .net 3.5 running in SharpDevelop.

Here is the code:

I wrote a wrapper class for all the calls to the USB driver:
Code:
public class USBWrapper
{
 	[DllImport("USBDriver", CallingConvention= CallingConvention.StdCall)]
 	public static extern Boolean InitUSB();
	 	
 	[DllImport("USBDriver", CallingConvention= CallingConvention.StdCall)]
 	public static extern Boolean CloseUSB();
	 	
 	[DllImport("USBDriver", CallingConvention= CallingConvention.StdCall)]
 	public static extern Boolean USBSendCommand(byte Cmd);
	 	
	[DllImport("USBDriver", EntryPoint = "USBReadData", CallingConvention= CallingConvention.StdCall, SetLastError = true)]
	Public static extern Boolean USBReadData(byte EPNumber, long BytesToRead, short[] DataBuffer);


	//Codes sent to the device
	public const byte Start_TEST = 0x00;
	public const byte LEGGI_PACCHETTO = 0x01;
	public const byte Cod_VC = 0x02;
	public const byte Cod_MVV = 0x04;
	public const byte Cod_FVC = 0x08;
}
Here is the class that calls the wrapper:
Code:
public class USBDevice : IDisposable
{
	public bool startUSBDevice(){
		try {
			return USBWrapper.InitUSB();
		} catch (Exception) {
			return false;
		}
	}
		
	public byte[] startTest(){
		return startTest(USBWrapper.Start_TEST);
	}
		
		
	public byte[] startTest(byte testCode){
		int lengthOfStructure = 3;
		byte[] test;
		test = sendCommandToDevice(testCode, lengthOfStructure);
		return test;
	}
		
		
	internal byte[] sendCommandToDevice(byte commandToSend, int arrayLength){
		byte[] arrayForReturnValues = new byte[arrayLength];
		if (USBWrapper.USBGetCommand(commandToSend,arrayLength, arrayForReturnValues)) {
			return arrayForReturnValues;
		} else {
			throw new Exception("Failed to read command data from device.");
		}		
	}
		
	public void getTestResults(){
		try {
			readDataFromDevice( 57 );
		} catch (Exception e) {
			System.Diagnostics.Debug.Print(e.Message);
		}	
	}
	
	internal void readDataFromDevice(long length){
		short[] results = new short[57]; 		
		if (USBWrapper.USBReadDataWithArray(1, length, results) == true) {
			System.Diagnostics.Debug.Print("I can read the test results.");
		}else{
			System.Diagnostics.Debug.Print(Marshal.GetLastWin32Error().ToString());
			throw new Exception("Could not read test results.");
		}
	}
	public void Dispose(){
		killUSBDevice();
	}

	public bool killUSBDevice(){
		try {
			return USBWrapper.CloseUSB();
		} catch (Exception) {
			return false;
		}
	}
}
Here is the nominal function that initiates the test (I've collapsed it to make reading easier):
Code:
public static void Main(){
	byte[] testStart;
	USBDevice device = new USBDevice();

	device.startUSBDevice();
	byte[] testStart = device.startTest();
//wait until you know that the test is complete in here	
	device.getTestResults();
	device.killUSBDevice();
	device=null;
	GC.Collect();
	GC.WaitForPendingFinalizers();
			
}
The first time I run it I get an output saying: "I can read the test results.". The next time (and every subsequent time) I run it I get: "Could not read test results.".

Any ideas about how to fix this would be greatly appreciated.

Thanks