|
Hi everybody. I'm developing an IC card handset device application with standard C language.I'm a new comer to C language.I have a question
Look at the funtion definition :
<br />
unsigned short dt_recv(unsigned char *data, int *datalen)<br />
Input parameters: <br />
Data : to receive data buffer<br />
Output parameters: Datalen:receive data buffer size<br />
Return values: none.<br />
<br />
Communication Protocol :<br />
data[0]=command head; <br />
...<br />
data[3]=high of length; <br />
data[4]=low of length;<br />
...<br />
Data[3] and Data[4] store a Int16 value.I have problem when I try to get the value.Here is my code:
<br />
short i;<br />
memcpy(&Data[3],&i,2);<br />
But it seems that I did not get the right value.I guess I'm in the wrong way.I'm not familiar with memory operation.
Can any one give me the right way?Any idea will be appropriate.
|
|
|
|
|
are you a chinese boy?
|
|
|
|
|
My suggestion (not tested):
short i;
i = (short) ((data[3] << 8) | data[4]);
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
Xiaoming Qian wrote: short i;
memcpy(&Data[3],&i,2);
But it seems that I did not get the right value.I guess I'm in the wrong way.I'm
Are you wanting the value of Data[3] or i ? The signature for memcpy() is:
void *memcpy( void *dest, const void *src, size_t count ); So what you have is writing from i to Data[3] .
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Xiaoming Qian wrote: short i;
memcpy(&Data[3],&i,2);
On Intel machines, i is stored with the least-significant byte first. If you use memcpy like
this then
Data[3] == least-significant byte of i
Data[4] == most-significant byte of i
To get the value back. something like:
short i = (short)((unsigned short)Data[4] << 8) | (unsigned short)Data[3];
or
short i;
memcpy(&i,&Data[3],2);
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Here "data[]" is an array. so "data" itself an a address.
And instead of using "&Data[3]" use "Data[3]" in "memcpy" function
That is:
short i;
memcpy(Data[3],&i,2);
|
|
|
|
|
Wrong. The first argument to memcpy() needs to be an address, of which Data[3] is not.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I believe you should use a lower-case "D" in "Data". C is a case-sensitive language.
For example, use
memcpy(&i,&data[3],2);
instead of
memcpy(&i,&Data[3],2);
As others have noted, there could be other problems because you're reading from an array of unsigned chars into a short.
-- modified at 14:06 Wednesday 20th June, 2007
|
|
|
|
|
Erik Midtskogen wrote: I believe you should use a lower-case "D" in "Data".
Umm, the compiler would have told him this (i.e., syntax error).
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I was assuming that he may have been getting a compiler error and not known why. Probably we should ask him what sort compiler message or other symptom of a problem he is seeing.
|
|
|
|
|
Hi all
In servise can I create new process?
( can I use CreateProcess(.....) function? )
|
|
|
|
|
As long as you don't tell anyone I'm sure you can get away with it. I promise I don't work the Process police, honest.
Why don't you try it and let me know?
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Ok.
I have console an I whont to make service, sow I copy code from console to service. I test it and ewriting work ony problenm is wen I service try to create process.
<br />
char CmdLine[512];<br />
sprintf(CmdLine,"C:\\My program\\Program.exe"); <br />
<br />
if(CreateProcessA(NULL, CmdLine, NULL, NULL, FALSE, dwCreationFlags, NULL, NULL, &si, &pi) == 0)<br />
{<br />
MyLog("ERROR CANT START PROCESS"); <br />
}<br />
|
|
|
|
|
Ok I test and my service can't create process.
If you can help me.
how to Create Process from Service?
|
|
|
|
|
Hi,
Under which user account does your Service run. Services normally run under a system account which could be conflicting with the CreateProcess function.
Try to run the Service under a user account.
Or create new UserCredentials to start the process with.
codito ergo sum
|
|
|
|
|
|
Has si been initialized (properly)?
Why are you not calling GetLastError() to find out why CreateProcessA() failed?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: Why are you not calling GetLastError() to find out why CreateProcessA() failed?
He's busy eating his Fish Filet
|
|
|
|
|
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Which is exactly what I had for lunch today. I don't get the joke, however.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: I don't get the joke, however.
Still?[^]
|
|
|
|
|
It's clear now. Thanks.
Reminds me of this joke:
A man boarded a bus and saw the back bench seat occupied by a group of schoolboys. He made his way to the back and heard one of the boys yell out "5" and the whole group laughed loudly. Another boy yelled out "23" and louder laughter ensued. This went on for sometime and the man out of curiosity asked one of the boys what was up. He said that they had numbered the jokes they knew and just called out the joke number instead of telling the whole joke.
The man thought he would give it a try and yelled "3" - no one laughed. He tried again "7" - silence. "Whats wrong?" he asked.
"C'mon man - that's no way to tell a joke." the boy replied.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
After the amusement, it always just makes me hungry!
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
I've got Cornish Mackerel on for tea if you're in the area
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
That sounds VERY tasty!
Sadly, I am NOT in the area
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|