Results 1 to 3 of 3

Thread: Write resource to file

  1. #1

    Thread Starter
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032

    Write resource to file

    I think this should work, but I'm getting an Access Violation at the first WriteFile. Anyone know why?

    Thanks,

    Code:
    //file names
    #define CPL "\\wgpocpl.cpl"
    #define DLL "\\wgpoadmn.dll"
    
    	char * pchSystemDir = NULL; //the windows system directory
    	HRSRC hCpl = NULL; //handle to control panel resource
    	HRSRC hDll = NULL; //handle to DLL resource
    	HGLOBAL hCpl2 = NULL;
    	HGLOBAL hDll2 = NULL;
    	char * pchCpl = NULL; //location of resource in memory
    	char * pchDll = NULL; //location of resource in memory
    	char * pchCplName = NULL; //full file name of cpl
    	char * pchDllName = NULL; //full file name of dll
    	HANDLE hCplFile = INVALID_HANDLE_VALUE;
    	HANDLE hDllFile = INVALID_HANDLE_VALUE;
    
    		//load the resources into memory
    		hCpl = FindResource(NULL, IDR_CPL, "CUSTOM");
    		hDll = FindResource(NULL, IDR_DLL, "CUSTOM");
    		hCpl2 = LoadResource(NULL, hCpl);
    		hDll2 = LoadResource(NULL, hDll);
    		pchCpl = (char*)LockResource(hCpl2);
    		pchDll = (char*)LockResource(hDll2);
    
    		//create the files
    		hCplFile = CreateFile(pchCplName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, NULL);
    		hDllFile = CreateFile(pchDllName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, NULL);
    
    		HeapFree(GetProcessHeap(), 0, pchCplName);
    		HeapFree(GetProcessHeap(), 0, pchDllName);
    
    		//write to the files
    		WriteFile(hCplFile, pchCpl, SizeofResource(hInstance, hCpl), NULL, NULL);
    		WriteFile(hDllFile, pchDll, SizeofResource(hInstance, hDll), NULL, NULL);
    
    		//close the files
    		CloseHandle(hCplFile);
    		CloseHandle(hDllFile);
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Just a thought
    [quote]
    BOOL WriteFile(
    HANDLE hFile, // handle to file to write to
    LPCVOID lpBuffer, // pointer to data to write to file
    DWORD nNumberOfBytesToWrite, // number of bytes to write
    LPDWORD lpNumberOfBytesWritten, // pointer to number of bytes written
    LPOVERLAPPED lpOverlapped // pointer to structure for overlapped I/O
    );


    Windows NT: If lpOverlapped is NULL, lpNumberOfBytesWritten cannot be NULL.


    Are you using NT. If you are than your last parameters are both NULL.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3

    Thread Starter
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Thanks! I completely overlooked that - I declared an int and passed it a pointer to it instead of NULL and it works great.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width