char* 值在赋值期间被损坏

发布于 2024-09-29 13:33:03 字数 2340 浏览 4 评论 0原文

我有以下代码,它从函数中获取返回值作为 char*

cDestDrive = ReadFromRegistry(HKEY_CURRENT_USER,NDSPATH,szDestPathRoot);

我能够读取 cDestDrive 内的值,直到我分配它。当我分配它时:

 CString strServerAddress = cDestDrive;

cDestDrive 的值被更改(损坏),并且我无法获取 CString strServerAdres 中的值,任何想法为什么会发生这种情况。

编辑: 从注册表读取的代码

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 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 "";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柏林苍穹下 2024-10-06 13:33:03

我相信您正在返回一个 char* 指向在堆栈上分配的数组,即这一行:

TCHAR tSZValue[MAX_PATH] = {0};

后面跟着:

char* cpRegKeyVal=tSZValue;

这是危险的,您正在经历第一手的最终结果!

编辑:为什么不在函数中直接分配给 CString 并返回它?

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?

只是一片海 2024-10-06 13:33:03

该函数返回一个指向 tSZValue 的指针,它是一个局部变量,因此当它超出范围时就不再存在。

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

香橙ぽ 2024-10-06 13:33:03

您将返回一个指向 tSZValue 的指针,它是一个临时变量,将在函数退出后的某个时间被覆盖。

最简单的解决方案:让 ReadFromRegistry() 返回 CString 而不是 char *

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 *.

忆梦 2024-10-06 13:33:03

看起来 ReadFromRegistry 没有分配内存来返回值(或者它确实分配了内存,但它位于堆栈上并在函数返回之前被销毁)。

也许您可以传入对 char * 的引用作为参数,而不是返回 char *,并在 ReadFromRegistry 之外分配内存。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文