error when using wmemcpy
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: error when using wmemcpy

  1. #1
    Join Date
    Nov 2009
    Posts
    128

    Question error when using wmemcpy

    Code:
    struct DeviceParam {
      
     UINT32 count;
        UINT32 selection;
     wchar_t *MyStrings[MAX_DEVICES];
     
    };
    _declspec(dllexport) int Device(DeviceParam *objnew)
    {
         wchar_t *temp[MAX_DEVICES];
         objnew->MyStrings[0] = L"Device1";
      temp[0]= L"Device2";
      int size_t = wcslen(temp[0]);
         wchar_t *ptr;
      ptr=wmemcpy( objnew->MyStrings[0], temp[0], size_t);
    }
    when i compile the code i get the following error ,
    An unhandled exception of type 'System.AccessViolationException' occurred in TestLib.exe

    Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    where i am doing wrong ?

  2. #2
    Join Date
    Jun 2012
    Posts
    58

    Re: error when using wmemcpy

    tmp[0] is a ptr to a constant, read-only string. You cant write to it. (and if you could, who guratnetess there`s enough free memory?).
    If you want a writable buffer you have to explicitly reserve the amount of memory either

    on the stack, locally (wchar path[MAC_PATH]), where MAX_PATH is the maximal length of the string.
    or on the heap (new/delete).

    Alternatively, you can use the std::string class for handling strings, which does memory management for you and allows for direct assigning of strings to each other.

    Lastly, dont use size_t as a variable name, it`s a reserved keyword with most compilers.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center