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
Re: Passing class pointer via socket
Within the same process? Yes. Within different processes? (generally) No.
Re: Passing class pointer via socket
Quote:
Originally Posted by
Don Guy
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.
Re: Passing class pointer via socket
Quote:
Originally Posted by
Don Guy
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
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?
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?
Re: Passing class pointer via socket
Quote:
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
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?