Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. DllRegisterServer() triggering Windows Data Execution Protection (DEP) - String Manipulation Problem

DllRegisterServer() triggering Windows Data Execution Protection (DEP) - String Manipulation Problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpcomperformancequestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike the Red
    wrote on last edited by
    #1

    I have this code as part of my DllRegisterServer() method:

    TCHAR szPath[MAX_PATH];
    GetModuleFileName ( g_hinstThisDll, szPath, MAX_PATH );
    TCHAR * pdest = _tcsrchr(szPath, _T('\\'));
    TCHAR szTemp[MAX_PATH];
    _tcsncpy(szTemp, szPath, (size_t) (pdest + 1));
    _tcscpy(szPath, szTemp);
    _tcscat(szPath, "My.tlb");

    When I try to use RegSvr32.exe to register the DLL, I get a message that Windows DEP (Data Execution Protection) has stopped the execution of RegSvr32.exe. I replaced the above code segment with a constant assignment (snippit follows) and RegSvr32.exe completed successfully:

    TCHAR szPath[] = _T("C:\\SomePath\\My.tlb");

    I copied the problem code into the main() of a console application and outputted the result, which yielded the intended string. (The console application wasn't under Windows DEP.) From what little I know of DEP, this code must be doing something with protected memory in order to trip it. I don't usually use this style of string manipulation - I generally use CString's or .NET's String class. Can anyone see what I'm doing wrong here? Any help would be greatly appreciated. MZR

    _ 1 Reply Last reply
    0
    • M Mike the Red

      I have this code as part of my DllRegisterServer() method:

      TCHAR szPath[MAX_PATH];
      GetModuleFileName ( g_hinstThisDll, szPath, MAX_PATH );
      TCHAR * pdest = _tcsrchr(szPath, _T('\\'));
      TCHAR szTemp[MAX_PATH];
      _tcsncpy(szTemp, szPath, (size_t) (pdest + 1));
      _tcscpy(szPath, szTemp);
      _tcscat(szPath, "My.tlb");

      When I try to use RegSvr32.exe to register the DLL, I get a message that Windows DEP (Data Execution Protection) has stopped the execution of RegSvr32.exe. I replaced the above code segment with a constant assignment (snippit follows) and RegSvr32.exe completed successfully:

      TCHAR szPath[] = _T("C:\\SomePath\\My.tlb");

      I copied the problem code into the main() of a console application and outputted the result, which yielded the intended string. (The console application wasn't under Windows DEP.) From what little I know of DEP, this code must be doing something with protected memory in order to trip it. I don't usually use this style of string manipulation - I generally use CString's or .NET's String class. Can anyone see what I'm doing wrong here? Any help would be greatly appreciated. MZR

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Your code has a bug.

      Mike the Red wrote:

      _tcsncpy(szTemp, szPath, (size_t) (pdest + 1));

      What do you expect here as the count parameter? It is taking the value as an address plus 1. This is going to be a very big value. Another example of why typecasting must be avoided. I wonder how it worked in a console application. I tried it in a console application and it gave me the DEP error.

      «_Superman_»

      M 1 Reply Last reply
      0
      • _ _Superman_

        Your code has a bug.

        Mike the Red wrote:

        _tcsncpy(szTemp, szPath, (size_t) (pdest + 1));

        What do you expect here as the count parameter? It is taking the value as an address plus 1. This is going to be a very big value. Another example of why typecasting must be avoided. I wonder how it worked in a console application. I tried it in a console application and it gave me the DEP error.

        «_Superman_»

        M Offline
        M Offline
        Mike the Red
        wrote on last edited by
        #3

        Thanks for the quick response, Superman! That code you quoted (_tcsncpy(szTemp, szPath, (size_t) (pdest + 1));) was copied from the MS help file's example code for _tcsrchr. To be honest, I copied it without careful examination. What I was expecting for the the count parameter was the index of the last \ in the file path. GetModuleFileName returns the full path to the .DLL, say "c:\some path\This.DLL". I'm trying to extract the path and change the filename to the type lib's name, for instance "c:\some path\My.tlb". Now that I re-read the helpfile on _tcsrchr, "Returns a pointer to the last occurrence of c in string, or NULL if c is not found", I actually have no idea how I would make use of this function. At this point, I would probably manually iterate through the filepath string to find the index of the last \, then pull a substring and concatenate the type lib's filename. Is there a more efficient way to do this with the built-in string manipulation routines? Thanks again for your help! MZR

        _ 1 Reply Last reply
        0
        • M Mike the Red

          Thanks for the quick response, Superman! That code you quoted (_tcsncpy(szTemp, szPath, (size_t) (pdest + 1));) was copied from the MS help file's example code for _tcsrchr. To be honest, I copied it without careful examination. What I was expecting for the the count parameter was the index of the last \ in the file path. GetModuleFileName returns the full path to the .DLL, say "c:\some path\This.DLL". I'm trying to extract the path and change the filename to the type lib's name, for instance "c:\some path\My.tlb". Now that I re-read the helpfile on _tcsrchr, "Returns a pointer to the last occurrence of c in string, or NULL if c is not found", I actually have no idea how I would make use of this function. At this point, I would probably manually iterate through the filepath string to find the index of the last \, then pull a substring and concatenate the type lib's filename. Is there a more efficient way to do this with the built-in string manipulation routines? Thanks again for your help! MZR

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          You must use _tsplitpath. In the dir parameter you will get the path minus the filename. Now you can concatenate the .tlb filename using _tcsncat_s.

          «_Superman_»

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups