views:

760

answers:

4

Can u Give solution for this code of typecasting, LPCTSTR(here lpsubkey) to Char* for below code snippet ,

char* s="HKEY_CURRENT_USER\\";
strcat(s,(char*)lpSubKey); 
printf("%S",s);

here it makes error of access violation ,so what will be the solution for that?. ...thanks in advance

+1  A: 

These are not the same thing. What are you trying to do?

The problem is you are trying to append to a string that you have not reserved memory for. Try:

char s[1024] = "HKEY_CURRENT_USER"; 
strcat(s,(char*)lpSubKey ); 
printf("%S",s);

Do be careful with the arbitrary size of 1024. If you expect your keys to be much longer your program will crash.

Also, look at strcat_s.

DanDan
char* s="HKEY_CURRENT_USER\\";strcat(s,(char*)lpSubKey ); printf("%S",s);here lpSubKey is LPCTSTR string variable .now can you explain the solution for what i am asked?...
Rajakumar
i thing,if suppose i using 's' only for five character means ,the remaining places are waste in my program..its correct or not?
Rajakumar
Yes, you are correct. If this is an issue, look at Plinth's solution which will dynamically resize the buffer to the exact length.
DanDan
A: 

ATL and MFC has set of macros to such conversion, where used next letters:

  • W - wide unicode string
  • T - generic character string
  • A - ANSI character string
  • OLE - BSTR string,

so in your case you need T2A macros

Dewfy
+2  A: 

There are several issues with your code that might well lead to the access violation. I don't think any have anything to do with the cast you mentioned.

You are assigning a pointer to the first element of a fixed size char array to a char * and then attempt to append to this using strcat. This is wrong as there is no additional space left in the implicitly allocated string array. You will need to allocate a buffer big enough to hold the resulting string and then copy the string constant in there before calling strcat. For example, like so:

char *s = (char*)malloc(1024 * sizeof(char));
strcpy(s, "HKEY_CURRENT_USER\\");
strcat(s, T2A(lpSubKey));
printf("%s", s);
free(s);

Please note that the fixed size array I'm allocating above is bad practise. In production code you should always determine the correct size of the array on the go to prevent buffer overflows or use functions like strncat and strncpy to ensure that you are not copying more data into the buffer than the buffer can hold.

Timo Geusch
i cannot proceed with your code ,error generate "T2A identifier is not Found" ,can i need any header in to my program?
Rajakumar
The T2A macro I was referring to is the one Dewfy mentioned. They're part of ATL and MFC so you'll need to include the appropriate header files.
Timo Geusch
A: 

strcat does not attempt to make room for the combination. You are overwriting memory that isn't part of the string. Off the top of my head:

char *strcat_with_alloc(char *s1, char *s2)
{
    if (!s1 || !s2) return NULL;

    size_t len1 = strlen(s1);
    size_t len2 = strlen(s2);

    char *dest = (char *)malloc(len1 + len2 + 1);
    if (!dest) return NULL;

    strcpy(dest, s1);
    strcat(dest, s2);

    return dest;
}

now try:

char* s="HKEY_CURRENT_USER\\";
char *fullKey = strcat_with_alloc(s,(char*)lpSubKey); 
if (!fullKey)
    printf("error no memory");
else {
    printf("%S",fullKey);
    free(fullKey);
}
plinth
thanks a lot to you....it works also i can understand ..what is the process happening there...
Rajakumar