if I add a text file to the resource, how could I get this file in the programe?
-
Hi: I add a text file(or html file) to the resource of the project, how could I get this file in the programe?I want to get its content .Is there any API function to do so? Thanks Benben
And how you have added it? Has wrote a his path as string resource or how? Best regards, Eugene Pustovoyt
-
Hi: I add a text file(or html file) to the resource of the project, how could I get this file in the programe?I want to get its content .Is there any API function to do so? Thanks Benben
Try these Win32-API functions:
FindResource
LoadResource
SizeofResource
LockResource
That will do the trick! Yours, Jens -
Try these Win32-API functions:
FindResource
LoadResource
SizeofResource
LockResource
That will do the trick! Yours, Jens -
Sure. Let's assume you have added a textfile to your resource with ID=IDR_TEXTFILE, and your custom resource type has the name "TEXTFILE". You can extract the resource with the following code:
#include "stdafx.h"
#include "resource.h"#include <TCHAR.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HRSRC hRsrc = ::FindResource( hInstance,
MAKEINTRESOURCE( IDR_TEXTFILE ),
_T("TEXTFILE") ); // *** My custom res typeif ( NULL != hRsrc )
{
HGLOBAL hGlob = ::LoadResource( NULL, hRsrc );if ( NULL != hGlob ) { DWORD dwSize = ::SizeofResource( NULL, hRsrc ); LPVOID lpVoid = ::LockResource( hGlob ); if ( NULL != lpVoid ) { // \*\*\* Now you can work with the loaded resource. // \*\*\* In this case, we now it is a text file, // \*\*\* so cast it to a string LPCTSTR lpszString = reinterpret\_cast< LPCTSTR>( lpVoid ); MessageBox( NULL, lpszString, \_T("From resource"), MB\_OK ); } }
};
return 0;
}That's it ;-) Jens
-
Sure. Let's assume you have added a textfile to your resource with ID=IDR_TEXTFILE, and your custom resource type has the name "TEXTFILE". You can extract the resource with the following code:
#include "stdafx.h"
#include "resource.h"#include <TCHAR.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HRSRC hRsrc = ::FindResource( hInstance,
MAKEINTRESOURCE( IDR_TEXTFILE ),
_T("TEXTFILE") ); // *** My custom res typeif ( NULL != hRsrc )
{
HGLOBAL hGlob = ::LoadResource( NULL, hRsrc );if ( NULL != hGlob ) { DWORD dwSize = ::SizeofResource( NULL, hRsrc ); LPVOID lpVoid = ::LockResource( hGlob ); if ( NULL != lpVoid ) { // \*\*\* Now you can work with the loaded resource. // \*\*\* In this case, we now it is a text file, // \*\*\* so cast it to a string LPCTSTR lpszString = reinterpret\_cast< LPCTSTR>( lpVoid ); MessageBox( NULL, lpszString, \_T("From resource"), MB\_OK ); } }
};
return 0;
}That's it ;-) Jens
-
And how you have added it? Has wrote a his path as string resource or how? Best regards, Eugene Pustovoyt
-
Hmmm, one annotation: The more I think about it the more I get convinced, that it is not allowed to just cast the memory pointer to an LPCTSTR, because the trailing zero is missing. Instead it should be copied to an according buffer. So it should be:
LPCTSTR lpszString = reinterpret\_cast< LPCTSTR>( lpVoid ); TCHAR \*tcBuffer = new TCHAR\[ dwSize + 1 \]; if ( NULL != tcBuffer ) { ZeroMemory( tcBuffer, ( dwSize + 1 ) \* sizeof( TCHAR ) ); memcpy( tcBuffer, lpszString, dwSize \* sizeof( TCHAR ) ); }
But this is only the case with strings. Byte buffer or similar can't be used directly anyway (like LPCTSTR). Jens