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. VC++ pointers with Readfile function [modified]

VC++ pointers with Readfile function [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionannouncementlearning
7 Posts 4 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.
  • G Offline
    G Offline
    GC104
    wrote on last edited by
    #1

    I am learning to use VS2005 to create a VC++ MFC project to read from the serial port using the 'ReadFile' function. in particular I am having problems getting the code to compile 'ReadFile' parameter 4: h-file ... DWORD ActualBytesRead DWORD * pActualBytesRead ... cpp-file attempt 1: pActualBytesRead = &ActualBytesRead ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, pActualBytesRead, NULL) -compiles, but parameter 4 ends up with 0xbaadF00d at the time of the 'ReadFile' call? attempt 2: ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, &ActualBytesRead, NULL) -compiles (passing by reference?) attempt 3: pActualBytesRead = &ActualBytesRead ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, *pActualBytesRead, NULL) -won't compile? OK, so I have a version of code that compiles but I don't really understand why attempt 1 & 3 fail?

    modified on Tuesday, September 8, 2009 11:06 AM

    _ D 2 Replies Last reply
    0
    • G GC104

      I am learning to use VS2005 to create a VC++ MFC project to read from the serial port using the 'ReadFile' function. in particular I am having problems getting the code to compile 'ReadFile' parameter 4: h-file ... DWORD ActualBytesRead DWORD * pActualBytesRead ... cpp-file attempt 1: pActualBytesRead = &ActualBytesRead ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, pActualBytesRead, NULL) -compiles, but parameter 4 ends up with 0xbaadF00d at the time of the 'ReadFile' call? attempt 2: ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, &ActualBytesRead, NULL) -compiles (passing by reference?) attempt 3: pActualBytesRead = &ActualBytesRead ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, *pActualBytesRead, NULL) -won't compile? OK, so I have a version of code that compiles but I don't really understand why attempt 1 & 3 fail?

      modified on Tuesday, September 8, 2009 11:06 AM

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      1 and 3 fails because you're passing a DWORD** instead of DWORD*. The following should compile.

      DWORD ActualBytesRead = 0;
      BYTES ReceiverBuffer[1024];
      DWORD NumberOfBytes2Read = 1024;

      ReadFileStatus = ReadFile(SerialPortHandle, ReceiverBuffer, NumberOfBytes2Read, &ActualBytesRead, NULL);

      «_Superman_» I love work. It gives me something to do between weekends.

      C 1 Reply Last reply
      0
      • G GC104

        I am learning to use VS2005 to create a VC++ MFC project to read from the serial port using the 'ReadFile' function. in particular I am having problems getting the code to compile 'ReadFile' parameter 4: h-file ... DWORD ActualBytesRead DWORD * pActualBytesRead ... cpp-file attempt 1: pActualBytesRead = &ActualBytesRead ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, pActualBytesRead, NULL) -compiles, but parameter 4 ends up with 0xbaadF00d at the time of the 'ReadFile' call? attempt 2: ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, &ActualBytesRead, NULL) -compiles (passing by reference?) attempt 3: pActualBytesRead = &ActualBytesRead ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, *pActualBytesRead, NULL) -won't compile? OK, so I have a version of code that compiles but I don't really understand why attempt 1 & 3 fail?

        modified on Tuesday, September 8, 2009 11:06 AM

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        GC104 wrote:

        ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, pActualBytesRead, NULL) -won't compile, even though I am passing a pointer to a 'DWORD'?

        This compiles for me.

        GC104 wrote:

        ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, *pActualBytesRead, NULL) -won't compile?

        Because argument 4 is the contents, or value, of pActualBytesRead.

        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        G 1 Reply Last reply
        0
        • _ _Superman_

          1 and 3 fails because you're passing a DWORD** instead of DWORD*. The following should compile.

          DWORD ActualBytesRead = 0;
          BYTES ReceiverBuffer[1024];
          DWORD NumberOfBytes2Read = 1024;

          ReadFileStatus = ReadFile(SerialPortHandle, ReceiverBuffer, NumberOfBytes2Read, &ActualBytesRead, NULL);

          «_Superman_» I love work. It gives me something to do between weekends.

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          «_Superman_» wrote:

          1 and 3 fails because you're passing a DWORD** instead of DWORD*.

          Actually, in the first case he is passing a DWORD* (so this should compile) and in the third case he is passing a DWORD.

          Cédric Moonen Software developer
          Charting control [v2.0] OpenGL game tutorial in C++

          1 Reply Last reply
          0
          • D David Crow

            GC104 wrote:

            ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, pActualBytesRead, NULL) -won't compile, even though I am passing a pointer to a 'DWORD'?

            This compiles for me.

            GC104 wrote:

            ReadFileStatus = ReadFile(SerialPortHandle, &ReceiverBuffer, NumberOfBytes2Read, *pActualBytesRead, NULL) -won't compile?

            Because argument 4 is the contents, or value, of pActualBytesRead.

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            G Offline
            G Offline
            GC104
            wrote on last edited by
            #5

            Since posting the original message, I would expect attempt 1 to compile. Is this likely to be a quirk in the VS2005 VC++ compiler? Many thanks for thoughts. Geoff :)

            D 1 Reply Last reply
            0
            • G GC104

              Since posting the original message, I would expect attempt 1 to compile. Is this likely to be a quirk in the VS2005 VC++ compiler? Many thanks for thoughts. Geoff :)

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Without seeing the actual error message, I've no idea.

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              G 1 Reply Last reply
              0
              • D David Crow

                Without seeing the actual error message, I've no idea.

                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                G Offline
                G Offline
                GC104
                wrote on last edited by
                #7

                Sorry, I made a typo whilst trying 'attempt 1'. Yolu're right it does compile, the problem is that pActualBytesRead gets loaded with 0xbaadf00d prior to the 'ReadFile' call. Thanks for help :)

                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