Using resources

This is a discussion on Using resources within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I had no idea if I should post it in Windows Programming or General C-programming, but since win32 is ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    25

    Using resources

    Hi, I had no idea if I should post it in Windows Programming or General C-programming, but since win32 is the platform I'm working on I'm posting here...
    Mod may move if it's in the wrong section.

    So here is my question - I have a program that uses cURL to speak HTTP easily with my webserver.
    But cURL needs a couple of DLL-files that should exist in the system32-folder. Now I don't want to ship these files with my executable - but instead have them as resources in my executable and automatically extract them and install in system32.

    I am using Dev-Cpp and programming in C.

    Do you have any idea on how to implement this in my program?

    Thank you for your consideration

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Why you would want to do such thing? Your extractiong will not check the version of dll and could replace the newer dll with the older one - breaking other applications

    Installer on the other hand - always checks the file version before replacing the dll and notifies the end user - giving him the choice to replace dll or leave it intact.

    You can always put the required dlls into the local dir of your application - in this case you can bring them as you like, but still I do not see a reason not to use a self extracting archive and start play with the resources of the executable
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    I didn't mention error handling etc - but it will offcorse be included.

    I have made my choice of handling this problem, and I want to extract them from resources in my executable. With hours of googling, I have yet not found any information.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    Btw. This is how I solved it:

    resources.rc -
    Code:
    1 BIN DISCARDABLE "path-to-.dll"
    extract.c -
    Code:
    void Extract(WORD wResId , char szFilePath[])
    {
    	HRSRC hrsrc;
    	HGLOBAL hLoaded;
    	LPVOID lpLock;
    	DWORD dwSize;
    	HANDLE hFile;
    	DWORD dwByteWritten;
    	
    	hrsrc = FindResource(NULL, MAKEINTRESOURCE(wResId) ,"BIN");
    	if(hrsrc == NULL)
    	{
    		//printf("Error Finding resource\n");
    	}
    
    	hLoaded = LoadResource(NULL, hrsrc);
    	if(hLoaded == NULL)
    	{
    		//printf("Error Loading Resource\n");
    	}
    	
    	lpLock = LockResource(hLoaded);
    	if(lpLock == NULL)
    	{
    		//printf("Error Locking Resource\n");
    	}
    	
    	dwSize = SizeofResource(NULL, hrsrc);
    	
    	hFile = CreateFile(szFilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
    	
    	if(!WriteFile(hFile, lpLock , dwSize , &dwByteWritten , NULL))
    	{
    		//printf("Error Writing to file\n");
    	}
    	CloseHandle(hFile);
    	FreeResource(hLoaded);
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think you do NOT want CREATE_ALWAYS - that will cause the problems that vart points out with a newer version already existing on the target system and breaking other applications.

    By the way, DLL's do not have to reside in system32 - they can be provided in the local directory where the application is located, e.g \program files\myapp.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Resources in Dev C++
    By JJFMJR in forum Windows Programming
    Replies: 7
    Last Post: 08-27-2007, 05:14 AM
  2. measuring system resources used by a function
    By Aran in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 04:35 PM
  3. Getting resources from another program.
    By Queatrix in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2006, 08:00 PM
  4. Storing resources into a single file
    By LuckY in forum Game Programming
    Replies: 20
    Last Post: 08-14-2004, 11:28 PM
  5. Adding resources
    By nima_ranjbar in forum Windows Programming
    Replies: 0
    Last Post: 04-14-2002, 11:36 PM

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