使用 wcscat_s 连接两个 wchar_t* 时出现访问冲突错误
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
path
是一个指向常量、不可变、只读数组的指针。您无法cat
进入其中,因为*cat()
函数想要写入到目标缓冲区,并在末尾附加数据。相反,创建一个可变的接收者缓冲区:(
更新:显然这个函数还有一个模板化重载,可以识别静态数组:
wcscat_s(dst, path);
。很整洁。)Your
path
is a pointer to a constant, immutable, read-only array. You cannotcat
into it, because the*cat()
functions want to write into the destination buffer, appending data at the end.Instead, create a mutable recipient buffer:
(Update: Apparently there's also a templated overload of this function that recognizes static arrays:
wcscat_s(dst, path);
. Neat.)您正在重写常量内存字符串的末尾。尝试对 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.