|
Here have a look at this!
http://msdn2.microsoft.com/en-us/dxt965kz.aspx
Robbie
|
|
|
|
|
Hi guys,
I have a function where I am passing a value by reference. Below is the prototype:
bool retrieveLogFileLocation(LPSTR&);
Now within the body of the function definition I have the following function as well.
bool retrieveLogFileLocation(LPSTR& value){
char[255] lszValue;
HKEY hKey;
LONG returnStatus;
DWORD dwType = REG_SZ;
DWORD dwSize =255;
bool success = true;
RegQueryValueEx(hKey, "InstallationPath", NULL, &dwType, (LPBYTE)&lszValue, &dwSize);
strcpy(value, lszValue);
}
The problem I am receiving is that whenever my program runs, it crashes instantly, and I know for sure it is an issue with strcpy(value, lszValue) . I have tried in multiple ways through type-casting and such to resolve the issue, but I'm perplexed as to whats causing the problem.
Any help would be appreciated
Robbie
-- modified at 4:53 Monday 19th June, 2006
PS: I only included bits and pieces of the function definition simply because the actual function definition is very long. :P
|
|
|
|
|
capricious_001 wrote: Any help would be appreciated
May be the length of the receiving string variable is short as compared to the data you tring to copy into it.
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
hmm I didnt initialize the length of the referenced value variable to a certain length.
Actually I dont quite understand what you mean lol.
Robbie
|
|
|
|
|
An alternative way of doing this is to use a string class, e.g., CString. Here's some old code I dug out - look at the last if block - RegQueryValueEx call...
long OcipsAutoInterface::GetConsoleIDEx(LPCTSTR pszValueName, CString& strValueData)
{
HKEY hkOperatorConsole;
long nResult;
unsigned long nSize;
CString strSubKey;
strSubKey.LoadString( IDS_OP_CONSOLE_SUBKEY );
nResult = ::RegOpenKeyEx( HKEY_LOCAL_MACHINE,
LPCTSTR( strSubKey ),
0,
KEY_EXECUTE,
&hkOperatorConsole );
if ( ERROR_SUCCESS == nResult )
{
nResult = ::RegQueryValueEx( hkOperatorConsole, pszValueName, NULL, NULL, NULL, &nSize );
if ( ERROR_SUCCESS == nResult )
{
LPTSTR pszValueData = strValueData.GetBuffer( nSize );
nResult = ::RegQueryValueEx ( hkOperatorConsole, pszValueName, NULL, NULL, LPBYTE( pszValueData ), &nSize );
strValueData = pszValueData;
}
}
VERIFY( ERROR_SUCCESS == ::RegCloseKey( hkOperatorConsole ) );
return nResult;
}
Kevin
|
|
|
|
|
There isnt any other way? Because there is a lot of code I have to change if I change the type of that one variable. I'd prefer keeping value as a LPSTR type.
|
|
|
|
|
Well, in that case, as Laxman says, you need to carefully check the sizes of your source and destination buffers.
For future code though I recommend using string objects (string or CString) instead of C-style strings. You'll have fewer problems of this sort.
Kevin
|
|
|
|
|
Is there really a need to pass by reference cos i think it can work without it.
|
|
|
|
|
It's a problem with RegQueryValue(IMHO). You are not opening the registry before modifying it.
Handle to an open key. The key must have been opened with the KEY_QUERY_VALUE access right.
This handle is returned by the RegCreateKeyEx or RegOpenKeyEx function, or it can be one of the following predefined keys:
HKEY_CLASSES_ROOT
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_PERFORMANCE_DATA
HKEY_PERFORMANCE_NLSTEXT
HKEY_PERFORMANCE_TEXT
HKEY_USERS
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
Ya I created a handle to the open key and all of that is fine (closed the handle appropriately etc). Its just with the strcpy that I'm having the problems with.
As well, either by passing via reference or returning LPSTR value, I am still receiving the same unhandled exception error. It still crashes when my program is loaded.
|
|
|
|
|
I just did a test using your code(with some modifications in bold) except without the reference and it works fine.:
void somefunc()
{
char cBuffer[400];
retrieveLogFileLocation(cBuffer);
}
bool retrieveLogFileLocation(LPSTR value)
{
char lszValue[255];
HKEY hKey = HKEY_LOCAL_MACHINE;
LONG returnStatus;
DWORD dwType = REG_SZ;
DWORD dwSize =255;
bool success = true;
RegQueryValueEx(hKey, "InstallationPath", NULL, &dwType, (LPBYTE)&lszValue, &dwSize);
strcpy(value, lszValue);
return true;
} I set hKey just for testing purposes. It could be a problem with your key.
Last modified: Monday, June 19, 2006 4:57:58 AM --
|
|
|
|
|
_AnShUmAn_ wrote: You are not opening the registry before modifying it.
While it's true that the registry key must be opened prior to use, it's certainly not being modified.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
capricious_001 wrote: bool retrieveLogFileLocation(LPSTR& value){
What is the size of the variable that value refers to?
capricious_001 wrote: char[255] lszValue;
What's this?
capricious_001 wrote: RegQueryValueEx(hKey, "InstallationPath", NULL, &dwType, (LPBYTE)&lszValue, &dwSize);
What does RegQueryValueEx() return?
capricious_001 wrote: strcpy(value, lszValue);
Use strncpy() instead.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
How could I show a CString use notepad.exe without saving the string into a txt file?
|
|
|
|
|
See
<br />
::ShellExecute(NULL,_T("open"),_T("notepad.exe"),NULL,NULL,SW_SHOWNORMAL);<br />
Sleep(150);<br />
<br />
HWND hNoteWnd=::FindWindow(_T("Notepad"),NULL);<br />
LPCTSTR MY;<br />
MY=_T("Check");<br />
<br />
CWnd* hEditWnd=FindWindowEx(hNoteWnd,NULL,_T("Edit"),NULL);<br />
::SendMessage(hEditWnd->m_hWnd,WM_SETTEXT,0,(LPARAM)MY);<br />
<br />
whitesky
|
|
|
|
|
WhiteSky wrote: Sleep(150);
HWND hNoteWnd=::FindWindow(_T("Notepad"),NULL);
Use ShellExecuteEx() and these two statements can be avoided.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Thank you Mr DavidCrow for information
whitesky
|
|
|
|
|
|
Hi guys
I get following error when running my program.
Run-Time Check Failure #2 - Stack around the variable 'Temp' was corrupted.
where 'Temp' is a pointer to a class.
Does anyone know what the source of this run time error is ?
regards
Mahdi
|
|
|
|
|
Maybe you have some strings (or arrays) around the Temp variable, and you assigned too long values to that strings?
|
|
|
|
|
m_monhi wrote:
Does anyone know what the source of this run time error is ?
Could you provide some code snippet..??
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
m_monhi wrote:
Run-Time Check Failure #2 - Stack around the variable 'Temp' was corrupted.
Initialize Temp variable with NULL.. are you using Temp variable without being intialise.. this is common error arises in Visual Studio .net 2003 and above
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
Can you show your code that has error
whitesky
|
|
|
|
|
how can we apply socket program to send data from parent dialog to child dialog;
sree.
|
|
|
|
|
And why would one do this?
It's easy to pass data from parent dialog to child dialog using GetParent()......
etc.
However you will have to create a socket in your parent dialog, send data on it and then keep on listening to the port that you are binding the socket to, in your child dialog(in your case).
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|