5

Microsoft has deprecated _open in favor of _sopen_s. What are the recommended equivalent arguments?

4
  • Here is the answer (I'm not allowed to answer my own question). For int fd = _open(name,oflags); Replace with int fd; errno_t errno = _sopen_s(&fd,name,oflags,_SH_DENYRW,0);
    – user281806
    Aug 5, 2011 at 23:35
  • 2
    Of course you're allowed to answer your own question... just not immediately after you had posted your question. Try putting that in as an answer again. Aug 5, 2011 at 23:58
  • Ah, inflation of the arguments. Win32-itis. Aug 6, 2011 at 7:13
  • I'm not allowed to answer a question within 8 hours of asking it. I'm in the 8 hour jail. :(
    – user281806
    Aug 10, 2011 at 22:55

3 Answers 3

9

Well now I can post my nicely formatted answer, so here it is:

For

int fd = _open(name,oflags);

Replace with

int fd;
errno_t errno = _sopen_s(&fd,name,oflags,_SH_DENYNO,0);

The reason I posted this Q&A is that it provides an important bit of non-obvious info that Microsoft didn't provide. I agree that the _sopen_s is a poor replacement for _open. I only researched this topic because I was fixing a bug caused by the wrong arguments being supplied to _sopen_s. The _sopen_s was only in the code to get rid of the compiler warnings; the original _open call was fine.

3
  • So you as well tried to pass 0 as the fourth argument? They could at least make it so that passing zero means that you don't care about that flag or option. Currently passing 0 as this argument raises an exception and terminates your application.
    – SoleSoul
    Feb 22, 2018 at 15:10
  • If I spy MS code of _wopen I see the following: errno_t const _Result = _wsopen_dispatch(_FileName, _OFlag, _SH_DENYNO, _PMode, &_FileHandle, 0); so it's using SH_DENYNO instead of _SH_DENYRW.
    – gast128
    Mar 6, 2019 at 17:01
  • You are correct, it is _SH_DENYNO. I've updated the answer.
    – user281806
    Apr 9, 2019 at 17:18
1

The best recommendation is ignoring all of Microsoft's politically motivated deprecations. Their "safe" functions offer no actual safety advantages over correct use of the existing, standard C or POSIX-like functions, and if used incorrectly, they are just as "unsafe" as the functions they aim to replace.

4
  • That's a very provocative statement. Do you have some evidence to back it up? Aug 6, 2011 at 17:02
  • It's been discussed on SO before, but it doesn't really require evidence, just some basic reasoning ability... Aug 6, 2011 at 17:10
  • 2
    Perhaps you're referring to the phrase "politically motivated". That does call for evidence, as opposed to the rest of my answer which is just that there's nothing "safer" about the "safe" functions. Of course if you make a gratuitiously incompatible interface to do something that existing interfaces do perfectly well, and falsely describe the existing standard interfaces as "unsafe" and even add annoying compiler warnings to try to prevent people from using the standard interfaces, it's pretty clear you have a motive to undermine standards. Aug 6, 2011 at 17:15
  • @R: just use standard functions correctly, and then define _SCL_SECURE_NO_WARNINGS and _STL_SECURE_NO_WARNINGS. Aug 10, 2011 at 23:25
0

On Windows I suggest to use CreateFile unless there is a good reason not to do so. On Linux "open" maps to kernel function, on Windows - to library function, that uses "CreateFile" anyway. As per portability, this function is somewhat more portable on Microsoft platforms (Win32/64/CE). And, naturally, not available on *nix.

1
  • 1
    I think the OP wasn't looking for a solution that would require that he also change all the reads/write/whatever calls too. May 19, 2012 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.