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
  1. Home
  2. General Programming
  3. C#
  4. Typecasting string to char * in C#

Typecasting string to char * in C#

Scheduled Pinned Locked Moved C#
csharptutorialquestion
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.
  • V Offline
    V Offline
    Vikas K
    wrote on last edited by
    #1

    Hi, Sorry if this is too basic, Can you please let me know how to typecast string to char* in C#? I need this as Im calling a managed DLL function. Thanks Vikas

    L J 2 Replies Last reply
    0
    • V Vikas K

      Hi, Sorry if this is too basic, Can you please let me know how to typecast string to char* in C#? I need this as Im calling a managed DLL function. Thanks Vikas

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Do you mean char[] ? string s = "abc"; char[] c = s.ToCharArray();

      V 1 Reply Last reply
      0
      • V Vikas K

        Hi, Sorry if this is too basic, Can you please let me know how to typecast string to char* in C#? I need this as Im calling a managed DLL function. Thanks Vikas

        J Offline
        J Offline
        J a a n s
        wrote on last edited by
        #3

        Vikas K. wrote:

        Im calling a managed DLL function

        I don't think that you will be using pointers much in managed environment.

        Vikas K. wrote:

        string to char*

        If you want to call some native C++ functions you can use Marshal.StringToHGlobalAnsi[^] method.

        *jaans

        1 Reply Last reply
        0
        • L Lost User

          Do you mean char[] ? string s = "abc"; char[] c = s.ToCharArray();

          V Offline
          V Offline
          Vikas K
          wrote on last edited by
          #4

          I mean to char*

          L 1 Reply Last reply
          0
          • V Vikas K

            I mean to char*

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            "Managed" ??? Do you want to use pointer in managed environment ???

            V 1 Reply Last reply
            0
            • L Lost User

              "Managed" ??? Do you want to use pointer in managed environment ???

              V Offline
              V Offline
              Vikas K
              wrote on last edited by
              #6

              Actually from C# I'm calling an unmanaged MFC DLL. MFC DLL has an API for example: int fnSetMonitorNo(char* m_MonitorNo) int fnGetMonitorNo(char* m_MonitorNo) If I pass char* to the DLL, it will see the MonitorNo. I tried to do in C# as follows [DllImport("OfficeCTE.dll")] public static extern unsafe int fnSetMonitorNo([MarshalAs(UnmanagedType.LPStr)]string arg1); [DllImport("OfficeCTE.dll")] public static extern unsafe int fnGetMonitorNo([MarshalAs(UnmanagedType.LPStr)]string arg1); But it didn't work. How can this be done?

              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