-
October 14th, 2013, 06:49 PM
#1
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
-
October 14th, 2013, 07:40 PM
#2
Re: Passing class pointer via socket
Within the same process? Yes. Within different processes? (generally) No.
-
October 15th, 2013, 06:44 AM
#3
Re: Passing class pointer via socket
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.
Last edited by Igor Vartanov; October 15th, 2013 at 06:47 AM.
Best regards,
Igor
-
October 15th, 2013, 08:16 AM
#4
Re: Passing class pointer via socket
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
-
October 15th, 2013, 11:48 AM
#5
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?
-
October 15th, 2013, 11:49 AM
#6
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?
-
October 15th, 2013, 02:53 PM
#7
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 15th, 2013, 02:55 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|