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. 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
-
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
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.
-
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
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.
-
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
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 !!.
-
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.
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.
-
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
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.
-
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
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).
-
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).
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?
-
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?
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).
-
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?
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.
-
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
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. :)
-
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
Oh yeah, watch out for endian ness.