Re: CString Array to LPCTSTR *

Tech-Archive recommends: Fix windows errors by optimizing your registry



Vipin wrote:

c_str() returns a pointer to the internal string, but I don't have a mechanism to resize the string to certain number of characters unless I assign a dummy string that wide. CString is pretty useful in the case I mentioned, I call RegQueryValue(...) with NULL and then I get the required size of memory, I use GetBuffer(...) and then pass in the pointer returned by it to RegQueryValue(...) again. Also c_str() returns a const char *, GetBuffer(...) gets me LPTSTR which is not constant. ofcourse I can cast the const char * to char* to make it work

CString is well suited for windows programming when I can use with win32 apis, but std::string falls short.


Vipin:

Personally I do not like GetBuffer/ReleaseBuffer. And it's no easier than using the standard library in your case.

long nData;
RegQueryValue(..., NULL, &nData);

Now there is no convenience difference between

std::vector<TCHAR> data(nData+1);
RegQueryValue(..., &data[0], nData);
CString str(&data[0]);

and

CString str;
RegQueryValue(..., str.GetBuffer(nData+1), nData);
str.ReleaseBuffer();

If you do not actually need the CString, then the standard library method is a shorter way to get a LPTSTR.

David Wilkinson

.



Relevant Pages

  • Re: CString Array to LPCTSTR *
    ... CString is pretty useful in the case I ... a const char *, GetBuffergets me LPTSTR which is not constant. ... RegQueryValue(..., str.GetBuffer(nData+1), nData); ...
    (microsoft.public.vc.mfc)
  • Re: CString Array to LPCTSTR *
    ... mechanism to resize the string to certain number of characters unless I ... assign a dummy string that wide. ... CString is pretty useful in the case I ... Also c_strreturns a const char *, ...
    (microsoft.public.vc.mfc)
  • Re: Can someone help with this error
    ... The error seems to occur when allocating a buffer in the CString ... NTDLL! ... operator new(unsigned int 0x00000064, int 0x00000001, const char * ... operator new(unsigned int 0x00000064, const char * 0x006a2cec THIS_FILE, ...
    (microsoft.public.vc.mfc)
  • Re: conversion of cstring to char[]
    ... A CString is a class which has an internal pointer to a buffer ... buffer and updating its internal pointer to point to the new buffer. ... You can get a pointer to the buffer by casting to "const char *" ...
    (microsoft.public.vc.language)
  • Re: Converting CString to char*
    ... You really should consider thinking of CString as a TCHAR manager, ... if it were declared as taking a const char* whether the ... > CString. ...
    (microsoft.public.vc.mfc)