Package transfer structure or large memory block

xiaoxiao2021-03-06  39

This article assumes that you are familiar with SAFEARRAY, C , Boost and MSMQ.

Abstract: This paper describes four packaging methods for common use of structural / large memory block distributed transmission, and demonstrates how you use these four methods to send / read data through MSMQ.

Sometimes we need to remotely transfer a variety of structures or data blocks, such as passing any size structures or interface pointers through the MSMQ message queue, how to pack delivery? This can actually decompose into a general problem: how to pack a structure object or a huge memory block (such as 5MB or so) into a binary data stream or the type of PROPVARIANT-COMPATIBLE?

This article describes four transmission methods: a BSTR; a SafeArray; Boost :: Serialization; istreAm stream. This article also describes how to send and receive / resolve these four types of data from MSMQ. The solution of BSTR's solution BSTR should be the simplest, most simple understanding of this. From BSTR Definition "A BSTR is a predetermined length OLECHAR (each 16-bit) buffer", BSTR is not equivalent to OLECHAR, and its front provides 4 bytes to retain the length of the string. BSTR really points to the fifth byte, which is the beginning of the true OleChar string. Thus we can usually use the BSTR prefix to determine how many bytes of OLECHAR are interpreted as a string, so the data may be binary data with an embedded null value (0x00). This feature is just useful for us. Another problem that needs attention is that we want to call SystringBytelen to get the number of bytes in BSTR, rather than simply calculate the number of Unicode characters. First, we give a class definition to be transmitted, which has several common types of member variables: Class A {Int i; unsigned int UI; long L; unsigned long ul; char szint [MAX_PATH]; std :: str pen Public: a (): i (std :: rand ()), UI (std :: rand ()), L (std :: rand ()), ul (std :: rand ()) {std :: Stringstream SS; SS << i; ss >> szint; ss.clear (); ss << l; ss >> strlong;}}; package is easy: a asend; bstr bstrsend = sysallocstringbytelen (null, sizeof (asend) ); Lpbyte pv = reinterpret_cast (bstrsnd); CopyMemory (PV, (Void *) & Asend, SizeOf (Asend));

Here to explain the rules of the MSMQ reception message. Smart pointer IMSMQMESSAGEPTR's body attribute receives the _variant_t parameter. So if we want to write the class object instance as a message, we need to convert to _variant_t in advance. The following code is to do this conversion: IMSMQMESSAGEPTR SPMSG ("msmq.msmqmessage"; ccombstr BSTRBody; BSTRBODY .Appendbstr (PDATA); ccomvariant varocy (bstrbody); spmsg-> body = varbody; hr = spmsg-> send (spqueue); in this way, the message is sent to MSMQ. Let's demonstrate how to unpack. A aRead; IMSMQMessagePtr pMsg; ReadMSMQMessage (spQueueRead, pMsg); UINT uiRead = SysStringByteLen (pMsg-> Body.bstrVal); LPBYTE pvRead = reinterpret_cast (pMsg-> Body.bstrVal); CopyMemory ((void *) & aRead , pvread, uiread; new class object instance AREAD data has been subjected to ASEND data.

转载请注明原文地址:https://www.9cbs.com/read-64382.html

New Post(0)