The ultimate solution for converting _ variant_t type into char * in C/C ++

Source: Internet
Author: User
This is because when I access the database using ADO, the retrieved field value is of the _ variant_t type. When using the field value, type conversion is required. It is easy to convert to int. It takes a lot of time to convert to char. After searching for a long time on the Internet, find the relevant method. The most reprinted function is the varianttocstring (variant var) function. Of course, it is used to convert the cstring type. But after the application is applied, it is converted and the program reports an error. I don't know why.
Finally, I thought of a final solution. Although I did not find the root cause of the error, I successfully prevented the error from "appearing in front of you ". In fact, it is also very simple, just using try catch
_ Variant_t GPRS Sid;
Gprs sid = "1234567890"; // The value returned by the database processing function, that is, the value of the _ variant_t type.
Const char * GPRS Sid;
Try
{
_ Bstr_t bst_t = uplsid;
Gprs sid = (const char *) bst_t;
}
Catch (...)
{
// Printf ("% s \ r \ n", uplsid );
// The purpose of the above sentence is to test that the conversion of the GPRS Sid has been successful, but an error occurred in the middle. The comment has no effect on the program below, so the error is hidden, but the type conversion is successful, and the goal has been achieved.
}
// Continue to execute other code.

Comment: In fact, not only in C ++, but in other languages, such as C #, it must have been applied in the same industry. I call this method "Seeking for perfection!

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.