Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Using CString in MC++ project

Using CString in MC++ project

Scheduled Pinned Locked Moved Managed C++/CLI
c++helptutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Katamneni
    wrote on last edited by
    #1

    Hi all I'm trying to use a UnmanagedC++ class in my MC++ application.The unmanaged class internally using CString class as member variables.I'm converting the String to LPCTSTR and passing it as a parameter to the methods of the unmanaged class.There i'm getting access voilations on CString class GetData()method(this is inside the class CString).Can anybody help me how to solve this problem and what are all the other settings needed.(in project options i have enbaled using MFC in dynamic libraries). Thanks You Rgds Krishna

    T 1 Reply Last reply
    0
    • K Katamneni

      Hi all I'm trying to use a UnmanagedC++ class in my MC++ application.The unmanaged class internally using CString class as member variables.I'm converting the String to LPCTSTR and passing it as a parameter to the methods of the unmanaged class.There i'm getting access voilations on CString class GetData()method(this is inside the class CString).Can anybody help me how to solve this problem and what are all the other settings needed.(in project options i have enbaled using MFC in dynamic libraries). Thanks You Rgds Krishna

      T Offline
      T Offline
      Tom Archer
      wrote on last edited by
      #2

      Are you making sure to pin the String object's memory so that the GC doesn't move it once you've converted it? For example, the following code would convert a String to a __wchar_t type (note the pin keyword). #include <vcclr.h> //... String* s = S"Hello"; const __wchar_t __pin * str = PtrToStringChars(s); Cheers, Tom Archer Inside C#,
      Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

      K 1 Reply Last reply
      0
      • T Tom Archer

        Are you making sure to pin the String object's memory so that the GC doesn't move it once you've converted it? For example, the following code would convert a String to a __wchar_t type (note the pin keyword). #include <vcclr.h> //... String* s = S"Hello"; const __wchar_t __pin * str = PtrToStringChars(s); Cheers, Tom Archer Inside C#,
        Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

        K Offline
        K Offline
        Katamneni
        wrote on last edited by
        #3

        I'm not using that method for conversion.My code looks like this LPCTSTR _pszFileName = 0; _pszFileName = static_cast(const_cast(static_cast(Marshal::StringToHGlobalAnsi(m_FileName)))); this->m_pUnManageClass->SetFilePath(_pszFileName); //inside the unmanaged class i have a member variable of type CString // m_strFilePath = _pszFileName. Marshal::FreeHGlobal(static_cast(const_cast(static_cast(_pszFileName)))); after this,any operation on the CString variable causing access voilation at GetData() member of the CString template class. Thank You verymuch Rgds Krishna

        T I 2 Replies Last reply
        0
        • K Katamneni

          I'm not using that method for conversion.My code looks like this LPCTSTR _pszFileName = 0; _pszFileName = static_cast(const_cast(static_cast(Marshal::StringToHGlobalAnsi(m_FileName)))); this->m_pUnManageClass->SetFilePath(_pszFileName); //inside the unmanaged class i have a member variable of type CString // m_strFilePath = _pszFileName. Marshal::FreeHGlobal(static_cast(const_cast(static_cast(_pszFileName)))); after this,any operation on the CString variable causing access voilation at GetData() member of the CString template class. Thank You verymuch Rgds Krishna

          T Offline
          T Offline
          Tom Archer
          wrote on last edited by
          #4

          And since we know that your method is not working, have you tried my suggestion? Cheers, Tom Archer Inside C#,
          Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

          I 1 Reply Last reply
          0
          • T Tom Archer

            And since we know that your method is not working, have you tried my suggestion? Cheers, Tom Archer Inside C#,
            Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

            I Offline
            I Offline
            igor1960
            wrote on last edited by
            #5

            And since we know that your method is not working, have you tried my suggestion? C'mon Tom, this is not an answer... What's wrong with his code??? "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

            1 Reply Last reply
            0
            • K Katamneni

              I'm not using that method for conversion.My code looks like this LPCTSTR _pszFileName = 0; _pszFileName = static_cast(const_cast(static_cast(Marshal::StringToHGlobalAnsi(m_FileName)))); this->m_pUnManageClass->SetFilePath(_pszFileName); //inside the unmanaged class i have a member variable of type CString // m_strFilePath = _pszFileName. Marshal::FreeHGlobal(static_cast(const_cast(static_cast(_pszFileName)))); after this,any operation on the CString variable causing access voilation at GetData() member of the CString template class. Thank You verymuch Rgds Krishna

              I Offline
              I Offline
              igor1960
              wrote on last edited by
              #6

              Why are you using LPCTSTR -- shouldn't LPSTR be used? Your Unmanaged MFC implementation: is it Unicode? If yes it may explain the reason of problem: StringToHGlobalAnsi converts to Ansi(single byte) -- while your unmanaged MFC maybe expecting Unicode... Just my 2 cents "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups