|
That way you have to be very careful.
I think it is an hazardous design.
Probably it would be better if both memory allocation and deallocation are delegated to the caller (e.g. the standard behaviour of Windows API).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Hi,
CPallini wrote: Probably it would be better if both memory allocation and deallocation are delegated to the caller (e.g. the standard behaviour of Windows API).
I didnot understand what you are referring to??
Priya Sundar
|
|
|
|
|
Was a note on Nelek suggestion.
For instance GetWindowText WIN32 API work that way, the caller has to
(1) Allocate a buffer
const int SIZE = 256;
TCHAR * buffer = new TCHAR[SIZE];
(2) pass it (together as is size) as function argument
if ( buffer)
{
GetWindowText(hWnd, buffer, SIZE);
}
(3) release the buffer
if (buffer) delete buffer;
buffer = NULL;
BTW all the above stuff can be done (and the life will be easier...) using the stack instead of the heap.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Can you explain a bit what are you referring to? I don't understand you well and I want to learn more.
That this is not the best option... I agree (I know I am just a low/middle skilled programmer that needs to learn more), but... what do you mean with the standard behaviour of windows api? can you put an example?
Maybe something like this?
LPTSTR COperDialog::ConvertResourceStringIdToLptstr(UINT nResString, LPSTR pStr)
{
CString strLoad;
strLoad.LoadString(nResString);
pStr = new TCHAR[strLoad.GetLength()+1];
CHECK_FOR_NULL(pStr)
_tcscpy(pStr,strLoad);
return pStr;
}
void COperDialog::AddProjectRootItem()
{
TV_INSERTSTRUCT curTreeItem;
curTreeItem.hParent =TVI_ROOT;
curTreeItem.hInsertAfter = TVI_ROOT;
curTreeItem.item.iImage = 0;
curTreeItem.item.iSelectedImage = 0;
;
LPSTR pMyLpstr = NULL;
curTreeItem.item.pszText = ConvertResourceStringIdToLptstr(IDS_PROJECT, pMyLpstr);
;
delete [] pMyLpstr; pMyLpstr = NULL;
curTreeItem.item.cchTextMax=15;
curTreeItem.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
htrProjectRoot = m_Edit->InsertItem(&curTreeItem);
}
Is this option better than the other one?
-- modified at 8:11 Friday 23rd November, 2007
-- modified at 8:12 Friday 23rd November, 2007
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
|
|
|
|
|
Nelek wrote: Maybe something like this?
No.
Nelek wrote: LPTSTR COperDialog::ConvertResourceStringIdToLptstr(UINT nResString, LPSTR pStr) //parameter added
The parameter added is irrelevant.
I meant the standard behaviour of WIN32 API, for instance:
int GetWindowText( HWND hWnd, LPTSTR lpString, int nMaxCount );
The above function doesn't allocate the buffer, it relies on the one allocated by the caller.
Please note that your suggestion is a perfectly viable solution I only noted that it must be handled with care.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Yes, it can be viable, but I prefer to know the best ways to do things.
CPallini wrote: int GetWindowText( HWND hWnd, LPTSTR lpString, int nMaxCount );
The above function doesn't allocate the buffer, it relies on the one allocated by the caller.
So before calling the function you have to create the LPTSTR lpString to give him as a parameter, and after the use delete it. And what is the difference with my second option? That the new is inside the function that is being called? For this schema... how can be dealed? To use the new, it must be known the length of the loadString that is calculated inside the called function. (I know, resctructurating and not using the function is a solution)
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
|
|
|
|
|
Nelek wrote: And what is the difference with my second option? That the new is inside the function that is being called?
Yes, the caller has total responsibility on buffer allocation/deletion. This, IMHO is a quite acceptable design choice.
On the other hand, I insist on saying that yours is a viable solution.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Thanks for answering,
CPallini wrote: On the other hand, I insist on saying that yours is a viable solution.
I know. But I want to learn, and be better. I like to programm against idiots (although I know is very complicated) and my programms usually are working in 99% of time and situations. But that doesn't mean that are "designly correct". I actually studied electronic engineering not informatic. But that doesn't mean that I may not be able to be a good programer, if I learn with the bests
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
|
|
|
|
|
Nelek wrote: I like to programm against idiots
Oh, it is a lost battle.
Nelek wrote: I actually studied electronic engineering not informatic. But that doesn't mean that I may not be able to be a good programer, if I learn with the bests.
I think the same about me (forced point: I have a physics background ).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Very glad to know people!
I am also from an ECE(Electronic & Comm Eng). I guess i can get encouraged to do better in programming by you all!!
Thankyou!
Priya Sundar
|
|
|
|
|
Thanks a lot!!
It did solve my problem for the time being.
As the other person has commented, as this is sort of hazardous, will try if there is any other way of doing it.
Again thanks a lot!
Sometimes, too much into coding... then a simple common sense logic also seems to be complex!
Have a nice evening!
Priya Sundar
|
|
|
|
|
Discard your endless-name-function and simply do:
...
CString strLoad;
strLoad.LoadString(nResString);
curTreeItem.item.pszText =(LPTSTR)(LPCTSTR) strLoad;
...
You can safely do that since you're inserting an item, i.e. you know that your buffer will only read.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
I need to apply this for some 50 strings.. so it will be bulky code if i write these statements for every treectrl item. So i need it as a function which will be just a single line of code!
Thankyou for your suggestion!
Priya Sundar
|
|
|
|
|
Priya_Sundar wrote: I need to apply this for some 50 strings
Good to know. In such a case you may prefer to allocate a single buffer instead of doing all that allocation/deallocation stuff.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Could you help me more with respect to the code i have posted? i.e how you want me to allocate single buffer??
Priya Sundar
|
|
|
|
|
At the very beginnning of the process you may allocate a buffer (wide enaough to contain the biggest string you're dealing with), then use it for the retrieval of every string, maybe using the raw WIN32 API
int LoadString( HINSTANCE hInstance, UINT uID, LPTSTR lpBuffer, int nBufferMax);
finally you deallocate the buffer when the last string has been retrieved.
You can also rely on the stack for such purpose:
{
const int SIZE=1000;
TCHAR buffer[SIZE];
LoadString( hInstance, uID1, buffer, SIZE);
...
LoadString( hInstance, uID2, buffer, SIZE);
...
LoadString( hInstance, uIDN, buffer, SIZE);
...
}
Note: I omitted (leaving it to the reader) the error checking code.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Hi,
Thank you very much.
I am able to get the strings displayed properly in the RELEASE VERSION. But in debug version some special characters are getting displayed as some letters as the part of the required string to be displayed!
Do you have any suggestions so as to explain why it is happening like that??
Also at the initial stage of my program when the string resource has not been yet initialized by the program, there are some global variables where i am assinging some strings.
Do you have any idea regarding how to load string values for such variables from the string table which at that point have not been initialized(eg. registry var).
Appreciate your help! Thankyou once again!!
Priya Sundar
|
|
|
|
|
hi,
i am using VC++ 6 editor and i am handling a project that was done by another team of developers and i am having a problem in debugging the static library in the project workspace. when i am trying to put a breakpoint in the static library source file the compilor will disable the breakpoint and move the cursour to the winmain. The following message box window is comming while i try on F5.
---------------------------
Microsoft Visual C++
---------------------------
One or more breakpoints cannot be set and have been disabled. Execution will stop at the beginning of the program.
---------------------------
OK
---------------------------
My work space contained of two exe projects and 3 static librararies and a Dll project all the exe and dll projects allow me to put the break point in the source editor and allow me to debug. But the static library project editor is not allowing to do the debug. What is the Problem. Is i am missing anything. Need your help urgently.
Thanks
Shine joseph
|
|
|
|
|
The debug information cant proceeded: most common case is that is a release build.
Greetings from Germany
|
|
|
|
|
No it is in the Debug Mode. Here is the confiiguration
/nologo /MTd /w /W0 /Gm /GX /Zi /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /FR"Debug/" /Fp"Debug/Sockets.pch" /Yu"stdafx.h" /Fo"Debug/" /Fd"Debug/" /FD /GZ /Zm200 /c
and
WIN32,_DEBUG,_WINDOWS,_MBCS
|
|
|
|
|
hello,
I set a value in registry. I checked the registry, the value was really set. And then I read it and try to print it. But the output is aways zero.
my code is:
char regValue[20];
unsigned long dataType;
RegOpenKey(HKEY_LOCAL_MACHINE,keyStr,&hkey);
dwRegValue = 100000;
RegSetValueEx(hkey,L"V1",0, REG_DWORD, (LPBYTE)&dwRegValue, sizeof(DWORD));
RegCloseKey(hkey);
RegOpenKey(HKEY_LOCAL_MACHINE,keyStr,&hkey);
RegQueryValueEx(hkey,L"V1",0, &dataType,(LPBYTE)regValue,&nSize);
RegCloseKey(hkey);
printf("\nregvalue: %d", atol(regValue)); //the output is: regvalue: 0
I don't know what's wrong with my code. could someone help me to solve the problem?
thanks in advance!
|
|
|
|
|
dadacncn wrote: char regValue[20];
Since you're retrieving a DWORD value, change the above, for instance into:
DWORD dwRegValueBack;
then you have also to change
dadacncn wrote: RegQueryValueEx(hkey,L"V1",0, &dataType,(LPBYTE)regValue,&nSize);
into:
RegQueryValueEx(hkey,L"V1",0, &dataType,(LPBYTE)&dwRegValueBack,&nSize);
and finally
dadacncn wrote: printf("\nregvalue: %d", atol(regValue)); //the output is: regvalue: 0
into:
printf("\nregvalue: %d", dwRegValueBack);
Hope that helps
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
thank you very much! it works!
|
|
|
|
|
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
dadacncn wrote: thank you very much! it works!
Just a comment, please use CRegKey class, which is optimized for this type of stuff!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|