Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Extract resources from a DLL
Message
De
01/02/2013 10:42:23
 
 
À
01/02/2013 10:25:47
Emerson Reed
Folhamatic Tecnologia Em Sistemas
Americana - São Paulo, Brésil
Information générale
Forum:
Visual FoxPro
Catégorie:
Fonctions Windows API
Versions des environnements
Visual FoxPro:
VFP 9
OS:
Windows 7
Application:
Desktop
Divers
Thread ID:
01564921
Message ID:
01564922
Vues:
70
I'm not aware of a function which lets you do this directly. In the past, I've had to lock the resources to get a pointer to their starting location in memory, and then using their length, write the data at that pointer to my target disk file.

I've done this in C/C++ WIN32 API code. There's a sample of how to do it here:
http://www.codeproject.com/Articles/3087/Reading-and-Using-Resources-in-an-Executable-file

To lock: http://msdn.microsoft.com/en-us/library/windows/desktop/ms648047%28v=vs.85%29.aspx
To get size: http://msdn.microsoft.com/en-us/library/windows/desktop/ms648048%28v=vs.85%29.aspx

Here is some sample code in C++. It's extracted from a library utility that works. It might be missing some global variables, but they should be discernable from context if you're going to go the C/C++ route. :-)
int iSaveFileToDisk(int tnResource, char* tcPath, char* tcFilename)
{
	PROCESS_INFORMATION	lpi;
	STARTUPINFO			si;
	SIZE_T				lnLength, lnNumread;
	bool				llResult;
	TCHAR*				rptr;
	HRSRC				hrsrc;
	DWORD				lnSize, errorNumber;
	HGLOBAL				glob;
	void*				dptr;
	FILE*				lfh;
	char				buffer[MAX_PATH + 1];


	// Make sure the resource exists
	rptr	= MAKEINTRESOURCE(tnResource);
	hrsrc	= FindResource(ghInstance, rptr, _T("FILES"));
	if (hrsrc)
	{
		// Find out its size
		lnSize = SizeofResource(ghInstance, hrsrc);

		// Load it into a global
		glob = LoadResource(ghInstance, hrsrc);
		if (glob)
		{
			// Lock it so we can read it (gets a pointer to it)
			dptr = LockResource(glob);
			if (dptr)
			{
				// Initialize the filename
				memset(gcCurrentFileBuffer, 0, sizeof(gcCurrentFileBuffer));

				// Append the path
				lnLength = strlen(tcPath);
				memcpy(gcCurrentFileBuffer, tcPath, lnLength);

				// Append the trailing \ to the path/directory if needed
				if (gcCurrentFileBuffer[lnLength - 1] != '\\')
				{
					gcCurrentFileBuffer[lnLength] = '\\';
					++lnLength;
				}

				// Copy the filename portion
				memcpy(gcCurrentFileBuffer + lnLength, tcFilename, strlen(tcFilename));

				// Write the file to disk
				// Create the file
				lfh = fopen(gcCurrentFileBuffer, "wb+");
				if (lfh)
				{
					// We're good so far, write to it
					// Write with feedback during the write
					lnNumread = iWriteBufferToDiskWithFeedback((char*)dptr, lnSize, lfh);
					fclose(lfh);
					if (lnNumread == lnSize)
					{
						// We're good!
						llResult = true;

					} else {
						// Not so much
						llResult = false;
					}

				} else {
					// No good
					llResult = false;
					// Fall through
				}

				// Regardless if successful or not, we need to unlock the resource
				UnlockResource(glob);
			}
		}
	}
}
>Hi Folks.
>I have a DLL with embedded resources (PNG and INI files) that I need to enumerate and save as standalone files.
>I'm already enumerating the resources, but I don't know how to save them.
>Someone could help me to accomplish this?
>Here is the code...
>
>Local lcFolder
>m.lcFolder = Justpath(Sys(16))
>Set Default To (m.lcFolder)
>
>* Init Vfp2c32
>Set Library To vfp2c32
>
>#INCLUDE vfp2c.h
>If !InitVFP2C32(VFP2C_INIT_ALL)
>   Local laError[1], lnCount, xj, lcError
>   lnCount = AERROREX('laError')
>   lcError = 'VFP2C32 Library initialization failed:' + Chr(13)
>   For xj = 1 To lnCount
>      lcError = lcError + ;
>         'Error No : ' + Transform(laError[1]) + Chr(13) + ;
>         'Function : ' + laError[2] + Chr(13) + ;
>         'Message : "' + laError[3] + '"'
>   Endfor
>   && show/log error and abort program initialization ..
>   Messagebox(m.lcError,16,"Error")
>   Return
>Endif
>
>#Define LOAD_LIBRARY_AS_IMAGE_RESOURCE 0x00000020
>#Define LOAD_LIBRARY_AS_DATAFILE       0x00000002
>
>* http://msdn.microsoft.com/en-us/library/windows/desktop/ms684179%28v=vs.85%29.aspx
>Declare Integer LoadLibraryEx In Win32api ;
>   String lpFileName, ;
>   Integer hFile, ;
>   Integer dwFlags
>
>* http://msdn.microsoft.com/en-us/library/windows/desktop/ms683152%28v=vs.85%29.aspx
>Declare Integer FreeLibrary In Win32api ;
>   Long hModule
>
>Local lcResourceFile, lnhModule
>m.lcResourceFile = Addbs(m.lcFolder) + "Office2007.dll"
>
>m.lnhModule = LoadLibraryEx(m.lcResourceFile, 0, LOAD_LIBRARY_AS_IMAGE_RESOURCE + LOAD_LIBRARY_AS_DATAFILE)
>If m.lnhModule > 0
>
>   *
>   Local lcResources, laResourceTypes[1], lnResourceType, laResources[1], lnResource
>   m.lcResources = ""
>   If AResourceTypes("laResourceTypes", m.lnhModule) > 0
>      For m.lnResourceType=1 To Alen(m.laResourceTypes)
>         m.lcResources = m.lcResources + Chr(13) + Chr(10) + ;
>            "Resource type: " + ;
>            Alltrim(Transform(m.laResourceTypes[m.lnResourceType])) + ;
>            Chr(13) + Chr(10)
>         If AResourceNames("laResources", m.lnhModule, m.laResourceTypes[m.lnResourceType]) > 0
>            For m.lnResource=1 To Alen(m.laResources)
>               m.lcResources = m.lcResources + ;
>                  "Resource name: " + ;
>                  Alltrim(Transform(m.laResources[lnResource])) + ;
>                  Chr(13) + Chr(10)
>               * Save each resource here...
>            Endfor
>         Endif
>      Endfor
>      _Cliptext = m.lcResources
>   Endif
>   *
>
>   If FreeLibrary(m.lnhModule) <> 0
>   Else
>      ? "Error releasing library..."
>   Endif
>Else
>   ? "Error loading library..."
>Endif
>
>Release Library vfp2c32
>
>Clear Dlls LoadLibraryEx
>Clear Dlls FreeLibrary
>
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform