Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. _open generate "Access is denied" error

_open generate "Access is denied" error

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++linuxquestion
18 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    Confidence that I have solved all strange errors here, I come back with some issue generated by a legacy C code for linux, code that I intend to use it in a MFC app in windows OS. Ok. I have a simple code:

    int nRespond = _open(device, 020);
    UINT err = ::GetLastError();

    where devide is const char* and has value C (or D:, or E: ) The nRespond is -1 and err has value 5, which is mean Access is denied. What could be the problem here ? I ran the test app as administrator mode (ran from VS2017 as admin mode).

    G L L 3 Replies Last reply
    0
    • _ _Flaviu

      Confidence that I have solved all strange errors here, I come back with some issue generated by a legacy C code for linux, code that I intend to use it in a MFC app in windows OS. Ok. I have a simple code:

      int nRespond = _open(device, 020);
      UINT err = ::GetLastError();

      where devide is const char* and has value C (or D:, or E: ) The nRespond is -1 and err has value 5, which is mean Access is denied. What could be the problem here ? I ran the test app as administrator mode (ran from VS2017 as admin mode).

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      The problem appears to be that you are expecting Linux code to work on Windows. The _open function opens a file - on unix systems you can open directories and devices as files, but not on Windows. If you're lucky the code is actually expecting a normal file and you just passed in a device letter by mistake. Otherwise you will have to port the code, replacing the Linux system functions with code that does the same thing on Windows.

      1 Reply Last reply
      0
      • _ _Flaviu

        Confidence that I have solved all strange errors here, I come back with some issue generated by a legacy C code for linux, code that I intend to use it in a MFC app in windows OS. Ok. I have a simple code:

        int nRespond = _open(device, 020);
        UINT err = ::GetLastError();

        where devide is const char* and has value C (or D:, or E: ) The nRespond is -1 and err has value 5, which is mean Access is denied. What could be the problem here ? I ran the test app as administrator mode (ran from VS2017 as admin mode).

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You cannot access raw devices on Windows by their drive letters, you must use their volume identifiers. See File path formats on Windows systems | Microsoft Docs[^].

        _ 1 Reply Last reply
        0
        • _ _Flaviu

          Confidence that I have solved all strange errors here, I come back with some issue generated by a legacy C code for linux, code that I intend to use it in a MFC app in windows OS. Ok. I have a simple code:

          int nRespond = _open(device, 020);
          UINT err = ::GetLastError();

          where devide is const char* and has value C (or D:, or E: ) The nRespond is -1 and err has value 5, which is mean Access is denied. What could be the problem here ? I ran the test app as administrator mode (ran from VS2017 as admin mode).

          L Offline
          L Offline
          leon de boer
          wrote on last edited by
          #4

          Your flags (020) are linux flags not windows try ( _O_WRONLY | _O_CREAT ) or some normal windows flags _open, _wopen | Microsoft Docs[^] I would add even in linux the code really should be written as flags not a value like that for this exact reason.

          In vino veritas

          _ 2 Replies Last reply
          0
          • L leon de boer

            Your flags (020) are linux flags not windows try ( _O_WRONLY | _O_CREAT ) or some normal windows flags _open, _wopen | Microsoft Docs[^] I would add even in linux the code really should be written as flags not a value like that for this exact reason.

            In vino veritas

            _ Offline
            _ Offline
            _Flaviu
            wrote on last edited by
            #5

            I have tried this:

            int nRespond = _open(device, _O_WRONLY | _O_CREAT);
            UINT e = ::GetLastError();

            with the same result (error 5). Strange ....

            1 Reply Last reply
            0
            • L Lost User

              You cannot access raw devices on Windows by their drive letters, you must use their volume identifiers. See File path formats on Windows systems | Microsoft Docs[^].

              _ Offline
              _ Offline
              _Flaviu
              wrote on last edited by
              #6

              So I cannot open disk with this function ? ... this is the original code ... strange ...

              L 1 Reply Last reply
              0
              • L leon de boer

                Your flags (020) are linux flags not windows try ( _O_WRONLY | _O_CREAT ) or some normal windows flags _open, _wopen | Microsoft Docs[^] I would add even in linux the code really should be written as flags not a value like that for this exact reason.

                In vino veritas

                _ Offline
                _ Offline
                _Flaviu
                wrote on last edited by
                #7

                I also tried this:

                #include int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
                {

                \_open("D:", 0x02 | 0x04);
                UINT e = ::GetLastError();
                cout << e;
                

                }

                from cmd line as administrator rights … the same result: 5 (access is denied).

                L 1 Reply Last reply
                0
                • _ _Flaviu

                  So I cannot open disk with this function ? ... this is the original code ... strange ...

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  _Flaviu wrote:

                  this is the original code

                  From where?

                  _ 1 Reply Last reply
                  0
                  • _ _Flaviu

                    I also tried this:

                    #include int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
                    {

                    \_open("D:", 0x02 | 0x04);
                    UINT e = ::GetLastError();
                    cout << e;
                    

                    }

                    from cmd line as administrator rights … the same result: 5 (access is denied).

                    L Offline
                    L Offline
                    leon de boer
                    wrote on last edited by
                    #9

                    Big Errors You can't open "D:" that isn't a file ... its not linux you don't mount drives Try

                    int nRespond = _open("D:\\somefilename.txt", _O_WRONLY | _O_CREAT);
                    UINT e = ::GetLastError();

                    I am sure it will open :-)

                    In vino veritas

                    _ 1 Reply Last reply
                    0
                    • L leon de boer

                      Big Errors You can't open "D:" that isn't a file ... its not linux you don't mount drives Try

                      int nRespond = _open("D:\\somefilename.txt", _O_WRONLY | _O_CREAT);
                      UINT e = ::GetLastError();

                      I am sure it will open :-)

                      In vino veritas

                      _ Offline
                      _ Offline
                      _Flaviu
                      wrote on last edited by
                      #10

                      Agree. Is there any windows methods to open a disk and get the handle ? Because this nResponse it is used further as a handle ...

                      V 1 Reply Last reply
                      0
                      • L Lost User

                        _Flaviu wrote:

                        this is the original code

                        From where?

                        _ Offline
                        _ Offline
                        _Flaviu
                        wrote on last edited by
                        #11

                        I get this code from a plain C code which has built for Linux:

                        int nRespond = _open(device, 020);

                        L 1 Reply Last reply
                        0
                        • _ _Flaviu

                          I get this code from a plain C code which has built for Linux:

                          int nRespond = _open(device, 020);

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          You cannot run Linux code on Windows without adjusting it for the differences. Windows does not let you address raw devices in the same way that Linux does. And in fact doing so is very dangerous as you could destroy your entire system.

                          _ 1 Reply Last reply
                          0
                          • L Lost User

                            You cannot run Linux code on Windows without adjusting it for the differences. Windows does not let you address raw devices in the same way that Linux does. And in fact doing so is very dangerous as you could destroy your entire system.

                            _ Offline
                            _ Offline
                            _Flaviu
                            wrote on last edited by
                            #13

                            And is any other method to open a device rather than _open in Windows ? I think I have to made some changes into Linux code to run on Windows ...

                            L 1 Reply Last reply
                            0
                            • _ _Flaviu

                              And is any other method to open a device rather than _open in Windows ? I think I have to made some changes into Linux code to run on Windows ...

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              I gave you a link in my first message above which shows details of how to address devices in Windows. However, the real question is, what exactly are you trying to do?

                              1 Reply Last reply
                              0
                              • _ _Flaviu

                                Agree. Is there any windows methods to open a disk and get the handle ? Because this nResponse it is used further as a handle ...

                                V Offline
                                V Offline
                                Victor Nijegorodov
                                wrote on last edited by
                                #15

                                Yes. It is [CreateFile function (fileapi.h) | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea)

                                _ 1 Reply Last reply
                                0
                                • V Victor Nijegorodov

                                  Yes. It is [CreateFile function (fileapi.h) | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea)

                                  _ Offline
                                  _ Offline
                                  _Flaviu
                                  wrote on last edited by
                                  #16

                                  Good idea. I have used CreateFileA, and I get rid of that "access denied". But there a thing that I had afraid: the original code, with _open returned int, and CreateFileA return HANDLE ... casting HANDLE to int is OK ? I guess not ...

                                  V L 2 Replies Last reply
                                  0
                                  • _ _Flaviu

                                    Good idea. I have used CreateFileA, and I get rid of that "access denied". But there a thing that I had afraid: the original code, with _open returned int, and CreateFileA return HANDLE ... casting HANDLE to int is OK ? I guess not ...

                                    V Offline
                                    V Offline
                                    Victor Nijegorodov
                                    wrote on last edited by
                                    #17

                                    How are you going to use the handle returned from _open ?

                                    1 Reply Last reply
                                    0
                                    • _ _Flaviu

                                      Good idea. I have used CreateFileA, and I get rid of that "access denied". But there a thing that I had afraid: the original code, with _open returned int, and CreateFileA return HANDLE ... casting HANDLE to int is OK ? I guess not ...

                                      L Offline
                                      L Offline
                                      leon de boer
                                      wrote on last edited by
                                      #18

                                      Windows has GetLastError you notice CreateFile Simply returns invalid handle for an error if you get that then you call GetLastError GetLastError function (errhandlingapi.h) | Microsoft Docs[^] That is the equivalent of your original int it's just a non zero number identifying the error, 0 always equals no error.

                                      In vino veritas

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups