we'll get straight to business, the following snippet returns
Unhandled exception at 0x7786AA3C (ntdll.dll) in Project4.exe: 0xC0000374: A heap has been corrupted (parameters: 0x7787FE38).
Snippet:
while(true)
{
if(WSALookupServiceNext(lookup_handle,flags,&query_set_length,query_set)==NO_ERROR)
{
char buffer[40] = {0};
DWORD buffer_size = sizeof(buffer);
device_socket_address = (SOCKADDR_BTH*)query_set->lpcsaBuffer->RemoteAddr.lpSockaddr;
result = device_socket_address->btAddr;
WSAAddressToString(query_set->lpcsaBuffer->RemoteAddr.lpSockaddr,sizeof(SOCKADDR_BTH),NULL,buffer,&buffer_size);
fprintf(stdout,"found device: %s - %s\n", buffer,query_set->lpszServiceInstanceName);
break;
}
}
The break occurs on WSAAddressToString
before WSALookupServiceNext I call
//start the LookUp service for bluetooth devices
if(WSALookupServiceBegin(query_set,flags,&lookup_handle)==SOCKET_ERROR)
{
fprintf(stderr, "something went completely wrong... %d",WSAGetLastError());
system("PAUSE");
ExitProcess(2);
}
Query set initialization
DWORD query_set_length = sizeof(WSAQUERYSET);
DWORD flags = LUP_CONTAINERS | LUP_FLUSHCACHE | LUP_RETURN_NAME | LUP_RETURN_ADDR;
WSAQUERYSET query_set = (WSAQUERYSET*)malloc(query_set_length);
ZeroMemory(query_set,query_set_length);
query_set->dwSize = query_set_length;
query_set->dwNameSpace = NS_BTH;
query_set->dwNumberOfCsAddrs = 0;
Update:
The source of the problem seems to be WSALookupServiceNext
, I suspect my handle(lookup_handle) of being passed in a wrong manner.
My question being, what caused the heap corruption, and how can i repair it?
I just want to find a device, and be able to send it a test string.
I have removed LUP_FLUSCACHE
and LUP_CONTAINERS
from my flags,
DWORD flags = LUP_RETURN_NAME | LUP_RETURN_ADDR;
and only use it in WSALookupServiceBegin
if(WSALookupServiceBegin(query_set,flags |= LUP_FLUSHCACHE | LUP_CONTAINERS,&lookup_handle)==SOCKET_ERROR)
{
fprintf(stderr, "something went completely wrong... %d",WSAGetLastError());
system("PAUSE");
ExitProcess(2);
}
it seems to prevent the problem. I just read this MSDN entry
hehe, I also used fprintf to stdin instead of stdout (may I burn in eternal fire).