Solved

Converting CString to const char*

Posted on 2006-07-03
16
2,087 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:sudhakar_koundinya
  • 10
  • 6
16 Comments
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 17034972
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
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17035108
try
(LPCSTR)_T(files.GetAt(i))

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


-MAHESH
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 17035167
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 22

Expert Comment

by:mahesh1402
ID: 17035179
post you code about how 'files' is declared

0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 17035190
CStringArray& files=list.GetFiles();
      

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

      }
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17035218
try :

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

     }
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17035231
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
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 17035263
Mahesh

Still i am getting the same error

Thanks
Sudhakar
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17035289
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
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17035339
you can also try like if cvLoadImage accepts char * type :

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

-MAHESH
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17035356
or even

char * strFile  = files[i].GetBuffer(0);
cvLoadImage(strFile,1);
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 17035435
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
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17035462
try using _bstr_t()

cvLoadImage(_bstr_t(strFile),1);

where strFile is BSTR object

-MAHESH
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17035477
or may be even easier

BSTR strFile = ...

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

...

but prefered us to use _bstr_t()

-MAHESH
0
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 50 total points
ID: 17035491
OR

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

-MAHESH
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 17035645
Mahesh,

Thanks for all your help.

The above accepted function is working for me.

Thanks & Best Regards
Sudhakar Chavali
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WinWaitActive parameters 12 31
difference between String.subString() and String.subSequence() 6 196
zeroMAx challenge 20 100
Problem to open Excel file 15 133
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question