Author Topic: WriteFile causes exception in VB.net but not VC++  (Read 17547 times)

theoracle39

  • Member
  • ***
  • Posts: 8
WriteFile causes exception in VB.net but not VC++
« on: July 09, 2010, 05:12:29 pm »
If I detect a USB device, unplug it, and then try to write to the device WriteFile causes an exception in VB.net.  If I run the same code in VC++ it returns WriteFile as false as it should.  Why does the function work differently in one language than the other?  How do I get WriteFile to return as false in VB.net if a USB device is suddently unplugged?

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: WriteFile causes exception in VB.net but not VC++
« Reply #1 on: July 09, 2010, 08:39:50 pm »
The exception is giving you useful information - you can no longer communicate with the device because it's no longer attached or other communication problems. If you don't need the information, catch and otherwise ignore the exception.

Jan

theoracle39

  • Member
  • ***
  • Posts: 8
Re: WriteFile causes exception in VB.net but not VC++
« Reply #2 on: July 10, 2010, 08:41:06 am »
If I ignore the exception then the program crashes with the message ObjectDisposedException was unhandled by user code.  I think this exception either needs to be prevented from happening or I need to be able to deal with it once it happens.  Any suggestions?

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: WriteFile causes exception in VB.net but not VC++
« Reply #3 on: July 10, 2010, 11:59:09 am »
By ignore I meant catch the exception but take no action in the exception handler.

Jan