• Advertisement
Sign in to follow this  

stack corruption accessing external hardware through usb.

This topic is 2635 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys,

So I have a DLL of 4 functions from a guy who is on holiday. The 4 functions just open a line to a hardware device, read from it, write to it and close it (open, read, write, close).

I have to build a packet and send it to the device to make it do things and I'm running into stack corruption errors. The packet is divided into 1 byte header, 1 byte command and 4 bytes data arranged as follows:


bits |47:44 |43|42:40| 39:38 | 37:32 |31:0|
|checksum|CE|Error|SET/GET|Command|Data|


[I added code tags here to preserve your formatting, and I assume you meant "bits" rather than "bytes". - Zahlman]

The read and write functions are declared as:

unsigned char write(PBYTE p_header, PBYTE p_command, float* p_data,ULONG* size, PBYTE p);
unsigned char read(PBYTE p_header, PBYTE p_command, float* p_data, unsigned char* p);

So I've defined the following structures:


typedef byte unsigned char;

union header_t {
struct {
byte error : 3;
byte ce : 1;
byte checksum : 4;
};
byte u8;
};

union command_t {
struct {
byte cmd : 6;
byte setget : 2;
};
byte u8;
};




So when I set up the header and command ala:


header_t h; // memset to 0
command_t c; // memset to 0
c.cmd = 0xf;
c.setget = 1;
h.checksum = calculate_checksum(h, c, data);
unsigned long num_bytes;
byte p;
write(&h.u8, &c.u8, &data, &num_bytes, &p);




Then as soon as I'm done with the write function I get a stack overflow saying: "Stack around variable num_bytes was corrupted"

Am I doing something wrong with my bits or is there something going wrong in the write function that I have no control over? By the way, num_bytes gets given a value, you don't set it. I know, weird.

Thanks for any help.

[Edited by - Zahlman on December 8, 2010 1:51:51 AM]

Share this post


Link to post
Share on other sites
Advertisement
What calling convention do the functions in the dll use? Are you calling them using the correct calling convention?

Also, have you tried initializing all your variables to some value, even the ones you are not required to initialize? It's a good habit to get into (I typically initialize status variables to "critical failure" and everything else to 0).

Share this post


Link to post
Share on other sites
Ok so apparently, the variable after num_bytes was the problem. There was no documentation but it has to be six bytes long.

Thanks for the help.

Share this post


Link to post
Share on other sites
Quote:
Original post by IFooBar
There was no documentation but it has to be six bytes long.
Murder your colleague.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement