Error 998 when acessing variable from unmanaged code second time
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Error 998 when acessing variable from unmanaged code second time

Threaded View

  1. #1
    Join Date
    May 2012
    Location
    London
    Posts
    4

    Error 998 when acessing variable from unmanaged code second time

    Sorry wrong forum - I have reposted to .Net / c sharp forum - sorry

    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;
    }
    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(){
    		int lengthOfStructure = 3;
    		byte[] test;
    		test = sendCommandToDevice(USBWrapper.Start_TEST, 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.USBReadData(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
    Last edited by OptiDev; July 8th, 2012 at 01:44 AM. Reason: Wrong forum

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center