Link to home
Start Free TrialLog in
Avatar of sudhakar_koundinya
sudhakar_koundinya

asked on

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
Avatar of sudhakar_koundinya
sudhakar_koundinya

ASKER

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
try
(LPCSTR)_T(files.GetAt(i))

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


-MAHESH
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      
post you code about how 'files' is declared

CStringArray& files=list.GetFiles();
      

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

      }
try :

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

     }
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
Mahesh

Still i am getting the same error

Thanks
Sudhakar
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
you can also try like if cvLoadImage accepts char * type :

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

-MAHESH
or even

char * strFile  = files[i].GetBuffer(0);
cvLoadImage(strFile,1);
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
try using _bstr_t()

cvLoadImage(_bstr_t(strFile),1);

where strFile is BSTR object

-MAHESH
or may be even easier

BSTR strFile = ...

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

...

but prefered us to use _bstr_t()

-MAHESH
ASKER CERTIFIED SOLUTION
Avatar of mahesh1402
mahesh1402
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Mahesh,

Thanks for all your help.

The above accepted function is working for me.

Thanks & Best Regards
Sudhakar Chavali