问题描述:

I have a WiFi Listener registered as a callback (pointer function) with a fixed 3rd party interface. I used a static member of my function to register the callback function and then that static function calls a nonstatic member through a static cast. The main problem is that I cannot touch the resulting char * buff with any members of my class nor can I even change an int flag that is also a member of my class. All result in runtime access violations. What can I do? Please see some of my code below. Other problems are described after the code.

void *pt2Object;

TextWiFiCommunication::TextWiFiCommunication()

{

networkDeviceListen.rawCallback = ReceiveMessage_thunkB;

/* some other initializing */

}

int TextWiFiCommunication::ReceiveMessage_thunkB(int eventType, NETWORK_DEVICE *networkDevice)

{

if (eventType == TCP_CLIENT_DATA_READY)

static_cast<TextWiFiCommunication *>(pt2Object)->ReceiveMessageB(eventType,networkDevice);

return 1;

}

int TextWiFiCommunication::ReceiveMessageB(int eventType, NETWORK_DEVICE *networkDevice)

{

unsigned char outputBuffer[8];

// function from an API that reads the WiFi socket for incoming data

TCP_readData(networkDevice, (char *)outputBuffer, 0, 8);

std::string tempString((char *)outputBuffer);

tempString.erase(tempString.size()-8,8); //funny thing happens the outputBuffer is double in size and have no idea why

if (tempString.compare("facereco") == 0)

cmdflag = 1;

return 1;

}

So I can't change the variable cmdflag without an access violation during runtime. I can't declare outputBuffer as a class member because nothing gets written to it so I have to do it within the function. I can't copy the outputBuffer to a string type member of my class. The debugger shows me strlen.asm code. No idea why. How can I get around this? I seem to be imprisoned in this function ReceiveMessageB.

Thanks in advance!

Some other bizzare issues include: Even though I call a buffer size of 8. When I take outputBuffer and initialize a string with it, the string has a size of 16.

网友答案:

You are likely getting an access violation because p2tObject does not point to a valid object but to garbage. When is p2tObject initialized? To what does it point?

For this to work, your code should look something like this:

...
TextWifiCommunication twc;
p2tObject = reinterpret_cast<void*>(&twc);
...

Regarding the string error, TCP_readData is not likely to null-terminate the character array you give it. A C-string ends at the first '\0' (null) character. When you convert the C-string to a std::string, the std::string copies bytes from the C-string pointer until it finds the null terminator. In your case, it happens to find it after 16 characters.

To read up to 8 character from a TCP byte stream, the buffer should be 9 characters long and all the bytes of the buffer should be initialized to '\0':

...
unsigned char outputBuffer[9] = { 0 };
// function from an API that reads the WiFi socket for incoming data
TCP_readData(networkDevice, (char *)outputBuffer, 0, 8);
std::string tempString((char *)outputBuffer);
...
相关阅读:
Top