DllRegisterServer() triggering Windows Data Execution Protection (DEP) - String Manipulation Problem
-
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
-
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
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_»
-
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_»
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 -
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! MZRYou must use
_tsplitpath
. In thedir
parameter you will get the path minus the filename. Now you can concatenate the .tlb filename using_tcsncat_s
.«_Superman_»