The SDK is driving me insane! I did a proc to find and save a resource to file. The code looks like this:

[pascal]
//Finds and saves a resource to specified file.
function SaveResource(Filename,ResID,ResType: PChar):boolean;
var
SoundRes: cardinal;
SoundResHandle: cardinal;
NumBytesWritten: cardinal;
SoundResSize: cardinal;
FileHandle: cardinal;
Locked: pointer;
begin
SoundResHandle:=FindResource(0,ResID,ResType);
SoundRes:=LoadResource(0,SoundResHandle);
if SoundRes=0 then
begin
MessageBox(0,PChar('Error loading '+ResID+' resource'+#13+'Error code '+inttostr(GetLastError)),'Error!',0);
Result:=false;
exit;
end;
Locked:=LockResource(SoundRes);
SoundResSize:=SizeofResource(0,SoundResHandle);
FileHandle:=CreateFile(Filename, GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,0);

if WriteFile(FileHandle,SoundRes,SoundResSize,NumByte sWritten,0)=false then
begin
MessageBox(0,PChar('Failed to write data to '+Filename+#13+'Error code '+inttostr(GetLastError)),'Error!',0);
DeleteFile(Filename);
Result:=false;
exit;
end;
CloseHandle(Filehandle);
Result:=true;
end;
[/pascal]

The resource is found, the size of the resource is the correct one, SoundRes is really pointing to the memory block it's supposed to, yet, it won't write content to the output file. I figured the error must be in WriteFile(FileHandle,SoundRes,SoundResSize,NumByte sWritten,0) so I have tried every possible changes to it without luck. I have tried using Locked and SoundResHandle instead of SoundRes and I have tried using the OVERLAPPED structure. But I can't get it right. I get the custom buffer error when doing WriteFile();

I have read the SDK documentation so many times now, that I had nightmares about the evil structures comming to get me.. Any help greatly appriciated..