CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2004
    Posts
    8

    ActiveX properties

    I currently am coding an activeX control and I need to have a property that is a string. The only string type that it appears I can use is the BSTR type. But I need it to be a LPCTSTR type. What is the code for changing the type from a BSTR to a LPCTSTR type? Iam not familiar with type casting in c++ as I usually deal with java so sorry if this is a stupid question!

  2. #2
    Join Date
    Mar 2002
    Location
    Philadelphia
    Posts
    150
    Strings in COM interfaces are typically BSTRs which are WCHAR based. LPCTSTR are sometimes char based. LPCTSTR changes based on whether you are doing a unicode build or not. Therefore you cannot use LPCTSTR in a COM interface. All strings surfaced as properties in an ActiveX control must be BSTRs.

    If you still want to you can convert to/from unicode strings try MSDN TN059: Using MFC MBCS/Unicode Conversion Macros. OLE2CT will convert from a BSTR to a LPCTSTR.
    Sincerely,
    - Ron

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: ActiveX properties

    Originally posted by Mikewithersone
    I currently am coding an activeX control and I need to have a property that is a string. The only string type that it appears I can use is the BSTR type. But I need it to be a LPCTSTR type. What is the code for changing the type from a BSTR to a LPCTSTR type? Iam not familiar with type casting in c++ as I usually deal with java so sorry if this is a stupid question!
    Not need to think about casting BSTR to LPCTSTR or whatever.
    Let Class(good)Wizard to add a property of BSTR type (e.g. "TestProperty"), then...
    add a member variable of type CString:
    Code:
    class C_test_ocxCtrl : public COleControl
    {
    // Atributes:
    protected:
       CString m_strTestProperty;
    //...
    };
    modify the corresponding Get/Set functions as follows:
    Code:
    BSTR C_test_ocxCtrl::GetTestProperty() 
    {
       return m_strTestProperty.AllocSysString();
    }
    void C_test_ocxCtrl::SetTestProperty(LPCTSTR lpszNewValue) 
    {
       m_strTestProperty = lpszNewValue;
       SetModifiedFlag();
    }
    When inserting the control into a project, you will see in generated wrapper class something more familiar (and no BSTRs
    Code:
    class C_test_ocx : public CWnd
    {
    // ...
    // Attributes
    public:
       CString GetTestProperty();
       void SetTestProperty(LPCTSTR);
    };
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured