[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Cannot open ifstream in non-shared mode on Windows 2008 Server

Posted on 2011-04-29
Medium Priority
Last Modified: 2012-05-11
Dear All!

I have a problem with opening file in non-shared mode on Windows 2008 server using ifstream object. My code works fine on Windows XP, but as soon as I try to run it on Windows 2008 Server, the file open fails with errno value 13 (permission denied). I have "Full Control" rights over the file and the folder containing the file. The file is not open by any other users, as it can be renamed and moved to the other folder. In case of both operating systems the file is located on a local drive, not on a network share. The line in my code opening the file is as follows:

inpfile.open (ifname, ios::in | ios::binary, filebuf::sh_none);

where inpfile is an object of class ifstream.

If I remove the "filebuf::sh_none" parameter, it works fine also on Windows 2008. However, I would like to make sure that no one else is accessing the file at the same time (so I need the exclusive mode), that's why I used protection specification sh_none.

What could be the possible reason of such behaviour? Does someone have the same experience?

I am using the old Microsoft Visual C++ 6.0 - perhaps it's just too old to compile a program to be used on Windows 2008...

I would be happy if someone could share some experience. Thanks in advance!
Question by:otu
LVL 86

Accepted Solution

jkr earned 1000 total points
ID: 35494962
Well, to be blunt - 'filebuf::sh_none' is definitely not a valid member of 'ios_base::openmode' (http://msdn.microsoft.com/en-us/library/5kb732k7%28v=vs.80%29.aspx). Anyway, opening files in 'exclusive' mode is highly OS specific and therefore not covered by the C++ standard. However, the discussion at http://social.msdn.microsoft.com/Forums/en-HK/vcgeneral/thread/2bea83b3-f07a-4868-8f86-81fc0bf5adc4 ("filebuf::sh_none - compiler error") and the followup at http://stackoverflow.com/questions/4435091/sh-none-is-not-a-member-of-stdbasic-filebuf-elem-traits might helpt - try

inpfile.open (ifname, ios::in | ios::binary, _SH_DENYRW);

instead (even though I'd still prefer to use the Wiindows API for such a purpose).
LVL 40

Expert Comment

ID: 35495134
I agree with JKR, if you are doing file locking, etc. on Windows, I'd prefer to use the CreateFile() and other Win32 methods. They are closer to the OS, as far as debugging what the heck is going wrong. I usually use a thin layer of abstraction with conditional compilation across UNIX or Windows.

If your code never needs to run any non-Win32 platforms, then Win32 API is the customary way to code anyway. The C++ iostreams lib has changed a bit over the years (especially since VC 6) and in my opinion is too bloated and complex, compared to Win32 or STDIO (C).
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35495603
I'm not nearly as knowledgeable as jkr or mrjoltcola, so you're probably better off following their advice than mine - plus this more of a "why it happens" comment, not a "how to resolve it" (at least an educated guess at any rate).

If it works under XP but not Server 2008 the first thing that comes to my mind is UAC.  Certain folders, like Program Files, are protected by UAC - so that even if you login as an administrator and/or have full access to a file in C:\Program Files somewhere, unless you right-click on the executable and specifically choose "Run as Administrator..." UAC will cause a permission denied.

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

LVL 12

Expert Comment

ID: 35501957
I have run into a similar situation on WS2008 and I got round it by using this API and passing _SH_DENYNO  as the mode.

However I agree with what tgerbert is saying because there are still cases where I've seen my program fail with an error code 13 especially when I access files under %windir%/System32

So I too would suggest using the Windows API in your case and maybe try moving to a later version of the compiler (VS2005, VS2008 or VS2010)
LVL 12

Expert Comment

ID: 35502042
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35502643

If the file you're trying to access is anywhere under C:\Program Files or C:\Windows you're application will probably need to be run elevated (the user can right-click a shortcut and choose Run as administrator; you can include a UAC manifest with your executable so Windows will automatically prompt the user to elevate it; your application can use ShellExecute to re-launch itself with the runas verb; you can use a system policy to always run everything elevated - i.e. disable UAC).  However, unless absolutely necessary, you should consider using a location in the users' profile, or in %ALLUSERSPROFILE%.

If the file in question is not under %WinDir% or %ProgramFiles% then UAC might not have anything to do with your problem.

Embed a UAC manifest: http://msdn.microsoft.com/en-us/library/bb756929.aspx

LVL 40

Expert Comment

ID: 36518469
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question