WriteFile Error 1784

This is a discussion on WriteFile Error 1784 within the Windows Programming forums, part of the Platform Specific Boards category; I have compiled a midi file as a resource, which I want to call in my program. The program compiles ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    Kirby-le-Soken, Essex, UK
    Posts
    6

    WriteFile Error 1784

    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);
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You should remove the GENERIC_READ flag from your CreateFile() call if you are using CREATE_ALWAYS.

    I'm also not sure WriteFile() and LockResource will work with void pointers as it cannot know the element size... perhaps you should try hTune as an unsigned char* instead.

    Also when testing Function returns with Windows calls you should do so on a separate line...
    Code:
    PUCHAR hTune = NULL;
    
       hTune = LockResource (hRes);
       if (hTune == NULL)    
          MessageBox(hwnd,"Lock Failed","Test",MB_OK);
    Set your LValue intially to NULL, or some other failure value, call the function, then test to see if it has changed. Many windows functions return non-0 error values that would not be caught by the "not" test. Also many functions actually return 0 as a success value. The constants NO_ERROR and ERROR_SUCCESS are actually defined as 0 in Windows headers.

    Finally... a small suggestion... If you use the MCI commands and structs instead of the MCISendString() function you can get back an error message if things go wrong. It might save you some time if you simply tried to play the file first, if MCI finds it and plays it you're done. If MCI errors off, you need to unpack the file... But for each installation you should only have to unpack it once.

    Hope this helps...

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Kirby-le-Soken, Essex, UK
    Posts
    6
    Thanks for that. The temp.mid file is created but is empty. I'll try out your suggestion and get back to you

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Kirby-le-Soken, Essex, UK
    Posts
    6
    Hello Tater,

    Tried out your suggestion, but got a compile warning.

    Still the same writefile error

    ||=== midires, default ===|
    C:\Dev-Cpp\Projects\midiresource\main.c||In function `midiplay':|
    C:\Dev-Cpp\Projects\midiresource\main.c|122|warning: passing arg 2 of `SizeofResource' from incompatible pointer type|
    ||=== Build finished: 0 errors, 1 warnings ===|

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    >> You should remove the GENERIC_READ flag from your CreateFile() call if you are using CREATE_ALWAYS.
    One has nothing to do with the other. Leave GENERIC_READ in - it means more than "I'll be reading the file".
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    You need to save the HRSRC that FindResource() returns and pass that to SizeofResource().
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    gg

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by John Layton View Post
    Hello Tater,

    Tried out your suggestion, but got a compile warning.

    Still the same writefile error

    ||=== midires, default ===|
    C:\Dev-Cpp\Projects\midiresource\main.c||In function `midiplay':|
    C:\Dev-Cpp\Projects\midiresource\main.c|122|warning: passing arg 2 of `SizeofResource' from incompatible pointer type|
    ||=== Build finished: 0 errors, 1 warnings ===|
    So the error is actually in SizeOfResource()...
    The first parameter is your Instance handle for the module with the resource in it.
    The second parameter is the resource handle... not a pointer to the buffer it's in.

    Try...
    Code:
    INT ResSize = 0;
    
       ResSize = SizeofResource(hInstance,hRes)))
       if (ResSize == 0)
         MessageBox(hwnd,"Get Size Failed","Test",MB_OK);
    Also... can you repost your code... an updated version might help.

    I also see you are using DEV-CPP...
    This is a seriously outdated and sometimes broken IDE that is no longer maintained.
    Also, the windows headers supplied actually pre-date Win-2000.

    Are you working C++ or C on this project?

    If C++ I would suggest a switch to Microsoft's Visual Studio.
    If standard C You should look into PellesC ... http://smorgasbordet.com/pellesc/
    Last edited by CommonTater; 11-18-2010 at 08:17 AM. Reason: About DEV-CPP...

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    >> Try...
    >> ResSize = SizeofResource(hInstance, hRes)
    In the OP's code, "hRes" is not a resource handle. The HRSRC from FindResource() is needed.

    gg

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Kirby-le-Soken, Essex, UK
    Posts
    6
    Here is the corrected code, which compiles. But I still get the 1784
    Code:
    void midiplay(HWND hwnd)
    {
    
        HANDLE hFile;
        DWORD written;
        //DWORD ResSize;
        HGLOBAL hRes ;
        LPVOID hTune;
        int ResSize=0;
    
    
    
        hRes = LoadResource (hInstance,FindResource (hInstance, TEXT ("midkel"),TEXT("MyRes")));
        if (hRes==NULL)
        {
            MessageBox(hwnd,"Load Resource Failed","Test",MB_OK);
        }
    
        hTune=LockResource (hRes);
        if (hTune==NULL)
        {
            MessageBox(hwnd,"Lock Failed","Test",MB_OK);
        }
    
        ResSize = SizeofResource(hInstance, hTune);
        if (ResSize==0)
        {
            MessageBox(hwnd,"Get Size Failed","Test",MB_OK);
        }
    
        hFile=CreateFile("temp.mid",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
        if (hFile==NULL)
        {
            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);
    }

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Codeplug View Post
    >> Try...
    >> ResSize = SizeofResource(hInstance, hRes)
    In the OP's code, "hRes" is not a resource handle. The HRSRC from FindResource() is needed.

    gg
    Ahhh... right you are.

    So he needs to split that first call into two, unnesting the find from the load...

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by John Layton View Post
    Here is the corrected code, which compiles. But I still get the 1784
    Hmmmm... codeplug seems to have a better handle on this one than I do...
    I'll let you carry on with his help.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Kirby-le-Soken, Essex, UK
    Posts
    6
    Thanks everyone. Problem solved. Below the correct code
    Code:
    void midiplay(HWND hwnd)
    {
    
        HANDLE hFile;
        DWORD written;
        //DWORD ResSize;
        HRSRC hResFind;
        HGLOBAL hRes ;
        LPVOID hTune;
        int ResSize=0;
    
        hResFind=FindResource (hInstance, TEXT ("midkel"),TEXT("MyRes"));
        if (hResFind==NULL)
        {
            MessageBox(hwnd,"Can't Find Resource","Test",MB_OK);
        }
    
    
    
        hRes = LoadResource (hInstance,hResFind);
        if (hRes==NULL)
        {
            MessageBox(hwnd,"Load Resource Failed","Test",MB_OK);
        }
    
        hTune=LockResource (hRes);
        if (hTune==NULL)
        {
            MessageBox(hwnd,"Lock Failed","Test",MB_OK);
        }
    
        ResSize = SizeofResource(hInstance, hResFind);
        if (ResSize==0)
        {
            MessageBox(hwnd,"Get Size Failed","Test",MB_OK);
        }
    
        hFile=CreateFile("temp.mid",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
        if (hFile==NULL)
        {
            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);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic file creation and appending.. ofstream vs. WriteFile
    By rodrigorules in forum Windows Programming
    Replies: 2
    Last Post: 07-07-2010, 07:51 PM
  2. WriteFile and fwrite
    By George2 in forum C Programming
    Replies: 4
    Last Post: 08-10-2007, 05:33 AM
  3. storing integer values in files using WriteFile
    By Fender Bender in forum Windows Programming
    Replies: 1
    Last Post: 01-15-2006, 12:15 AM
  4. WriteFile question???
    By NewGuy100 in forum C Programming
    Replies: 27
    Last Post: 08-16-2005, 08:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21