|
Hi,
first of all, to be on the safe side, you should test the Handle returned by ResInfo
by inserting a line like
if(! ResInfo)
{
MessageBox("Could not find thingy in the resource, exiting...","Dear me",MB_EXCLAMATION);
return;
}
before calling LoadResource()
Then, you should also try using the MAKEINTRESOURCE() Macro to pass the Resource Id
i.e. your call to FindResource should now be:
HRSRC = FindResource(GetModuleHandle(NULL),MAKEINTRESOURCE(IDR_SCREENSAVER1),RT_RCDATA);
Let me know if this helps.
Cheers
Pierre Heler-Caruel
|
|
|
|
|
Well i tried both your suggestions. First i added the code to check if ResInfo was valid and then i tried using MAKEINTRESOURCE() but that didn't work. So i tossed that and i tried renaming my resource stringwise with the "" , that didn't work either. I tried all combinations. It can't seem to find the resource(allthough its there). I also tried going to the resource, clicking right>properties and unchecking the "external file" checkbox but that didn't help either.
Any suggestions?
Kuniva
|
|
|
|
|
Hi,
sorry to hear it didn't work.
Normally, you should name your Resource Ids as usual (i.e. no quotes) and use MAKEINTRESOURCE(ResourceId) as this is the usual way.
Having said that, there are two things you can try:
1) Ensure the Type of Resource for the file matches the type you use in FindResource(). My recommendation would be to define your own resource-type (for instance "SCREEN_SAVER") when you import the file in your project. Then use that name (note the quotes) as the Resource-type
2) Visual Studio has a funny way of not rebuilding correctly if you have changed the resource (this doesn't happen all the time though... I guess it's a "feature";)
So instead of just doing a compile (F7) do a clean then rebuild all of your project and try again.
BTW if you send me your .rc file and the associated file to import, I can give it a shot for you.
Let me know.
Cheers,
Pierre Heler-Caruel
|
|
|
|
|
First of all thanks a lot for your help, I kind of got the resource to load and stuff anyways i got a handle to it with the method Derek described (thanks). BUT so i have a handle, one of my following questions is: "what is LockResource() for exactly?"
my second and perhaps most important question is: "How do i get the data from the file and write it to a file on disk so i have an exact replica of it?? What kind of variable do i use? How do i even get the data with the handle? I mean.. I have a handle, so what? I dont know how to use a handle... *blush* lolz"
Kuniva
Want, take, have.
(oh btw, best way of learning some language is by looking at examples, start large!) :p
|
|
|
|
|
LockResource ... locks the resource, I think the names says it all ...
Seriously, once you've locked the resource, you can manipulate it.
With the handle, you could do things like this to write it to a file (let's assume hYourApp is a Handle to your Application):
first retrieve its size
DWORD dwDataSize = SizeofResource((HMODULE)hYourApplication,ResInfo)
if(dwDataSize == 0)
{
// Failed to retrieve the Size
MessageBox("Ooops","Dear me...",MB_ICONEXCLAMATION);
return;
}
Then, you can create a file and dump the content of the Resource into it
FILE * pfResourceData = NULL,
BYTE * pData = NULL;
pfResourceData = fopen("SomeFileName","wb+");
if(pfResourceData)
{
if(! fwrite(pData,dwDataSize,1,pfResourceData))
{
fclose(Result);
DeleteFile("SomeFileName");
MessageBox("Failed to write Data",
"Oh Dear!",
MB_ICONEXCLAMATION);
return;
}
else
{
// Wrote all the Data so close the File
fclose(pfResourceData);
}
}
There you have it. The Resource Data should now be stored in "SomeFileName"
HTH
Cheers,
Pierre
P.S. I've used the "C" way of writing files to keep as generic as possible but of course you're free to use the
MFC way with CFile or File-handles, etc...
|
|
|
|
|
yeehaa!!! i got it to work, thanks to you!
Thanks sooooooooooo much!!!
Kun
|
|
|
|
|
Could it be that you have named your resource with a numeric identifier? Go to the resource editor, change the identifier from IDR_SCREENSAVER1 (numeric ID) to "IDR_SCREEN_SAVER1" (string ID), and try your program again.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Well i tried both your suggestions. First i added the code to check if ResInfo was valid and then i tried using MAKEINTRESOURCE() but that didn't work. So i tossed that and i tried renaming my resource stringwise with the "" , that didn't work either. I tried all combinations. It can't seem to find the resource(allthough its there). I also tried going to the resource, clicking right>properties and unchecking the "external file" checkbox but that didn't help either.
Any suggestions?
Kuniva
|
|
|
|
|
Hi Kuniva,
I don't know what Resource Type you're resource is, but that could be the problem. The RT_RCDATA thing is for a generic data resource. I've included some code that does just this (the resource is actually another .exe file for a self-extracting setup program). In the resource editor, what treeview heading does your resource appear under? In the example below, my resource has a type of "BIN" (which is what appears in the resource editor).
vhRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_DATA), _T("BIN"));
if (vhRsrc != NULL)
{
vhGlobal = LoadResource(NULL, vhRsrc);
if (vhGlobal != NULL)
{
vdwLen = SizeofResource(NULL, vhRsrc);
vpBuffer = (BYTE *)LockResource(vhGlobal);
}
}
Hope this helps.
------------------------
Derek Waters
derek@lj-oz.com
|
|
|
|
|
First of all thanks a lot for your help, I kind of got the resource to load and stuff anyways i got a handle to it with the method Derek described (thanks). BUT so i have a handle, one of my following questions is: "what is LockResource() for exactly?"
my second and perhaps most important question is: "How do i get the data from the file and write it to a file on disk so i have an exact replica of it?? What kind of variable do i use? How do i even get the data with the handle? I mean.. I have a handle, so what? I dont know how to use a handle... *blush* lolz"
Kuniva
|
|
|
|
|
Hi Kuniva,
Sorry about not replying earlier - I've been away on holidays.
LockResource() returns an LPVOID, ie a void pointer. This points at a block of memory that contains the raw resource data. You can get the length of this block of data using the function SizeofResource. Then you just need to open a file and write this block of data to disk. For example (no error checking):
HRSRC vhRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_MY_DATA), _T("DATA"));
HGLOBAL vhData = LoadResource(NULL, vhRsrc);
void *vpbData = LockResource(vhData);
DWORD vdwLen = SizeofResource(NULL, vhRsrc);
HANDLE vhMyFile = CreateFile(_T("D:\blah.dat"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile(vhMyFile, vpbData, vdwLen, &vdwWritten, NULL);
CloseHandle(vhMyFile);
------------------------
Derek Waters
derek@lj-oz.com
|
|
|
|
|
Thanks for your answer Derek allthough i had already got it to work.
Kuniva
|
|
|
|
|
Hello
is there a similiar "with" statement in c++ like in delphi?
for example I can do a lot of drawing stuff with this procedure:
with Foo do
begin
Rectangle...
LineTo...
MoveTo...
end
this way I don't need to put "Foo" before each function
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Could you please explain a bit what namespaces do and how to use them?
I never used them
regards
Gregor
modified 12-Sep-18 21:01pm.
|
|
|
|
|
a namespace is a way of encapsulating a set of classes (or functions, or variables, etc).
you can do something like this:
namespace fred
{
class myClass
{
...
};
}
now, if you want to create an object of type myClass, you have to tell the compiler to look in the "fred" namespace, like this:
fred::myClass var;
or, you can use the "using" keyword, like this:
using namespace fred;
myClass var;
"using" just tells the compiler that it's OK to search the namespace "fred" for any object definitions that it might need.
-c
Smaller Animals Software, Inc.
|
|
|
|
|
Thanks a lot!
But actually they're not a real substitute for the "with"-statement
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
|
Chris Losinger wrote:
"With" is a Pascal thing
Yes. It is also present in VB. Quite a nice feature, I think.
I vote pro drink
|
|
|
|
|
There's no equivalent of "with" in C/C++ but if you wanna avoid is typing a large name over and over again, you can create a short-name version of it using a reference variable:
CSomeType& t = thisReallyLongAndHorribleVariable;
t.Rectangle(...);
t.LineTo(...);
t.MoveTo(...);
Regards,
Alvaro
|
|
|
|
|
What's wrong with this piece of code?
The destructor of Class Value is never been called!
If I try to set a breakpoint there, the compiler immediately
disables it.
What am I doing here that's against the rules?
.h file
class Value
{
public:
unsigned char *pvalue;
Value(int size)
{
pvalue=new unsigned char(size);
}
~Value()
{
delete [] pvalue; <-- No break point here
}
};
class Parameter
{
public:
CString Name;
CString MenuItem;
void *pValue;
void *ValueText;
Parameter *next;
Parameter()
{
pValue = NULL;
next = NULL;
}
~Parameter()
{
if(pValue)
delete pValue;
}
};
.cpp file
Parameter* Parm = new Parameter();
Parm->pValue = new Value(4);
delete Parm;
Thanks, Arjan
|
|
|
|
|
|
You're probably confusing the debugger by having your implementation of ~Value() inside the class definition. split it up, put the definition in a .h file, and the implementation in a .cpp file. And it really is a sensible convention to start class name "C", ie "CValue".
Sorry to dissapoint you all with my lack of a witty or poignant signature.
|
|
|
|
|
hi all,
well this is a pretty long post, i have to warn you.
my problem lies in a program. the program parses conifguration files (transferred from os2) which are structered like that:
program1 key1 value1
program1 key2 value2
program2 key1 value1
program2 key2 value2
the program searches for a key (sz_addAfter) from a program sz_Prg. if it finds the key sz_addAfter it inserts the newKey and newValue after that Key.
if it can't find the key sz_addAfter it inserts the key at the end of the block of the program sz_Prg.
to insert a new key i use the function AddKey. It calls the little helper function CreateNewLine to insert the line.
the problem is that this program works most of the time very well.. but.. under rare circumstances it deletes one line from the source file (the last one).
i can't find which circumstances.. i just know that this happens from time to time.
the files are on a shared network folder and many processes can access them.
my problem is right now that i don't have got any clue, why this can happen, so i would be very happy if someone could find a flaw in the code and / or post
a better one for this problem.
sorry if there should be any typos in the code, cause i had to translate it from german to english.
thank you in advance
bernhard
the code i am talking about:
inline void CreateNewLine (const char* sz_Prg, const char* sz_newKey, const char* sz_newValue, int pos[3], std::string &newLine)
{
newLine = sz_Prg;
for (int i = 0; i < pos[2] ; i ++)
newLine += " ";
newLine.insert (pos[1], sz_newKey);
newLine.insert (pos[2], sz_newValue);
newLine.erase( newLine.find_last_not_of(" ") + 1);
}
bool AddKey (const char* sz_Prg, const char* sz_addAfter, const char* sz_newKey, const char* sz_newValue)
{
m_message = "";
int i = 0;
int pos [3] = {0};
std::ifstream file (m_filename.c_str());
std::ostringstream stream;
std::string line1;
std::string line2;
std::string newLine;
bool f_change = false;
while (1)
{
if (++i > 50000)
{
return false;
}
int i_changepos = 0;
int prgpos1 = 0;
int prgpos2 = 0;
int keypos1 = 0;
int keypos2 = 0;
if (!std::getline (file, line1))
break;
std::streampos here = file.tellg();
std::getline (file, line2);
file.seekg (here);
prgpos1 = line1.find (sz_Prg);
prgpos2 = line2.find (sz_Prg);
if (prgpos1 != std::string::npos || prgpos2 != std::string::npos)
{
for (int i = 0; i < 3; i ++)
{
if ( (i - 1) < 0)
pos[i] = line1.find_first_not_of (" ");
else
pos[i] = line1.find_first_not_of (" ", line1.find_first_of(" ",pos[i-1]));
}
}
keypos1 = line1.find (sz_addAfter);
keypos2 = line2.find (sz_addAfter);
if (keypos1 != std::string::npos && prgpos1 != std::string::npos && !f_change)
{
CreateNewLine (sz_Prg, sz_newKey, sz_newValue, pos, newLine);
f_change = true;
i_changepos = 1;
}
if (prgpos1 != std::string::npos && prgpos2 == std::string::npos && f_change == false)
{
CreateNewLine (sz_Prg, sz_newKey, sz_newValue, pos, newLine);
f_change = true;
i_changepos = 1;
}
if (i_changepos == 1)
stream << line1 << std::endl << newLine << std::endl;
else
stream << line1 << std::endl;
}
file.close();
if (f_change)
{
std::ofstream out (m_filename.c_str(), std::ios::trunc);
out << stream.str();
}
return true;
}
"There are three roads to ruin: women, gambling and technicians. The most pleasant is with women, the quickest is with gambling, but the surest is with technicians." Georges Pompidou
|
|
|
|
|
Hi, All!!!
=======
I have created a based dialog application using the MFC wizard.
I inserted an activeX control - Microsoft Internet Explorer.
The class wizard generated a code for me to use with this control (create CExp for the ctrl).
void CFooDlg::OnButton1()
{
CButton *qqq= new CButton();
qqq->Create(&qqq.wndTopMost, 50,50,300,300,SWP_NOSIZE);
qqq->ShowWindow(SW_SHOW);
}
the problem is that no matter what I do, it always covered by the activeX control
(button, edit, listbox, ...) and when I click on it where the listbox ctrl is under it
the CButton loses its focus
Why ???
How can I solve it ???
Please, Help me if u can.
thanks.
BTW: come to #CodeProject on DalNet (mIRC)
|
|
|
|
|
qqq->Create(&qqq.wndTopMost, 50,50,300,300,SWP_NOSIZE);
This compiles for you?
/ravi
"There is always one more bug..."
ravib@ravib.com
http://www.ravib.com
|
|
|
|
|