RE: Question about different string type in VC++

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi,

Since you have probably modified the string, you have to call
ReleaseBuffer(). Your

void CSimpleDlg::OnBTNTest()
{
// TODO: Add your control notification handler code here
CString sInput;
LPSTR sTemp;

GetDlgItemText(IDC_TXTInput,sInput); // get the string to be
trimmed

sTemp=Trim(sInput.GetBuffer(0));
sInput.ReleaseBuffer(); // must be called since 'sInput' might have
changed

GetDlgItem(IDC_TXTOutput)->SetWindowText(sTemp); // display output
}

Hope this helps,
Geo


"xg" wrote:

> Hi, I want to write a simple Trim() function to trim the spaces around a
> LPSTR string. I created a dialog based app to test the routine, the Trim()
> function seems to be OK, but when I close the dialog, it causes General
> Protection Fault. I tried some other approaches but no go. Since I only
> intent to use Ansi string, so I did not use any _T() macro or _TCHAR() macro
> in Trim(). Please tell me what I did wrong. Thanks.
>
> Here are the code fragments:
>
> void CSimpleDlg::OnBTNTest()
> {
> // TODO: Add your control notification handler code here
> CString sInput;
> LPSTR sTemp;
>
> GetDlgItemText(IDC_TXTInput,sInput); // get the string to be
> trimmed
>
> sTemp=Trim(sInput.GetBuffer(0));
>
> GetDlgItem(IDC_TXTOutput)->SetWindowText(sTemp); // display output
> } // after the function
> exits, Crash!
>
> LPSTR Trim(LPCSTR s)
> {
> LPSTR sResult=new (CHAR);
> int i;
>
> if(s==NULL) return NULL;
>
> lstrcpy(sResult,s);
>
> for(i=0;i<lstrlen(sResult)&&sResult[i]==' ';i++);
> sResult+=i;
>
> for(i=lstrlen(sResult)-1;i>0&&sResult[i]==' ';i--);
> *(sResult+i+1)='\0';
>
> return sResult;
> }
>
>
>
.



Relevant Pages

  • Re: Question about different string type in VC++
    ... There is no need to write your own Trim() function; you could just as easily have written ... See my essay on avoiding GetDlgItem on my MVP Tips ... Having allocated a single CHAR to hold the string, ... >LPSTR string. ...
    (microsoft.public.vc.mfc)
  • Re: Optimization of code
    ... When I compile the code you gave me to search for string I get 3 errors, ... LPSTR found = strstr; ... I beg to differ that you didn't use CStringA ...
    (microsoft.public.vc.mfc)
  • LPSTR an Funktion übergeben
    ... ] String szText); ... Stringchar test = new Stringchar; ... Also gehe ich davon aus das ich irgendwas mit dem LPSTR ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: Need help calling a proprietary C DLL from Python
    ... LPBSTR = POINTER ... VmxInfo.argtypes = [LPHANDLE, LPSTR] ... res = VmxInfo, byref) ... Note how the string buffer parameter has NO odd games on it. ...
    (comp.lang.python)
  • Re: But..... WHY??? Access Violation
    ... Since the function later on pokes nulls into the string it's ... LPCSTR and LPSTR are stupid names for types anyhow. ...
    (comp.lang.cpp)