Question

Problem getting handle via CreateFile() when opening a SCSI tape drive via \\.\TAPE0, but only when tape is busy running microsoft backup

Asked by: dlethe

I am trying to perform some pass-through SCSI programming on my Windows-based Tape susbystem.  I want to open the tape in read-only mode so I can issue some CDBs to it.  Everything works fine unless the tape drive is running the microsoft backup program.

(Note, I have never had similar problems doing this under other operating systems, and I am performing pass-through SCSI commands that read log & mode pages. I am not trying to read media.  I know that the same tape drive has no problems doing this properly under other operating systems)

Here is my stripped down code
if (ReadWriteOpenRequired)
      handle = CreateFile("\\\\.\\TAPE0",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
else
      handle = CreateFile("\\\\.\\TAPE0",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_READONLY,NULL);

The problem is that handle returns a -1.
I am running everything As administrator, and the code works fine if the tape is not busy.  

I know that there is a function, GetTapeStatus()  that can tell me if the tape is somehow locked in exclusive mode, but that function also needs to invoke CreateFile.  Am I missing something?  I am using VS2008.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-11-03 at 10:15:29ID24868137
Tags

TAPE

,

Programming

,

SCSI

,

C

,

Windows programming

Topics

C++ Programming Language

,

Hard Drives & Storage

,

Windows MFC Programming

Participating Experts
2
Points
500
Comments
23

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. scsi ide zip and tape
    what /dev driver are scsi cdroms on? How about ide cdroms,zip drives and tape drives? thanks jesse.laeuchli@usa.net www.angelfire.com/biz/freestore/index.html
  2. loading scsi tape in dos
    How do I load a scsi tape device in dos
  3. SCSI
    How I can Install ISApport scsi and tape drive

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: jkrPosted on 2009-11-03 at 10:27:52ID: 25731879

What does 'GetLastError()' report after 'CreateFile()' fails?

 

by: jkrPosted on 2009-11-03 at 10:29:32ID: 25731898

BTW, see also http://msdn.microsoft.com/en-us/library/aa362520(VS.85).aspx ("Creating a Backup Application"). According to this article, the parameters to 'CreateFile()' should be as follows:

HANDLE hTape;   // handle to tape device
 
hTape = CreateFile(TEXT("\\\\.\\TAPE0"),         // tape dev to open
                   GENERIC_READ | GENERIC_WRITE, // read/write access
                   0,                            // not used
                   0,                            // not used
                   OPEN_EXISTING,                // req for tape devs
                   0,                            // not used
                   NULL);                        // not used
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:

Select allOpen in new window

 

by: dlethePosted on 2009-11-03 at 10:46:12ID: 25732052

GetLastError returns 20h, ERROR_SHARING_VIOLATION

jkr:  I tried your suggestion, handle = CreateFile(DriverDev,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,NULL);
got same error (when tape was streaming, of course.  Never a problem when the windows backup isn't running.  I can't imagine that MSFT created a situation where exclusive access is prevented for pass-through, this would break the O/S, and defeat the purpose of running something like GetTapeStatus to see if the tape is busy in the first place!



 

by: TapeDudePosted on 2009-11-03 at 11:34:21ID: 25732596

If another application has opened the tape drive, and has locked access to it, then I'd expect the OS to refuse your request to open it, particularly if you've set the GENERIC_WRITE flag. What happens if you just open it using GENERIC_READ?

If it still fails to open you may be able to access the drive by talking to the controller, and getting it to pass your SCSI requests through to the drive.

Not that I've ever tried it ;-)

 

by: TapeDudePosted on 2009-11-03 at 11:36:49ID: 25732624

PS, I don't think GetTapeStatus will return lock status... according to my documentation, the only statii it will return are:

ERROR_BEGINNING_OF_MEDIA
ERROR_BUS_RESET
ERROR_END_OF_MEDIA
ERROR_FILEMARK_DETECTED
ERROR_SETMARK_DETECTED
ERROR_NO_DATA_DETECTED
ERROR_PARTITION_FAILURE
ERROR_INVALID_BLOCK_LENGTH
ERROR_DEVICE_NOT_PARTITIONED
ERROR_MEDIA_CHANGED
ERROR_NO_MEDIA_IN_DRIVE
ERROR_NOT_SUPPORTED
ERROR_UNABLE_TO_LOCK_MEDIA
ERROR_UNABLE_TO_UNLOAD_MEDIA
ERROR_WRITE_PROTECT

 

by: jkrPosted on 2009-11-03 at 11:39:08ID: 25732652

Can you try ProcessExplorer (http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx) to see which other process (if any) is using the device in question?

 

by: dlethePosted on 2009-11-03 at 11:53:45ID: 25732819

Hi TapeDude, My original logic was CreateFile("\\\\.\\TAPE0",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_READONLY,NULL);
I've pretty much tried every combination trying GENERIC_READ|GENERIC_WRITE, and FILE_SHARE_READ|WRITE, with and without the READONLY; tried permutations with CREATE_ALWAYS instead of OPEN_EXISTING, as sell as FILE_ATTRIBUTE_READONLY both in and out.

I am doing everything as administrator, and verified that I am logged into administrator.  I used sysinternals' process monitor and confirmed ntbackup is using \device\tape0, and looked at permissions, just to see if anything weird added.

GetTapeStatus won't work anyway because it is useless unless you can get a handle via the CreateFile.  

Even if I did know how to talk to controller directly it would be vendor/product specific, so can't go down that path.  Perhaps there is a mechanism to use an alias for \\.\TAPE0 ?   Is the O/S incapable of opening a tape drive that is using NTBACKUP for pass-through?    

 

by: jkrPosted on 2009-11-03 at 12:03:13ID: 25732938

>> and confirmed ntbackup is using \device\tape0

Well, so the error code is correct. Try to stop NTBackup and see if it works then.

 

by: TapeDudePosted on 2009-11-03 at 12:21:06ID: 25733124

This is why I used to use the ASPI layer! You could do whatever you liked to a drive, independent of the OS. But Adaptec stopped supporting ASPI after XP.

The only thing I can think of is to run a process at boot time that opens a handle to the drive and then just sits in memory, fielding your requests to/from the drive. But if NTBackup tries to lock the drive, it may fall over if Windows refuses to lock it; and if Windows DOES allow NTBackup to lock the drive, your process may not be able to access the drive.

You could try writing a dirty little command-line program that opens a handle to the drive, and then sits awaiting keyboard input. Meanwhile, run NTBackup and see what happens. If NTBackup runs, then try getting your command-line program to do a GetTapeStatus.

 

by: dlethePosted on 2009-11-03 at 12:22:45ID: 25733147

it works fine when ntbackup is not actively doing any thing to the tape.  I want to access the tape to perform pass-through regardless of whether or not ntbackup is running.  

 

by: TapeDudePosted on 2009-11-03 at 12:25:37ID: 25733187

So if you've already opened a handle to the drive, and then run NTBackup and get it to do something, calls to your existing handle fail?

 

by: jkrPosted on 2009-11-03 at 12:28:39ID: 25733220

>>I want to access the tape to perform pass-through regardless of whether or
>>not ntbackup is running.

Sorry, it's not a good idea to access a device that is in use by any means - be it a tape or anything else. That's what ERROR_SHARING_VIOLATION was created for.

 

by: TapeDudePosted on 2009-11-03 at 12:32:13ID: 25733261

"Sorry, it's not a good idea to access a device that is in use by any means"

True. Particularly to read or write! But assuming dlethe knows the dangers and is not actually setting tape modes or requesting/writing blocks, it shouldn't be a problem.

Be a good idea to ensure the "verify" setting is on in NTBackup though :-)

 

by: dlethePosted on 2009-11-03 at 12:37:38ID: 25733323

Respectfully, I've been writing storage diagnostics since the 90s, and know exactly what is safe.  What I am doing is safe.

I am sending SCSI inquires, LOG SENSE & MODE SENSE.  These are entirely safe.  The code works on a dozen other operating systems as well, just got the one stupid problem with windows locking me out on a tape while NTBACKUP is accessing it.    But I do want to say that I appreciate your comments.  If I was the expert in this particular case, I would have posted the same response as you :)

