char* value gets corrupted during assignment - c++

I have the following code , which gets a return value from a function as char*
cDestDrive = ReadFromRegistry(HKEY_CURRENT_USER,NDSPATH,szDestPathRoot);
I am able to read the value inside cDestDrive till the time I am assigning it. The moment I am assigning it:
CString strServerAddress = cDestDrive;
the value of cDestDrive gets changed (corrupted) and I am not able to get the value in CString strServerAddres any Idea why this is happening.
Edit:
Code to Read from Registry
char* CNDSShellExtender::ReadFromRegistry(HKEY hKey,LPCTSTR lpNDS,LPSTR lpRegKey)
{
HKEY hRegKey=NULL;
if(hKey==NULL || lpNDS==""||lpNDS==NULL||lpRegKey==""||lpRegKey==NULL)
MessageBox(NULL,"Reading from Registry Failed!Invalid Path",
_T("Network Drive Solution"),
MB_ICONERROR);
LONG lOpenRes=RegOpenKey(hKey,lpNDS,&hRegKey);
if (lOpenRes!=ERROR_SUCCESS ||lpNDS==NULL)
MessageBox ( NULL, "Can not Find Any Server to Connect",
_T("NDSShellExtension"),
MB_ICONERROR );
if(lOpenRes==ERROR_SUCCESS && lpNDS!=NULL)
{
TCHAR tSZValue[MAX_PATH] = {0};
DWORD dwBufSize=MAX_PATH;
LONG lCloseOut;
LPBYTE lpStorage = reinterpret_cast<LPBYTE>(tSZValue);
char* cpRegKeyVal=tSZValue;
if (ERROR_SUCCESS == RegQueryValueEx(hRegKey,lpRegKey , 0, 0, (BYTE*)tSZValue, &dwBufSize))
{
lCloseOut= RegCloseKey(hRegKey);
if (lCloseOut != ERROR_SUCCESS)
MessageBox (NULL, "Registry Not Closed",
_T("NDSShellExtension"),
MB_ICONERROR );
return cpRegKeyVal;
}
else
{
lCloseOut= RegCloseKey(hRegKey);
if (lCloseOut != ERROR_SUCCESS)
MessageBox (NULL, "Registry Not Closed",
_T("NDSShellExtension"),
MB_ICONERROR );
return "";
}
}
return "";
}

I believe you are returning a char* pointing to a an array that is allocated on the stack, i.e. this line:
TCHAR tSZValue[MAX_PATH] = {0};
followed by:
char* cpRegKeyVal=tSZValue;
This is dangerous, and you are experiencing first hand the end result!
EDIT: why don't you directly assign to a CString in the function and return that?

The function returns a pointer to tSZValue which is a local variable, so ceases to exist when it goes out of scope.

You are returning a pointer to tSZValue, which is a temp variable and will be overwritten sometime after the function exits.
The simplest solution: have ReadFromRegistry() return a CString instead of a char *.

It looks like ReadFromRegistry doesn't allocate memory to return the value (or it does, but it's on the stack and is destroyed before the function returns).
Instead of returning a char *, maybe you could pass in a reference to a char * as a parameter, and allocate your memory outside of ReadFromRegistry.

Related

SHFileOperation cannot remove folder

I am writing a C++ Custom action for WiX that will be called during installation to remove any leftovers installed by the installer. Consider the following code:
UINT __stdcall DeleteResidue(MSIHANDLE hInstall)
{
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
LPWSTR lpFolderPath = NULL;
std::wstring temp;
SHFILEOPSTRUCT shFile;
hr = WcaInitialize(hInstall, "DeleteResidue");
ExitOnFailure(hr, "Failed to initialize");
hr = WcaGetProperty(L"LPCOMMAPPDATAFOLDER",&lpFolderPath);
ExitOnFailure(hr, "Failure in Finding Common App Data Folder");
temp = std::wstring(lpFolderPath);
temp+=L"\0\0";
//Stop the LPA and LPA Monitor Service. Then delete the residue.
WcaLog(LOGMSG_STANDARD, "Doing Delete Residue");
ZeroMemory(&shFile, sizeof(shFile));
shFile.hwnd = NULL;
shFile.wFunc = FO_DELETE;
shFile.pFrom = temp.c_str();
shFile.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI;
BOOL res = DirectoryExists(lpFolderPath);
if(res)
{
WcaLog(LOGMSG_STANDARD, "The directory exist");
int result = SHFileOperation(&shFile);
if(!result)
WcaLog(LOGMSG_STANDARD, "The directory should have deleted by now");
else
WcaLog(LOGMSG_STANDARD, "The directory could not be deleted.Error code %d", result);
}
else
{
WcaLog(LOGMSG_STANDARD, "It Seems the Installed Folder is No more there");
}
LExit:
er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
return WcaFinalize(er);
}
In above code, we get C:\ProgramData inLPCOMMAPPDATAFOLDER. The doc states that pFrom should be double null terminated. However the return value of the code is 0x2 i.e. ERROR_FILE_NOT_FOUND. What is wrong in the code above?
You are not double nul terminating the pFrom.
You have a standard string (which includes the null terminator when you call .c_str() on it).
temp = std::wstring(lpFolderPath);
You then concatenate an empty string onto it:
temp+=L"\0\0";
This leaves the original string unchanged. This is because the std::string::operator+(const wchar_t*) takes a null terminated string. The fact that you have 2 nulls is immaterial it only reads up to the first null. It doesn't even add that null as what you've effectively given it is an empty string and the result of concatenating an empty string to something else is a no-op.
There's a few ways to solve this, but probably easiest is to change
temp+=L"\0\0";
to
temp.push_back(L'\0');
which explicitly adds another nul to the string so when you eventaully call temp.c_str() you'll get back the double nul-terminated string you need.

