I have compiled a midi file as a resource, which I want to call in my program. The program compiles without errors or warnings, but I am unable to write the midi to a file and play it. I am getting a 1784 error with the WriteFile() function which translates to ERROR_INVALID_USER_BUFFER.
Below my resource entry and my function in it's entirety. Anyone got any ideas where I'm going wrong ?
Code:
midkel MyRes "blacksmith.mid"
void midiplay(HWND hwnd)
{
HANDLE hFile;
DWORD written;
DWORD ResSize;
HGLOBAL hRes ;
void *hTune;
if (!(hRes = LoadResource (hInstance,FindResource (hInstance, TEXT ("midkel"),TEXT("MyRes")))))
{
MessageBox(hwnd,"Load Resource Failed","Test",MB_OK);
}
if (!(hTune=LockResource (hRes)))
{
MessageBox(hwnd,"Lock Failed","Test",MB_OK);
}
if (!(ResSize = SizeofResource(hInstance, hTune)))
{
MessageBox(hwnd,"Get Size Failed","Test",MB_OK);
}
if (!( hFile=CreateFile("temp.mid",GENERIC_READ|GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0)))
{
MessageBox(hwnd,"Create File Failed","Test",MB_OK);
}
if (!(WriteFile(hFile,hTune,ResSize,&written,0)))
{
int err=GetLastError();
char buff[1111];
strcpy(buff,"");
sprintf(buff,"%d",err);
MessageBox(hwnd,buff,buff,MB_OK);
}
CloseHandle(hFile);
FreeResource(hRes);
// mciSendString("open \"temp.mid\" type sequencer alias mymidi", 0, 0, 0);
// mciSendString("play mymidi", 0, 0, 0);
}