The technique works just fine, BTW, on \\.\PHYSICALDISK, SES/SAFTE enclosures, CDROMS/DVDs, and even certain RAID controllers. Just have the issue with windows tape drives & ntbackup.





 

by: jkrPosted on 2009-11-03 at 12:41:06ID: 25733360

>> What I am doing is safe.

I am not doubting that, not the least. But if NTBackup locks the device, there's no way to access it, sorry. And also sorry if I came across disrepectfully, I had no intention to do that.

 

by: TapeDudePosted on 2009-11-03 at 12:47:17ID: 25733415

So, now that we all we know we respect each other... ;-)

So if you've already opened a handle to the drive, and then run NTBackup and get it to do something, calls to your existing handle fail?

 

by: dlethePosted on 2009-11-03 at 12:59:15ID: 25733551

Sorry, don't have meeting of minds on if i open the tape handler first and keep it open, then run ntbackup ...

If I attempt this ,then NTBackup says "There is no free media available with the selected type. Ad unused media or click cancel to end the operation". Note: It may take up to 60 seconds to recognize new media".

So NTBACKUP also has a problem, but the message it returns is misleading.

 

by: jkrPosted on 2009-11-03 at 13:07:57ID: 25733659

The problem with that approach would be that NTBackup apparently requests exclusive access - now, if you have a handle open (even with shared access), NTBackup will fail.

 