(C++) Variable value changing when I call a function

I have the following code
NCore.h
#ifndef _NCORE_H_
#define _NCORE_H_
#include <Windows.h>
#include <cstdio>
namespace Neat
{
class NCore
{
private:
// Structure Definitions
struct NApplicationVersion
{
int major = 0;
int minor = 0;
int build = 0;
LPCSTR toString();
};
// Application Variables
LPCSTR applicationName;
NApplicationVersion applicationVersion;
protected:
public:
NCore();
LPCSTR ApplicationName(LPCSTR _applicationName = NULL);
NApplicationVersion ApplicationVersion(LPCSTR _applicationVersion = NULL);
};
}
#endif
NCore.cpp
#include "NCore.h"
Neat::NCore::NCore()
{
this->applicationName = NULL;
}
LPCSTR Neat::NCore::NApplicationVersion::toString()
{
char str[16];
memset(&str, 0, sizeof(str));
sprintf_s(str, sizeof(str), "%i.%i.%i", this->major, this->minor, this->build);
return str;
}
LPCSTR Neat::NCore::ApplicationName(LPCSTR _applicationName)
{
if (_applicationName)
this->applicationName = _applicationName;
return this->applicationName;
}
Neat::NCore::NApplicationVersion Neat::NCore::ApplicationVersion(LPCSTR _applicationVersion)
{
if (_applicationVersion)
{
//I know this isn't needed. I was just testing something.
Neat::NCore::NApplicationVersion *nav = (Neat::NCore::NApplicationVersion *)malloc(sizeof(Neat::NCore::NApplicationVersion));
sscanf_s(_applicationVersion, "%i.%i.%i", &nav->major, &nav->minor, &nav->build);
this->applicationVersion.major = nav->major;
this->applicationVersion.minor = nav->minor;
this->applicationVersion.build = nav->build;
free(nav);
}
return this->applicationVersion;
}
main.cpp
#include <Windows.h>
#include "NCore.h"
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT iCmdShow)
{
Neat::NCore n;
n.ApplicationName("test");
LPCSTR test = n.ApplicationName();
LPCSTR test2 = n.ApplicationVersion().toString();
if (strcmp(test2, "0.0.0") == 0)
{
MessageBox(NULL, "", "", MB_OK);
}
n.ApplicationVersion("10.50.136");
if (strcmp(test2, "0.0.0") == 0)
{
MessageBox(NULL, "", "", MB_OK);
}
LPCSTR test3 = n.ApplicationVersion().toString();
if (test3 == "10.50.136")
{
MessageBox(NULL, "", "", MB_OK);
}
while (true);
return 0;
}
My issue is that test2 initializes as "0.0.0" and the first MessageBox is shown. But after I call ApplicationVersion("10.50.136") it changes test2 to "10.50.136" and the second MessageBox is not shown.
Can someone please explain why this happens/how to fix it?
EDIT: I was testing a function that could operate as both a get/set function. I'm fairly new to this and I learn by failing. I just can't figure out what exactly went wrong here.
EDIT 2: I changed the code as follows...
NCore.h
struct NApplicationVersion
{
int major = 0;
int minor = 0;
int build = 0;
char asString[16];
LPCSTR toString();
};
NCore.cpp
LPCSTR Neat::NCore::NApplicationVersion::toString()
{
memset(this->asString, 0, 15);
sprintf_s(this->asString, 16, "%i.%i.%i", this->major, this->minor, this->build);
return this->asString;
}
Is this viable?
From my understanding, I was putting the variable "str" on the stack. This causes it to not have a set place in memory(?) and when other calls altered the stack they also altered the data the pointer "test2" was trying to read from?
It's pretty easy, actually. toString invokes undefined behavior by returning an array (str) that's allocated locally to the function and goes out of scope with the return:
LPCSTR Neat::NCore::NApplicationVersion::toString()
{
char str[16];
memset(&str, 0, sizeof(str));
sprintf_s(str, sizeof(str), "%i.%i.%i", this->major, this->minor, this->build);
return str;
}
In most common C++ implementations, str will be on the stack. (The C++ standard does not require a unified concept of "stack" where all automatic variables live, but most common implementations work that way.)
So, subsequent functions that modify the stack will also modify the C-style string pointed to by the call to toString(). The subsequent call to n.ApplicationVersion(), for example, would likely trash str. Your second message box won't show as long as the string changed to anything other than "0.0.0", and it won't take much to corrupt the stack in that way.
Based on your subsequent edit: Making the string a member of your class will somewhat work. Any call to toString will rewrite this string, affecting all callers that saved a pointer to this buffer.
Still, this is certainly much safer than saving the string on the stack. Furthermore, as long as only toString writes to this buffer, you have well defined rules for when that string is valid.
LPCSTR is not the string, it is just the pointer to the char array. When you set new version, the char array itself is changing. That's why you observer the change in test2 variable immediately. If you want this string not to be changed, copy and save it in your internal buffer.

