How to use the CopyMemory() API simply?

Dear all,

I want to know how to use the CopyMemory() to copy an interger or word variable to an char[] buffer simply.

char * sendBuf;
sendBuf = new char[100];

ZeroMemory(sendBuf,100);

CopyMemory(sendBuf,"ABC",3);

int len = 5;

CopyMemory(sendBuf+3,&len,2);

-- This one cannot work well.

Do I have to convert the int variable to CString etc first?

Thank you in advance!

Best Regards,
HWB
[592 byte] By [hardworkboy] at [2007-11-19 16:05:27]
# 1 Re: How to use the CopyMemory() API simply?
Dear all,

I want to know how to use the CopyMemory() to copy an interger or word variable to an char[] buffer simply.

char * sendBuf;
sendBuf = new char[100];

ZeroMemory(sendBuf,100);

CopyMemory(sendBuf,"ABC",3);

int len = 5;

CopyMemory(sendBuf+3,&len,2);

-- This one cannot work well.

Do I have to convert the int variable to CString etc first?

Thank you in advance!

Best Regards,
HWB
Here is a simple example

void test(char *pbData, unsigned int cbData)
{
char buf[BUFFER_SIZE];
CopyMemory(buf, pbData, min(cbData,BUFFER_SIZE));
}
humptydumpty at 2007-11-10 23:54:21 >
# 2 Re: How to use the CopyMemory() API simply?
why using ::CopyMemory(..) in that case? you can simply do:

char * sendBuf = new char[100];
::ZeroMemory(sendBuf,100);
int len = 5;
sprintf(sendBuf,"%s %d","ABC",len);

// dont forget to delete []sendBuf when done

Cheers
golanshahar at 2007-11-10 23:55:16 >
# 3 Re: How to use the CopyMemory() API simply?
Dear Golanshaha,

Thank you for your reply.

Actually I was just make my question simple. The real problem is that I have to use CopyMemory() in EVC. I need to copy some unicode charaters, then the number of the characters, then some other characters, then the number. If using the CString, like following, it does't work.

char * sendBuf;
sendBuf = new char[100];
ZeroMemory(sendBuf,100);

CString firstString = "ABC";

CopyMemory(sendBuf,firstString,6);

So, the first question is that I want to know the details on how the Unicode characters are stored in the memory.

Then I ask the above question. The purpose is test how an interger or other variables are stored in the mem. I understand you want to help me to put the "information" of " how many". Yours method is actually to change the integer to char to store to the mem.


After all, thank you and sorry for my not clear expressions.

Sincerely,
HWB
hardworkboy at 2007-11-10 23:56:17 >
# 4 Re: How to use the CopyMemory() API simply?
If using the CString, like following, it does't work.What exactly doesn't work?

So, the first question is that I want to know the details on how the Unicode characters are stored in the memory. To find this out you never need to copy CString to buffer, since CString already has its own. You just access it properly... :)
CStringW sOrig(L"ABC");
LPBYTE s = (LPBYTE)(LPCWSTR)sOrig;
wprintf(L"%02X%02X%02X%02X%02X%02X", s[0],s[1],s[2],s[3],s[4],s[5]);

Then I ask the above question. The purpose is test how an interger or other variables are stored in the mem.It depends on memory storage convention for specific processor type. You know, big-endian, little-endian...
Igor Vartanov at 2007-11-10 23:57:18 >
# 5 Re: How to use the CopyMemory() API simply?
Thank you, Igor,

I want to send a frame from Pocket PC to PC with the following format:

"ABCD"--head

len -- the length of the characters

the char[] -- the content

"WXYZ" -- tail

In consideration of the Unicode, for the char[], I use a CString variable.

In the receiving part, I can only fetch the "ABCD" head, then the "len" and the following info cannot be traced.

Then I asked the above questions. I just use CopyMemory() to copy every part inside. Quite confused. I try to test to use CopyMemory() to copy a CString like "ABC" into the char buffer but got only "A "copied.

