A valid method for converting cstring to const char *

Source: Internet
Author: User

In Visual Studio 200x, it is a little difficult to directly convert cstring to const char *. The following is a feasible solution:

// Obtain the directory path

Cstring mpath = DLG. getpathname ();

// Convert the directory path to an absolute path for cvloadimage to call

Int M = mpath. getlength ();
For (INT Index = 0; index <m; index ++)
{
If (mpath. getat (INDEX) = '\\')
{
Mpath. insert (index ,'\\');
Index ++;
}

}

// Convert the cstring to the lpctstr

Lpctstr P = mpath. getbuffer ();

/*

Convert the lpctstr to const char *. Due to Unicode problems1. Const char * in a non-Unicode Environment *2. In the Unicode environment
Const unsigned short *

In Case 2, you need to use the API FunctionWidechartomultibyte

 
 
Int widechartomultibyte (uint codePage, // code page DWORD dwflags, // performance and mapping flags lpcwstr lpwidecharstr, // wide-character string int cchwidechar, // Number of chars in string lpstr lpmultibytestr, // buffer for new string int cbmultibyte, // size of buffer lpcstr lpdefaultchar, // default for unmappable chars lpbool lpuseddefachar char // set when default char used );


*/

DWORD dwminsize;
Dwminsize = widechartomultibyte (cp_acp, null, P,-1, null, 0, null, false );
Char * PP = new char [dwminsize];
Widechartomultibyte (cp_oemcp, null, P,-1, PP, dwminsize, null, false );

// Read the image
Iplimage * IP1 = cvloadimage (PP,-1 );


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.