{"id":214,"date":"2009-05-31T13:37:13","date_gmt":"2009-05-31T18:37:13","guid":{"rendered":"http:\/\/jaewon.hwang.info\/jaewon\/?p=214"},"modified":"2011-02-25T16:37:26","modified_gmt":"2011-02-25T21:37:26","slug":"how-to-convert-bstr-to-lpctstr","status":"publish","type":"post","link":"https:\/\/jaewon.hwang.info\/jaewon\/2009\/05\/how-to-convert-bstr-to-lpctstr\/","title":{"rendered":"How to convert BSTR to LPCTSTR"},"content":{"rendered":"<p>I&#8217;ve had to deal with a pointer of a BSTR string returned to an event handler of an ActiveX control. I wanted to convert it to LPCTSTR, and a simple implicit type-casting worked seamlessly when I tried in a test project.<\/p>\n<pre class=\"brush: cpp; highlight: 5; toolbar: false;\">void put_UseServer(LPCTSTR newValue);\r\n\r\nvoid ServerChanged(BSTR* NewServer)\r\n{\r\n\tput_UseServer(*NewServer); \/\/ This conversion works only in Unicode Character Set\r\n}\r\n<\/pre>\n<p>However, when I copied the same code into my actual project, it caused an error. The reason was that my project used the Multi-byte Character Set whereas I tested the code in the Unicode Character Set. The simplest solution I found which also works in the Multi-byte Character set is like the following.<\/p>\n<pre class=\"brush: cpp; highlight: 5; toolbar: false;\">void put_UseServer(LPCTSTR newValue);\r\n\r\nvoid ServerChanged(BSTR* NewServer)\r\n{\r\n\tput_UseServer(COLE2CT(*NewServer)); \/\/ Use ATL String Conversion Class\r\n}\r\n<\/pre>\n<p>More information about ATL and MFC String Conversion is on the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/87zae4a3(VS.71).aspx\" target=\"_blank\">MSDN website<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve had to deal with a pointer of a BSTR string returned to an event handler of an ActiveX control. I wanted to convert it to LPCTSTR, and a simple implicit type-casting worked seamlessly when I tried in a test project. void put_UseServer(LPCTSTR newValue); void ServerChanged(BSTR* NewServer) { put_UseServer(*NewServer); \/\/ This conversion works only in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[28],"class_list":["post-214","post","type-post","status-publish","format-standard","hentry","category-computer","tag-visual-studio"],"_links":{"self":[{"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/posts\/214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/comments?post=214"}],"version-history":[{"count":0,"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/posts\/214\/revisions"}],"wp:attachment":[{"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/media?parent=214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/categories?post=214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/tags?post=214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}