Maybe I should follow your advice to put all the information I want to send to a CString variable buffer.

Thank you very much!

HWB
hardworkboy at 2007-11-10 23:58:19 >
# 6 Re: How to use the CopyMemory() API simply?
Dear All,

Sorry for the confusion I brought in "How to use the CopyMemory() API simply? "

Actually I am trying to send some formatted "Frame" from 1 Pocket PC(EVC) to PC( VC). I want to make the frame format as following:

"ABCD" -- head
len ( int variable) -- to stand for the length of the contents

contents

"WXYZ" -- tail

As I need to transfer the information, I use the CopyMemory() API function to put all the above information into some sendbuffer ( char * sendBuf). On the receiving side, I received the whole buffer, but just cannot fetch out the corresponding information. Moreover, I still need to consider the Unicode on the EVC side.

So I post the "How to use the CopyMemory() API simply? " post and give us lots of confusion. I am very sorry. Hope this time I express my question clearly.

Sincerely,
HWB
hardworkboy at 2007-11-10 23:59:23 >
# 7 Re: How to use the CopyMemory() API simply?
In the receiving part, I can only fetch the "ABCD" head, then the "len" and the following info cannot be traced. First I'd like to know what do you mean saying 'send/receive'. Do you use some network protocol? If so, you have to translate data to network byte order at TX side and translate them back to host order at the RX side. Do you? (Dont ask me why, since I already told you about big-endian and little-endian stuff...)

Then I asked the above questions. I just use CopyMemory() to copy every part inside. Quite confused. I try to test to use CopyMemory() to copy a CString like "ABC" into the char buffer but got only "A "copied.Frankly saying I doubt if it so. Well, the questions is how you were trying to read the string you've copied. Since your buffer was declared as char[] the first zero byte encountered will be interpreted as string termination symbol, but in fact it's just a second byte of unicode 'A'. :)

In case you've put the unicode text, you have to cast char[] to WCHAR* to see unicode after copying. Ain't it obvious?

Another way is to see memory dump in a debugger Memory window.

Maybe I should follow your advice to put all the information I want to send to a CString variable buffer.If I were you I'd do this only been in a total desperation. ;)

BTW, as I can see you've paid no attention to my download utility (http://www.dev-archive.com/forum/showthread.php?p=1281753#post1281753). Don't you need to download dlls on build anymore? :)
Igor Vartanov at 2007-11-11 0:00:21 >
# 8 Re: How to use the CopyMemory() API simply?
Yes. I am trying to send the information via the UDP protocol.

Actually if I only send the "ABCD" -- the head, I can fetch it on the receiving side. But when I send the len, the information following the "len" seems strange. You know, to chang this length info to char[] or CString is not a good idea. like if the length is 65, I could not decide if it is 6 following by the info begins with 5.

You are really an expert on CString and char []. Thank you for your info.

But I don't think you need to consider the network order or host order for the info you sent. Only the ip address structure need the byte order transfer.

Actually I try to use byte[] to store the info, much better than char [].

byte buffer[10];
CopyMemory(buffer,"ABCD",4);

int len = 165;

CopyMemory(buffer+4,&len,2);

CopyMemory(buffer+6,"CDEF",4);

char a[4],b[4];
CopyMemory(a,buffer,4);
CopyMemory(b,buffer+6,4);

int getLen = *(buffer+4);
hardworkboy at 2007-11-11 0:01:29 >
# 9 Re: How to use the CopyMemory() API simply?
Yes. I am trying to send the information via the UDP protocol.

Actually if I only send the "ABCD" -- the head, I can fetch it on the receiving side. But when I send the len, the information following the "len" seems strange.Do you send all info in a single packet? Or in a separate ones? If latter, how do you tell the packet type? You cannot rely the packets will come to reciever in the order they were posted, because of UDP.

You are really an expert on CString and char []. Thank you for your info.Well, I feel some irony in your words... I don't think I deserve it.