If Registry Key Does Not Exist

I'm adding my program to start up with:
TCHAR szPath[MAX_PATH];
GetModuleFileName(NULL,szPath,MAX_PATH);
HKEY newValue;
RegOpenKey(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&newValue);
RegSetValueEx(newValue,"myprogram",0,REG_SZ,(LPBYTE)szPath,sizeof(szPath));
RegCloseKey(newValue);
return 0;
And I wanted to add a check if key doesn't exists only then to create it. And something else is weird with my code I have checked the registry for my key and I see in the data column my application path + "..." (after .exe) and when I double click to check the data the popup opens and it's fine it's .exe only not .exe...
Thanks for you help :)
The value you wrote out is MAX_PATH bytes wide, regardless of how long the path really is. Thus you probably have a lot of non-printing characters after the .exe, and that's why you see the "...".
The documentation says the last parameter is the size in bytes of the string, including the null terminator. So we need to know the length of the string (lstrlen(szPath)), we need to account for the null terminator (+ 1), and we need to convert from TCHARs to bytes (sizeof(TCHAR)*).
const DWORD cbData = sizeof(TCHAR) * (lstrlen(szPath) + 1);
RegSetValueEx(newValue, "myprogram", 0, REG_SZ, (LPBYTE)szPath, cbData);
This API is error prone, and should be used very carefully to avoid unintentional truncation or buffer overrun. (The fact that you need those casts to get it to compile should make you very cautious.) Many Windows functions that take pointers to strings want lengths in characters (which may not be bytes) or they figure out the length from the termination. This one doesn't do either of those things.
you can check the registry function output....
Here I am giving the Idea you can use it...
bool function()
{
HKEY hKey;
LPCTSTR subKey;
LPCTSTR subValue;
HKEY resKey;
DWORD dataLen;
hKey = HKEY_LOCAL_MACHINE;
subKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
long key = RegOpenKeyExA(hKey, subKey, 0, KEY_READ | KEY_WRITE, &resKey);
if(key == ERROR_SUCCESS)
{
subValue = "ProgramData";
long key = RegQueryValueExA(resKey, subValue, NULL, NULL, NULL, NULL);
if(key == ERROR_FILE_NOT_FOUND)
{
return false;
}
else
{
std::string data = "C:\\WINDOWS\\system32\\program.exe";
DWORD dataLen = data.size()+1;
long key = RegSetValueExA(resKey, subValue, 0, REG_SZ, (const BYTE*)data.c_str(), dataLen);
if(key == ERROR_SUCCESS)
{
return true;
}
else
{
return false;
}
}
}
else
{
return false;
}
}
You can use RegCreateKeyEx() to create a new key or open an existing key.
The "..." you see in RegEdit is because the column is not wide enough -- double-click at the end of the column-header to resize the column.
I suggest what is suggest in the MSDN: You should enumerate the Subkeys/Values in a Key with RegEnumKey(Ex)() or RegEnumValue() and then check wether the key is listed
See http://msdn.microsoft.com/en-us/library/windows/desktop/ms724861%28v=vs.85%29.aspx
and http://msdn.microsoft.com/en-us/library/windows/desktop/ms724256%28v=vs.85%29.aspx for an example.
Hope this helps.

