Sign in to follow this  

stack corruption accessing external hardware through usb.

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|

[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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this