Sign in to follow this  

How do handles work?

This topic is 4683 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

Alright, well here is my problem. I'm using handles in my sound class to find out points where sound buffers trigger the loaders. Now my main problem is understanding handles, or at least finding out whats going wrong with them. I have an array[x] of handles, which represents how many mixing buffers a sound card supports. Each event handle is then created with CreateEvent(NULL,FALSE,FALSE,NULL), if null is returned I clean up the handles. If it succeeds I assume the handles are ready to be triggered. So in my process I make a set of parameters to send to the thread, then the thread recieves them and sets them to they own varaibles (which I have checked out to be fine). Now this is where I'm having a problem.
returnchan = MsgWaitForMultipleObjects(((*maxchannels) + 1), events, FALSE, INFINITE, QS_ALLEVENTS);
			channel = returnchan - WAIT_OBJECT_0;
No matter what channel of the array pointer I trigger MsgWaitForMultipleObjects will always return 0xffffffff. What does this mean? It has the correct values to check, the correct amount of elements as well.

Share this post


Link to post
Share on other sites
I have no idea what any of your variables are supposed to be in there, but are you sure you want to increment 'maxchannels' after dereferencing it? You say "no matter what channel of the array pointer"; that's the only pointer I see, and if you intend to access an array element given a pointer to the array, then you want it the other way around.

Share this post


Link to post
Share on other sites
#define WAIT_FAILED 0xffffffff

It means the wait failed. "To get extended error information, call GetLastError"

Here's a snippet demonstrating how to convert error codes into more understandable text messages.



LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );



Share this post


Link to post
Share on other sites
Alright I get the error message "The parameter is incorrect", but I don't see anything wrong with it.

*Note: Keep in mind all of the pointers recieved check out fine, so there shouldn't be anything wrong with them.

DWORD WINAPI cSound::NotificationThread(void* lpParamater)
{
LPVOID ThreadParam[5];
memcpy(&ThreadParam, lpParamater, sizeof(LPVOID)*5);

bool* threadactive = (bool*)ThreadParam[0];
DWORD* maxchannels = (DWORD*)ThreadParam[1];
HANDLE* events = (HANDLE*)ThreadParam[2];
short* soundbufferid = (short*)ThreadParam[3];
cSoundLoader** soundevents = (cSoundLoader**)ThreadParam[4];

bool active = true;
DWORD returnchan;
DWORD channel;
MSG msg;
char messagestr[32];
LPVOID lpMsgBuf;

(*threadactive) = true;

while (active == true){

returnchan = MsgWaitForMultipleObjects(((*maxchannels) + 2), events, FALSE, INFINITE, QS_ALLEVENTS);
channel = returnchan - WAIT_OBJECT_0;

if(channel == 0xffffffff){

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);

MB((LPCSTR)lpMsgBuf);
}

if((channel >= 0) && (channel <= (*maxchannels))){
sprintf(messagestr, "%i", channel);
MB(messagestr);

}
else{
if(channel == ((*maxchannels) + 1)){
active = false;
}
if(channel > ((*maxchannels) + 1)){
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
if(msg.message == WM_QUIT){
active = false;
break;
}
}
}
}
}

(*threadactive) = false;

return 0;
};

Share this post


Link to post
Share on other sites
First off, don't forget the LocalFree after the MessageBox.

The incorrect parameter is either nCount or pHandles. Try hard coding the nCount for now. If the call succeeds then you know there's a problem with the transfers (all that ThreadParam stuff). If it continues to fail then you know the problem is with the pHandles parameter.

I suspect the function presumes that the handles in the array are contiguous and might be reading the events variable as the start of the block ThreadParam[2,3,4].

Try passing a pointer pointer and then dereferencing that - similar to the treatment given cSoundLoader.

HANDLE** events = (HANDLE**)ThreadParam[2];

returnchan = MsgWaitForMultipleObjects(...,*events,...);


Regarding ThreadParam, would it just be easier to cast lpParamater as a structure? something like this

struct foo {
bool threadactive;
DWORD maxchannels;
HANDLE* events;
short soundbufferid;
cSoundLoader* soundevents;
}

Share this post


Link to post
Share on other sites
I usually delete the code after it's served it's purpose, so I often forget about deleting things I added for cleaning up debugging code. It probably would be easier to make a structure for the parameter passed, but I try and stay away from making structures that I'm only going to use once in the program.

The main problem was that I didn't know MsgWaitForMultipleObjects can only take a maximum of 64 - 1 handles. So that begs the question; do I set a max amount of channels or do I create another thread to handle another x amount of handles so I can get the full use out of the audio card?

Share this post


Link to post
Share on other sites

This topic is 4683 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.

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