Solved

Converting CString to const char*

Posted on 2006-07-03
16
2,063 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
Comment Utility
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
Comment Utility
try
(LPCSTR)_T(files.GetAt(i))

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


-MAHESH
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
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
 
LVL 22

Expert Comment

by:mahesh1402
Comment Utility
post you code about how 'files' is declared

0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Mahesh

Still i am getting the same error

Thanks
Sudhakar
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:mahesh1402
Comment Utility
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
Comment Utility
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
Comment Utility
or even

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

Author Comment

by:sudhakar_koundinya
Comment Utility
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
Comment Utility
try using _bstr_t()

cvLoadImage(_bstr_t(strFile),1);

where strFile is BSTR object

-MAHESH
0
 
LVL 22

Expert Comment

by:mahesh1402
Comment Utility
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
Comment Utility
OR

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

-MAHESH
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
Mahesh,

Thanks for all your help.

The above accepted function is working for me.

Thanks & Best Regards
Sudhakar Chavali
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now