Passing class pointer via socket
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Passing class pointer via socket

  1. #1
    Join Date
    Aug 2012
    Posts
    53

    Passing class pointer via socket

    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.

    Thanks

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,358

    Re: Passing class pointer via socket

    Within the same process? Yes. Within different processes? (generally) No.

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,022

    Re: Passing class pointer via socket

    Quote Originally Posted by Don Guy View Post
    My question in general is, whether it's possible to pass any Classes pointer as a memory buffer across sockets.
    Any pointer is just an address, which is a number. So in general, yes you literally can pass this number over socket connection. But a big question is whether this operation would make any sense. Win32 process has a virtual address space, isolated from all other processes in the system. So even in source process the pointer of some value points at some sensible memory region, in destination process the pointer of the same value is going to point to something not guaranteed even to be allocated. The best case is you get an access violation error, the worst case is you corrupt the process data without noticing that.
    Last edited by Igor Vartanov; October 15th, 2013 at 06:47 AM.
    Best regards,
    Igor

  4. #4
    Join Date
    Apr 1999
    Posts
    27,433

    Re: Passing class pointer via socket

    Quote Originally Posted by Don Guy View Post
    Is it possible to pass a class pointer as memory buffer across the socket?
    You pass data over a socket, not pointers. A pointer in this aspect is not the data.

    As Igor indicated, a pointer is just an integer. If you pass a pointer, all you're doing is passing an integer, that, when received on the other side, means absolutely nothing.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Aug 2012
    Posts
    53

    Re: Passing class pointer via socket

    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?

  6. #6
    Join Date
    Aug 2012
    Posts
    53

    Re: Passing class pointer via socket

    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:

    <Param>&pImage</Param>

    Can i do like this?

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,544

    Re: Passing class pointer via socket

    This is within same process.
    Have you considered using named shared data segments?
    http://msdn.microsoft.com/en-us/library/thfhx4st.aspx
    http://msdn.microsoft.com/en-us/library/sf9b18xk.aspx

    eg

    Code:
    //Shared data segment
    //MUST INITIALSIE VARIABLES DECLARED HERE!!!!!!!!!
    #pragma data_seg("cCon")
    
    //Pointers into buffers
    volatile int	inpptr[LINESMAX][2] = {0};
    volatile int	outptr[LINESMAX][2] = {0};
    
    //Buffers for transfer
    volatile WORD	outbuf[LINESMAX][IOBUFFSIZE] = {0};
    volatile WORD	inpbuf[LINESMAX][IOBUFFSIZE] = {0};
    
    #pragma data_seg()
    #pragma comment (linker, "/SECTION:cCon,RWS")		//Read, write and shared segment
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,358

    Re: Passing class pointer via socket

    The reason I asked if you are operating within the same process is because the address of memory location (i.e. pointer) isn't going to be valid from one process to another.

    If you have a pointer to a class instance in a process and need to use that pointer in another area of the program, that pointer will be valid inside the same process.

    With this in mind, the approach you use to pass that pointer around in different parts of your program is up to you. You could use a method variable, a global variable, a memory mapped file, or even a socket to pass the [value of the] variable around.

    You could even use xml in a socket, but based on what you've written above, I think you misunderstand how this would works.

    Based on the xml snippet above, you seem to think you can pass in the variable name as a code fragment inside the xml:
    Code:
    <param>&pImage</param>
    Sure, this will get you the name of the variable, but that only helps when you're compiling. When the program is running '&pImage' isn't going to help at all. When your program is running, you are going to need the address of the variable and probably its type.
    Code:
    <param type="CImage" address="0x200032AF"/>
    That way when you extract the xml out, you can take the address of the param and cast it to the correct type in your program.

    That being said, its kind of odd to be using a socket in this fashion to be passing around info within the process. What exactly are you trying to do and why do you think you need to use a socket?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center