使用 wcscat_s 连接两个 wchar_t* 时出现访问冲突错误

发布于 2024-12-03 22:41:16 字数 1681 浏览 0 评论 0原文

我正在尝试使用 wcscat_s 函数将一个 wchar[] 连接到 wchar_t* 。我不断收到访问冲突错误。

这是代码 有

HANDLE hFindDll = FindFirstFile(dllDir,&findDllData);
wchar_t *path = L"C:\\Users\\andy\\Documents\\SampleProj\\";
rsize_t rsize = wcslen(path)+wcslen(findDllData.cFileName)+5;
wcscat_s(path,rsize,findDllData.cFileName);

什么建议我哪里出错了吗?

PS 如果我使用 wchar_t path[] 而不是 wchar_t* path ,我会在调试模式下收到损坏警告,但当我单击“继续”时,它会执行而不会中断应用程序。在发布模式下,错误根本不会显示。

问候, andy

更新:这是完整的代码:我想要实现的是从嵌入 dll 的资源中播放波形文件...

int _tmain(int argc, _TCHAR* argv[])
{
    WIN32_FIND_DATA findDllData;
    HANDLE hFindDll;
    LPCWSTR dllDir = L"C:\\Users\\andy\\Documents\\SampleProj\\*.dll";
    HMODULE hICR;
    HRSRC hRes;

hFindDll = FindFirstFile(dllDir,&findDllData);
        if(hFindDll != INVALID_HANDLE_VALUE)
        {
            do
            {
                const wchar_t * path = L"C:\\Users\\andy\\Documents\\SampleProj\\";
                rsize_t rsize = wcslen(path)+wcslen(findDllData.cFileName)+2;
                wchar_t dst[1024];
                wcscat_s(dst,1024,path); //--> this is where EXCEPTION occurs
                wcscat_s(dst,1024,findDllData.cFileName);


                hICR = LoadLibrary(dst);
                hRes = FindResource(hICR, MAKEINTRESOURCE(200), _T("WAVE"));
                if(hRes != NULL)
                {
                    break;
                }
            }while(FindNextFile(hFindDll,&findDllData));
            HGLOBAL hResLoad = LoadResource(hICR, hRes);
            PlaySound(MAKEINTRESOURCE(200), hICR,SND_RESOURCE | SND_ASYNC); 
        }

return 0;
}

I am trying to concatenate one wchar[] to a wchar_t* using wcscat_s function. I keep getting Access Violation Error.

Here is the code

HANDLE hFindDll = FindFirstFile(dllDir,&findDllData);
wchar_t *path = L"C:\\Users\\andy\\Documents\\SampleProj\\";
rsize_t rsize = wcslen(path)+wcslen(findDllData.cFileName)+5;
wcscat_s(path,rsize,findDllData.cFileName);

Any suggestions where I am going wrong?

P.S If I use wchar_t path[] instead of wchar_t* path , I get a Corruption Warning in debug mode, but it executes without breaking the applicaiton when I click continue. In release mode, the error doesn't show at all.

regards,
andy

Update: Here is the entire code: what I want to achieve is to play a wave file from a resoure embedded in a dll...

int _tmain(int argc, _TCHAR* argv[])
{
    WIN32_FIND_DATA findDllData;
    HANDLE hFindDll;
    LPCWSTR dllDir = L"C:\\Users\\andy\\Documents\\SampleProj\\*.dll";
    HMODULE hICR;
    HRSRC hRes;

hFindDll = FindFirstFile(dllDir,&findDllData);
        if(hFindDll != INVALID_HANDLE_VALUE)
        {
            do
            {
                const wchar_t * path = L"C:\\Users\\andy\\Documents\\SampleProj\\";
                rsize_t rsize = wcslen(path)+wcslen(findDllData.cFileName)+2;
                wchar_t dst[1024];
                wcscat_s(dst,1024,path); //--> this is where EXCEPTION occurs
                wcscat_s(dst,1024,findDllData.cFileName);


                hICR = LoadLibrary(dst);
                hRes = FindResource(hICR, MAKEINTRESOURCE(200), _T("WAVE"));
                if(hRes != NULL)
                {
                    break;
                }
            }while(FindNextFile(hFindDll,&findDllData));
            HGLOBAL hResLoad = LoadResource(hICR, hRes);
            PlaySound(MAKEINTRESOURCE(200), hICR,SND_RESOURCE | SND_ASYNC); 
        }

return 0;
}

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

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

发布评论

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

评论(2

似梦非梦 2024-12-10 22:41:16

您的path是一个指向常量、不可变、只读数组的指针。您无法 cat 进入其中,因为 *cat() 函数想要写入到目标缓冲区,并在末尾附加数据。

相反,创建一个可变的接收者缓冲区:(

const wchar_t * path = L"C:\\Users\\andy\\Documents\\SampleProj\\";

wchar_t dst[LARGE_NUMBER] = { 0 };  // ugh, very 1990

wcscat_s(dst, LARGE_NUMBER, path);
wcscat_s(dst, LARGE_NUMBER, findDllData.cFileName);

更新:显然这个函数还有一个模板化重载,可以识别静态数组:wcscat_s(dst, path);。很整洁。)

Your path is a pointer to a constant, immutable, read-only array. You cannot cat into it, because the *cat() functions want to write into the destination buffer, appending data at the end.

Instead, create a mutable recipient buffer:

const wchar_t * path = L"C:\\Users\\andy\\Documents\\SampleProj\\";

wchar_t dst[LARGE_NUMBER] = { 0 };  // ugh, very 1990

wcscat_s(dst, LARGE_NUMBER, path);
wcscat_s(dst, LARGE_NUMBER, findDllData.cFileName);

(Update: Apparently there's also a templated overload of this function that recognizes static arrays: wcscat_s(dst, path);. Neat.)

老子叫无熙 2024-12-10 22:41:16

您正在重写常量内存字符串的末尾。尝试对 rsize 长度的 wchat_t* 缓冲区进行 malloc 并复制路径 path,然后将文件名附加到其中。

You're writing over the end of a constant memory string. Try malloc'ing a wchat_t* buffer of a rsize length and copying path path and then appending filename into it.

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