Converting CString to const char*

Hi all,
I was trying to convert Cstring object to const char* in 2005 and either of below are not working. Could anybody help in this to type cast CString to const char*?

int nLen = files.GetAt(i).GetLength();
LPCSTR lpszBuf = files.GetAt(i).GetBuffer(nLen);


(LPCSTR)(CString)files.GetAt(i)

Thanks
Sudhakar
LVL 14
sudhakar_koundinyaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
mahesh1402Connect With a Mentor Commented:
OR

myFunc(BSTR bstr)
{
USES_CONVERSION;
char* pOut = OLE2A(bstr);
}

-MAHESH
0
 
sudhakar_koundinyaAuthor Commented:
I forgot to mention. I am getting below error
Error      1      error C2440: 'type cast' : cannot convert from 'ATL::CStringT<BaseType,StringTraits>' to 'LPCSTR'      c:\motionhistfilter\motionhistfilter\motionhistfilterdlg.cpp      207      


Thanks
Sudhakar
0
 
mahesh1402Commented:
try
(LPCSTR)_T(files.GetAt(i))

As Unicode is enabled by default in VS2005....


-MAHESH
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
sudhakar_koundinyaAuthor Commented:
i am getting below errors after including your suggested code

Error      1      error C2065: 'Lfiles' : undeclared identifier      c:\motionhistfilter\motionhistfilter\motionhistfilterdlg.cpp      215      
Error      2      error C2228: left of '.GetAt' must have class/struct/union      c:\motionhistfilter\motionhistfilter\motionhistfilterdlg.cpp      215      
0
 
mahesh1402Commented:
post you code about how 'files' is declared

0
 
sudhakar_koundinyaAuthor Commented:
CStringArray& files=list.GetFiles();
      

      
      for(int i=0;i<files.GetSize();i++)
      {
            IplImage* srcImage = cvLoadImage((LPCSTR)_T(files.GetAt(i)),1);            
              //other stuff

      }
0
 
mahesh1402Commented:
try :

for(int i=0;i<files.GetSize();i++)
     {
          IplImage* srcImage = cvLoadImage((LPCSTR)_T(files[i]),1);          
              //other stuff

     }
0
 
mahesh1402Commented:
you can use CString elements of CStringArray like you access normal array elements

CStringArray files..

access CString file name of above array as files[0]..files[1]..

-MAHESH
0
 
sudhakar_koundinyaAuthor Commented:
Mahesh

Still i am getting the same error

Thanks
Sudhakar
0
 
mahesh1402Commented:
i dont know what type of parameters cvLoadImage() accepts

as you know files[i] is of CString type then you may try

cvLoadImage((LPCSTR)files[i],1);

OR

cvLoadImage(LPCSTR(files[i]),1);

OR

cvLoadImage(files[i],1);

as per cvLoadImage() prototype

else try by turniing OFF UNICODE settings in you project

-MAHESH
0
 
mahesh1402Commented:
you can also try like if cvLoadImage accepts char * type :

char * strFile = (char *) (LPCTSTR)files[0];
cvLoadImage(strFile,1);

-MAHESH
0
 
mahesh1402Commented:
or even

char * strFile  = files[i].GetBuffer(0);
cvLoadImage(strFile,1);
0
 
sudhakar_koundinyaAuthor Commented:
Thanks for your support. But all above finctions are not working.

I am trying with BSTR strFile=file.AllocSysString();

Now could you please let me know how I can convert BSTR to LPCSTR?

Thanks
Sudhakar
0
 
mahesh1402Commented:
try using _bstr_t()

cvLoadImage(_bstr_t(strFile),1);

where strFile is BSTR object

-MAHESH
0
 
mahesh1402Commented:
or may be even easier

BSTR strFile = ...

const char * c;
c = (const char *)strFile;

...

but prefered us to use _bstr_t()

-MAHESH
0
 
sudhakar_koundinyaAuthor Commented:
Mahesh,

Thanks for all your help.

The above accepted function is working for me.

Thanks & Best Regards
Sudhakar Chavali
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.