CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: GetLastError = 998 when calling unmanaged code for the second time, first time works

  1. #1
    Join Date
    May 2012

    GetLastError = 998 when calling unmanaged code for the second time, first time works

    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;
    	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:
    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) {
    	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.");
    			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.


  2. #2
    Join Date
    Apr 1999

    Re: GetLastError = 998 when calling unmanaged code for the second time, first time wo

    Please see my response here:

    You may have messed up parameter passing by passing types that are not the same size that the DLL expects. Once you do that, then all bets are off as to how your program will behave.


    Paul McKenzie

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)