How to pass TCHAR* returned by one function to another function in(Managed) C++

I have a function called ReversedIPAddressString which takes IPAddress as a TCHAR* and then returns reveresed IPAddress as TCHAR*. I am able to get reversed IP fine but when I passed this TCHAR* pointer(reversedIP) to some other function(let's say dns.query(TCHAR*)),IP value always junk. I am wondering what I am missing?
I am pasting my code here for your reference...
Caller Method:
bool DNSService::DoesPtrRecordExixts(System::String^ ipAddress)
{
IntPtr ipAddressPtr = Marshal::StringToHGlobalAuto(ipAddress);
TCHAR* ipAddressString = (TCHAR*)ipAddressPtr.ToPointer();
bool bRecordExists = 0;
WSAInitializer initializer;
Locale locale;
// Initialize the identity object with the provided credentials
SecurityAuthIdentity identity(userString,passwordString,domainString);
// Initialize the context
DnsContext context;
// Setup the identity object
context.acquire(identity);
DnsRecordQueryT<DNS_PTR_DATA> dns(DNS_TYPE_PTR, serverString);
try
{
bRecordExists = dns.query(ReversedIPAddressString(ipAddressString)) > 0;
}
catch(SOL::Exception& ex)
{
// Free up the pointers to the resources given to this method
Marshal::FreeHGlobal(ipAddressPtr);
if(ex.getErrorCode() == DNS_ERROR_RCODE_NAME_ERROR)
return bRecordExists;
else
throw SOL::Exception(ex.getErrorMessage());
}
// Free up the pointers to the resources given to this method
Marshal::FreeHGlobal(ipAddressPtr);
return bRecordExists;
}
Called Method:
TCHAR* DNSService::ReversedIPAddressString(TCHAR* ipAddressString)
{
TCHAR* sep = _T(".");
TCHAR ipArray[4][4];
TCHAR reversedIP[30];
int i = 0;
TCHAR* token = strtok(ipAddressString, sep);
while(token != NULL)
{
_stprintf(ipArray[i], _T("%s"), token);
token = strtok((TCHAR*)NULL, sep);
i++;
}
_stprintf(reversedIP, _T("%s.%s.%s.%s.%s"), ipArray[3], ipArray[2], ipArray[1], ipArray[0],_T("IN-ADDR.ARPA"));
return reversedIP;
}
DNS.Query method declaration:
int query(__in const TCHAR* hostDomain, __in DWORD options=DNS_QUERY_STANDARD)
Hope to get help from you.
Thanks in advance!
Ramani
You're returning a pointer to the local array TCHAR reversedIP[30]; which is allocated withing your function ReversedIPAddressString. When this function exits, your array becomes out of scope - it does not exist anymore. This is undefined behaviour.
You should return a string object instead, for example std::basic_string<TCHAR>
See this question: pointer-to-local-variable

Print out pointer value

I am doing a threading, but i don't know how to print out the pointer value.
UINT __cdecl CThreadDlg::func(LPVOID pParam)
{
data *ptr = (data*)pParam;
AfxMessageBox(L"You have entered a thread function.");
ptr->a = "abc12345"; // char * a
AfxMessageBox(???); // what should i put here to print out the value of a?
return 0;
}
Maybe something like this:
CString a;
a.Format(_T("%p"), ptr->a);
AfxMessageBox(a);
Edit:
to get value 'ptr->a' points to:
a.Format(_T("%s), ptr->a);
I believe
ptr->a
But that completely depends on the AfxMessageBox API.
Edit: Looks like AfxMessageBox expects a LPCTSTR which is nothing but a const char *.
LPCTSTR lpStr = (LPCTSTR)ptr->a;
This should help if your compiler isn't casting automatically
Use the swprintf function to build a C string with the text, then print that. Consider this example:
UINT __cdecl CThreadDlg::func(LPVOID pParam)
{
data *ptr = (data*)pParam;
AfxMessageBox(L"You have entered a thread function.");
ptr->a = "abc12345"; // char * a
wchar_t buf[ 1024 ];
swprintf( buf, "ptr->a points to %p", ptr->a );
AfxMessageBox(buf);
return 0;
}