Error 998 when acessing variable from unmanaged code second time
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
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

    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:
    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:
    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) {
    	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.");
    			throw new Exception("Could not read test results.");
    	public void Dispose(){
    	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):
    public static void Main(){
    	byte[] testStart;
    	USBDevice device = new USBDevice();
    	byte[] testStart = device.startTest();
    //wait until you know that the test is complete in here	
    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.

    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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)