2

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;
}
Andy Finkenstadt
  • 3,547
  • 1
  • 21
  • 25
andy
  • 484
  • 8
  • 21

2 Answers2

3

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

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • when I do this, It throws a runtime exception stating "string is not Null terminated". Any ideas? – andy Sep 09 '11 at 15:52
  • On which line does the exception occur? – Kerrek SB Sep 09 '11 at 15:53
  • occurs at wcscat_s(dst, LARGE_NUMBER, path); – andy Sep 09 '11 at 16:03
  • Very strange. Make sure you've defined `path` the way I did in my edit (you forgot the "const"), this should really work. – Kerrek SB Sep 09 '11 at 16:06
  • (Yes, that overload only saves you from having to type out the array size, it's no different otherwise.) I'm trying this with MingW at the moment, I really can't see why this should fail. `path` is clearly null-terminated... What does your debugger show? Are you sure it's that line and not the one below? Try commenting out the second line. – Kerrek SB Sep 09 '11 at 16:17
  • Throwing a Runtime Exception stating : Debug Assertion Failed! The File it's showing is f:\dd\vctools\crt_bld\selfx86\crt\src\tcscat_s.inl Line:32 Expression: (L"String is not Null Terminated" &&0) – andy Sep 09 '11 at 16:24
  • I don't have `wcscat_s()` in MingW, but when I try it with the standard `wcscat()` function, it works just fine. I'm suspecting something in your ambient code. Perhaps you can add a larger code snippet to your question which shows the context? – Kerrek SB Sep 09 '11 at 16:25
  • Two more ideas (not solutions): 1) remove the line with `rsize`, we don't need that anyway. 2) replace `wcscat_s` with `wcscat`, I'm curious what happens. – Kerrek SB Sep 10 '11 at 09:59
  • same result when I use wcscat. But I could get it work using the Buffer idea given by @Burton Samograd. But thank you very much for sticking around and trying to help me. Much appreciate it :) – andy Sep 10 '11 at 12:38
  • Ohhh, no, sorry, how stupid -- you have to zero out the `dst` array! How thick of me not to think of that. Say `wchar_t dst[1024] = { 0 };` – Kerrek SB Sep 10 '11 at 12:48
0

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.

Burton Samograd
  • 3,652
  • 19
  • 21
  • The Concatenation works perfectly, but as and when I use that buffer a bad_alloc exception occurs at some other location. Any ideas? – andy Sep 09 '11 at 16:15
  • Try making the buffer twice (or four times, etc) as big and see if you still get the error. Sounds like you're still writing off the end of the buffer, with the bad alloc happening when the buffer is being freed. – Burton Samograd Sep 09 '11 at 16:45
  • thank you...got it when I increased the buffer 4 times. But does it have any unwanted effects later on? – andy Sep 10 '11 at 05:58