by: TapeDudePosted on 2009-11-03 at 13:09:05ID: 25733674

It's not looking good... did you open the handle with GENERIC_READ, and FILE_SHARE_READ | FILE_SHARE_WRITE access?

 

by: dlethePosted on 2009-11-03 at 13:11:07ID: 25733698

The windows event log recorded RSM cannot manage library Tape0. Failed to communicate with device or obtain device setup information.   The Event ID is 168. Category None, Type: Error.  / Remote Storage Manager.

After googlin' RSM error 168, I found a promising link to "Qlogic Support Solution 12362". This is what the qlogic site said:
===============================

Tape backups are failing in Windows when the Removable Service Manager (RSM) is enabled. The server may be receiving Event ID: 168 errors.
Cause
      
This event occurs because Removable Storage Management (RSM) and some backup software applications are both trying to control the management of the robotic library.
Action
      
To correct this issue, disable the Removable Storage service, as described in the following paragraphs.
 
1. Enter Computer Management by right-clicking My Computer and selecting Manage.
2. Expand Services and Applications, then highlight Services.
3. Right-click on the Removable Storage Service and click Stop.
4. Double-click Removable Storage, and set the Startup type to Disabled. Click OK.
5. Exit Computer Management.
=====
My system had it as Manual.
Rebooting system now & will let you know after experimenting...

 

by: dlethePosted on 2009-11-03 at 13:23:27ID: 25733871

Well, it won't find a tape drive unless service is running.  Started service, started NTBACKUP on a big job, killed service, .. started my code.  Got same error as before ARRGGH!!

 

by: TapeDudePosted on 2009-11-03 at 13:28:25ID: 25733949

It's looking like passing SRBs via the controller is the only way you're going to get this done.

It may not be as device/oem specific as you fear - check out http://karuppuswamy.com/wordpress/2008/12/15/notes-on-windows-scsi-miniport-driver/

 

by: dlethePosted on 2009-11-07 at 11:46:51ID: 31649553

I will reluctantly have to go with passing SRBs, but was hoping there was a more elegant solution that would have let the standard device-level pass-through to work.   For what it is worth, I'll probably log a support incident to MSFT to see if there is a simpler way, but TapeDude did provide a technique that will certainly do the job.
THanks - If I figure out an easier method, I will revisit this and let you know.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...