But I don't think you need to consider the network order or host order for the info you sent. Only the ip address structure need the byte order transfer.Pity you think so. This is true only for case TX and RX side processors have the same endian convention. But in this case even ip address doesn't need byte-order translation. :)

Actually I try to use byte[] to store the info, much better than char [].Actually I'm confused.
You mixes in your code CString (which is always unicode on WinCE), byte[] (non-unicode, though interpretable), "ABCD" (non-unicode literal).
Your task description is misty. As well as error description is uncertain.

It's really hard to advise something under the circumstances except trivial "clarify concepts, use debugger, brush up the code". :)
Igor Vartanov at 2007-11-11 0:02:24 >
# 10 Re: How to use the CopyMemory() API simply?
Dear Igor Vartanov,

As far as I know, for windows socket programming, we don't need to care much about the big-ending little-ending problem. I don't think all the PCs will use intel processor or AMD processor. Maybe you have such kind of examples. Pls tell me.

I am really confused about the difference of char [] byte[] and CString. I just try to use any of them to get my purpose of fetching the info from the receiving side.

By the way, to interprete others' praise as irony is something not good.

Sincerely,
HWB
hardworkboy at 2007-11-11 0:03:30 >
# 11 Re: How to use the CopyMemory() API simply?
Dear Igor Vartanov,

Can we put the little-end and big-end aside first? Please allow me to try my best to express my question clear.

I need to send some infor such as " I am tall". Since for the send() function, we have to use char[] to store the info, we can use CopyMemory() to put the info "ABCD""9(the info length)" "I am tall" "WXYZ" to the buffer. If we debug, we can find that the sending buffer is like"ABCD*", "*" stands for some strange signs. BUT I can not find the "WXYZ" info. This may due to integer info "9".

On the receiving side, I have received the same info. But similarly, I can not extract the info following "ABCD". In order to find the following info, I asked all the above questions( some may be quite silly). I tried to transfer the char[] into CString, due to the "space" included, I failed. I tried to use BYTE[] to replace the char[], it seems I can find the info, but I need to modify the send() function.

Since you really do well in this char[] byte[] etc related field(Believe I am NOT ironize AT ALL), I hope you can help me out.


Best Regards
hardworkboy at 2007-11-11 0:04:22 >
# 12 Re: How to use the CopyMemory() API simply?
By the way, to interprete others' praise as irony is something not good.Sorry, my fault. And my apologies.

As far as I know, for windows socket programming, we don't need to care much about the big-ending little-ending problem. I don't think all the PCs will use intel processor or AMD processor. Maybe you have such kind of examples. Pls tell me.Well, for sure PowerPC processor is big-endian, so connecting to MAC OS you have to make translation of data items, which size is bigger then sizeof(char).

You told you're programming for Pocket PC. Since Pocket PC is compatible to a big number of processors and some of them may be big-endian (I'm not sure but can imagine it) so you cannot ignore this. As for me.

Now, ok, indeed let's put those endians aside. :)

I am really confused about the difference of char [] byte[] and CString. I just try to use any of them to get my purpose of fetching the info from the receiving side.The main thing we have to remember WindowsCE is UNICODE. So let's remember CString contains unicode strings. You can send non-unicode strings over TCP/IP connection but in this case you have to convert CString underlying unicode string to ANSI string explicitly.

On the other hand you may send the unicode string as it is on WinCE end, but your other end must be aware of it and behave correspondingly.

Now about char[] and byte[]. These types declare some memory region as array of char/byte. But since the memory could contain any data types (because all of them are just numbers) it's possible to write to those char/byte array wide chars for example. At the moment of writing only memory size matters. But when we need to interpret data written, we must take care of actual memory content type.

In case we will follow the formal buffer type declaration we will interpret the buffer content as chars. But since they were actually wide chars, the misinterpretation is what we have.

Sorry, I'm very busy on these days. I'd like to write a little demo for you, but I'm absolutely short of time. Maybe later? :)
Igor Vartanov at 2007-11-11 0:05:25 >