Solved

Converting CString to const char*

Posted on 2006-07-03
16
2,111 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sum67 challenge 35 116
lucky13 challenge 11 159
matchUp  challenge 6 78
twoTwo  challenge 35 113
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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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