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. Passing class pointer via socket

Passing class pointer via socket

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++xmlperformancetutorial
12 Posts 8 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.
  • D Offline
    D Offline
    Don Guy
    wrote on last edited by
    #1

    Hey there, I have a function called, App 1: void GetImage(CImage * img) { //Pass &img over socket to another app } App 2: void DisplayImage() { CImage * pImg = &img; } Is it possible to pass a class pointer as memory buffer across the socket? The above code is just an example. My question in general is, whether it's possible to pass any Classes pointer as a memory buffer across sockets. UPDATE: This is within same process. In that case can i simply pass the pointer over socket? Another catch here is, i need to encode all the data in a XML format before sending across the socket. C++ Code: CImage *pImage; //pImage hold the data Inside XML: &pImage Can i do like this? Thanks

    Richard Andrew x64R E E R A 7 Replies Last reply
    0
    • D Don Guy

      Hey there, I have a function called, App 1: void GetImage(CImage * img) { //Pass &img over socket to another app } App 2: void DisplayImage() { CImage * pImg = &img; } Is it possible to pass a class pointer as memory buffer across the socket? The above code is just an example. My question in general is, whether it's possible to pass any Classes pointer as a memory buffer across sockets. UPDATE: This is within same process. In that case can i simply pass the pointer over socket? Another catch here is, i need to encode all the data in a XML format before sending across the socket. C++ Code: CImage *pImage; //pImage hold the data Inside XML: &pImage Can i do like this? Thanks

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      Don Guy wrote:

      My question in general is, whether it's possible to pass any Classes pointer as a memory buffer across sockets

      That depends. Are you passing it between different processes? If so, then the pointer will have no meaning in the context of the receiving process. Remember that each process has its own allocation of memory that is separate from every other process. Therefore a memory pointer has no meaning outside its own process. However, you could send the object itself through a process of serialization: http://msdn.microsoft.com/en-us/library/6bz744w8.aspx[^]

      The difficult we do right away... ...the impossible takes slightly longer.

      P 1 Reply Last reply
      0
      • D Don Guy

        Hey there, I have a function called, App 1: void GetImage(CImage * img) { //Pass &img over socket to another app } App 2: void DisplayImage() { CImage * pImg = &img; } Is it possible to pass a class pointer as memory buffer across the socket? The above code is just an example. My question in general is, whether it's possible to pass any Classes pointer as a memory buffer across sockets. UPDATE: This is within same process. In that case can i simply pass the pointer over socket? Another catch here is, i need to encode all the data in a XML format before sending across the socket. C++ Code: CImage *pImage; //pImage hold the data Inside XML: &pImage Can i do like this? Thanks

        E Offline
        E Offline
        Erudite_Eric
        wrote on last edited by
        #3

        Don Guy wrote:

        Is it possible to pass a class pointer as memory buffer across the socket?

        Of course you can. But your application will crash, acchieve nothing and be completely useless. You can pass the entire class if you want, and all referenced memory. And the other end must know about that class, and all the funcitons is contains.

        1 Reply Last reply
        0
        • D Don Guy

          Hey there, I have a function called, App 1: void GetImage(CImage * img) { //Pass &img over socket to another app } App 2: void DisplayImage() { CImage * pImg = &img; } Is it possible to pass a class pointer as memory buffer across the socket? The above code is just an example. My question in general is, whether it's possible to pass any Classes pointer as a memory buffer across sockets. UPDATE: This is within same process. In that case can i simply pass the pointer over socket? Another catch here is, i need to encode all the data in a XML format before sending across the socket. C++ Code: CImage *pImage; //pImage hold the data Inside XML: &pImage Can i do like this? Thanks

          E Offline
          E Offline
          ExcellentOrg
          wrote on last edited by
          #4

          Socket is just an unstructured byte stream, so you can certainly pass the pointer address as bytes on to other end. You will have to marshal pointer into bytes @ sending end and unmarshal it back to pointer at receiving end. To unmarshal, the receiver must either share the same address space as the sender or be provided a proxy to the same address space. Oh, does this start to sound like Remoting? Yep, that is what this is. Remoting does provide means to make a process function pointers visible to another process and Remoting does involve Sockets and passing of pointers to class. (Okay Okay, they're called reference) So, Study Remoting !!.

          1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            Don Guy wrote:

            My question in general is, whether it's possible to pass any Classes pointer as a memory buffer across sockets

            That depends. Are you passing it between different processes? If so, then the pointer will have no meaning in the context of the receiving process. Remember that each process has its own allocation of memory that is separate from every other process. Therefore a memory pointer has no meaning outside its own process. However, you could send the object itself through a process of serialization: http://msdn.microsoft.com/en-us/library/6bz744w8.aspx[^]

            The difficult we do right away... ...the impossible takes slightly longer.

            P Offline
            P Offline
            pasztorpisti
            wrote on last edited by
            #5

            This is the good solution but "serialization" is usually a blurry word for beginners, I try to give OP a bit of help about serialization. Serialization is the process of converting one or more objects into a self contained piece of data that is self-contained/independent of the volatile state of your program. You do serialization for example when you save out a document into a file. The document data is self-contained and independent of the volatile state of your program because you can perfectly load it back to your program even after a program restart (that causes total loss of volatile state info). When you serialize you don't necessarily have to save the data into a file, instead you can send it over the network to another process that can "load" (deserialize) this data by creating objects that contain the same information from which the serialized data was created in the first process. Of course the pointerss to the objects will be probably different but the connection between the objects and their state variables can be identical.

            1 Reply Last reply
            0
            • D Don Guy

              Hey there, I have a function called, App 1: void GetImage(CImage * img) { //Pass &img over socket to another app } App 2: void DisplayImage() { CImage * pImg = &img; } Is it possible to pass a class pointer as memory buffer across the socket? The above code is just an example. My question in general is, whether it's possible to pass any Classes pointer as a memory buffer across sockets. UPDATE: This is within same process. In that case can i simply pass the pointer over socket? Another catch here is, i need to encode all the data in a XML format before sending across the socket. C++ Code: CImage *pImage; //pImage hold the data Inside XML: &pImage Can i do like this? Thanks

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #6

              Sockets are just one of the many ways to communicate, especially across processes. It's technically possible to pass any pointer using a socket without regards to what it's pointing to. But if you pass a raw pointer from one process to another (using whatever mechanism, not just sockets), it immediately becomes meaningless because pointers are not portable across processes.

              "Real men drive manual transmission" - Rajesh.

              1 Reply Last reply
              0
              • D Don Guy

                Hey there, I have a function called, App 1: void GetImage(CImage * img) { //Pass &img over socket to another app } App 2: void DisplayImage() { CImage * pImg = &img; } Is it possible to pass a class pointer as memory buffer across the socket? The above code is just an example. My question in general is, whether it's possible to pass any Classes pointer as a memory buffer across sockets. UPDATE: This is within same process. In that case can i simply pass the pointer over socket? Another catch here is, i need to encode all the data in a XML format before sending across the socket. C++ Code: CImage *pImage; //pImage hold the data Inside XML: &pImage Can i do like this? Thanks

                A Offline
                A Offline
                Albert Holguin
                wrote on last edited by
                #7

                As others have stated, you could pass pointers, but they would be meaningless to the other program since he can't access the memory addresses of the other exe (unless, it's "shared memory", continue reading). Your best bet would be to either transfer all the bytes of the image being referenced (or object, using serialization or other similar methods) or if both programs are local, let the client know where to find the image. One common form of inter-process communications (IPC) is using "shared memory"[^]. In this case, one application would set up a shared memory pool that others can access. This is typically done for applications that need to work extremely fast (and all have direct access to the shared memory location) and don't want the overhead (or need the flexibility) of other IPC options (such as sockets or pipes).

                D 1 Reply Last reply
                0
                • A Albert Holguin

                  As others have stated, you could pass pointers, but they would be meaningless to the other program since he can't access the memory addresses of the other exe (unless, it's "shared memory", continue reading). Your best bet would be to either transfer all the bytes of the image being referenced (or object, using serialization or other similar methods) or if both programs are local, let the client know where to find the image. One common form of inter-process communications (IPC) is using "shared memory"[^]. In this case, one application would set up a shared memory pool that others can access. This is typically done for applications that need to work extremely fast (and all have direct access to the shared memory location) and don't want the overhead (or need the flexibility) of other IPC options (such as sockets or pipes).

                  D Offline
                  D Offline
                  Don Guy
                  wrote on last edited by
                  #8

                  UPDATE: This is within same process. In that case can i simply pass the pointer over socket? Another catch here is, i need to encode all the data in a XML format before sending across the socket. C++ Code: CImage *pImage; //pImage hold the data Inside XML: &pImage Can i do like this?

                  A R 2 Replies Last reply
                  0
                  • D Don Guy

                    UPDATE: This is within same process. In that case can i simply pass the pointer over socket? Another catch here is, i need to encode all the data in a XML format before sending across the socket. C++ Code: CImage *pImage; //pImage hold the data Inside XML: &pImage Can i do like this?

                    A Offline
                    A Offline
                    Albert Holguin
                    wrote on last edited by
                    #9

                    If it's within the same application, then yes... but if you're within the same application, why are you using a socket at all? Sockets are used to enable communications across multiple applications, not within the same application (you could but that doesn't mean you should, it's a lot of overhead for nothing).

                    1 Reply Last reply
                    0
                    • D Don Guy

                      UPDATE: This is within same process. In that case can i simply pass the pointer over socket? Another catch here is, i need to encode all the data in a XML format before sending across the socket. C++ Code: CImage *pImage; //pImage hold the data Inside XML: &pImage Can i do like this?

                      R Offline
                      R Offline
                      Rajesh R Subramanian
                      wrote on last edited by
                      #10

                      I am unsure why are you using sockets, and I feel that there may be better options to choose from if you're having just one process. If you could explain the situation with the required information (see this post[^] for help on what information to include in your question), someone here might be able to offer you a good alternative.

                      "Real men drive manual transmission" - Rajesh.

                      1 Reply Last reply
                      0
                      • D Don Guy

                        Hey there, I have a function called, App 1: void GetImage(CImage * img) { //Pass &img over socket to another app } App 2: void DisplayImage() { CImage * pImg = &img; } Is it possible to pass a class pointer as memory buffer across the socket? The above code is just an example. My question in general is, whether it's possible to pass any Classes pointer as a memory buffer across sockets. UPDATE: This is within same process. In that case can i simply pass the pointer over socket? Another catch here is, i need to encode all the data in a XML format before sending across the socket. C++ Code: CImage *pImage; //pImage hold the data Inside XML: &pImage Can i do like this? Thanks

                        W Offline
                        W Offline
                        WuRunZhe
                        wrote on last edited by
                        #11

                        It's very interesting. In my own opinion, it is possible but you need something. First, Receiver process have to open Sender process(use OpenProcess api with PROCESS_VM_READ) with memory access authority. Second, Sender process have to allocate class instance on allocated memory located on it's own process memory(I don't know about it but if you use Google, it will appears). Solving these problems may be difficult and complicated but it is possible, I think. :)

                        1 Reply Last reply
                        0
                        • D Don Guy

                          Hey there, I have a function called, App 1: void GetImage(CImage * img) { //Pass &img over socket to another app } App 2: void DisplayImage() { CImage * pImg = &img; } Is it possible to pass a class pointer as memory buffer across the socket? The above code is just an example. My question in general is, whether it's possible to pass any Classes pointer as a memory buffer across sockets. UPDATE: This is within same process. In that case can i simply pass the pointer over socket? Another catch here is, i need to encode all the data in a XML format before sending across the socket. C++ Code: CImage *pImage; //pImage hold the data Inside XML: &pImage Can i do like this? Thanks

                          E Offline
                          E Offline
                          Erudite_Eric
                          wrote on last edited by
                          #12

                          Oh yeah, watch out for endian ness.

                          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