convert a C# string to a unmanaged C char

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim

    convert a C# string to a unmanaged C char

    I have to access a third party api to work with their
    hardware. Everything was going great UNTIL I ran into the
    problem of C# char being 16 bits and the C char being 8
    bits. I cannot seem to cast the string properly to use it
    in the unmanaged code.

    I have tried to use the MarshalAs function in my DllImport
    statement but that didn't work.

    What i need is to take a string of numbers and convert it
    to a Zero Terminated C string or char. Can anyone please
    help me.
  • Michael Culley

    #2
    Re: convert a C# string to a unmanaged C char

    "Jim" <jime@kc.rr.com > wrote in message news:07aa01c3a9 8e$6b16c390$a30 1280a@phx.gbl.. .[color=blue]
    > I have to access a third party api to work with their
    > hardware. Everything was going great UNTIL I ran into the
    > problem of C# char being 16 bits and the C char being 8
    > bits. I cannot seem to cast the string properly to use it
    > in the unmanaged code.[/color]

    You shouldn't have to do anything, it should just marshal accross as an 8 bit null terminated char* that will work in C. eg

    [DllImport("user 32", EntryPoint= "MessageBox A")]
    private static extern int MessageBox(IntP tr hwnd, String lpText, String lpCaption, int wType);

    --
    Michael Culley


    "Jim" <jime@kc.rr.com > wrote in message news:07aa01c3a9 8e$6b16c390$a30 1280a@phx.gbl.. .[color=blue]
    > I have to access a third party api to work with their
    > hardware. Everything was going great UNTIL I ran into the
    > problem of C# char being 16 bits and the C char being 8
    > bits. I cannot seem to cast the string properly to use it
    > in the unmanaged code.
    >
    > I have tried to use the MarshalAs function in my DllImport
    > statement but that didn't work.
    >
    > What i need is to take a string of numbers and convert it
    > to a Zero Terminated C string or char. Can anyone please
    > help me.[/color]


    Comment

    • Habib Heydarian [MSFT]

      #3
      Re: convert a C# string to a unmanaged C char

      What do you get if you specify CharSet=CharSet .Auto in the DllImport
      declaration?

      HabibH.

      "Jim" <jime@kc.rr.com > wrote in message
      news:07aa01c3a9 8e$6b16c390$a30 1280a@phx.gbl.. .[color=blue]
      > I have to access a third party api to work with their
      > hardware. Everything was going great UNTIL I ran into the
      > problem of C# char being 16 bits and the C char being 8
      > bits. I cannot seem to cast the string properly to use it
      > in the unmanaged code.
      >
      > I have tried to use the MarshalAs function in my DllImport
      > statement but that didn't work.
      >
      > What i need is to take a string of numbers and convert it
      > to a Zero Terminated C string or char. Can anyone please
      > help me.[/color]


      Comment

      • Guest's Avatar

        #4
        Re: convert a C# string to a unmanaged C char

        [color=blue]
        >-----Original Message-----
        >"Jim" <jime@kc.rr.com > wrote in message[/color]
        news:07aa01c3a9 8e$6b16c390$a30 1280a@phx.gbl.. .[color=blue][color=green]
        >> I have to access a third party api to work with their
        >> hardware. Everything was going great UNTIL I ran into[/color][/color]
        the[color=blue][color=green]
        >> problem of C# char being 16 bits and the C char being 8
        >> bits. I cannot seem to cast the string properly to use[/color][/color]
        it[color=blue][color=green]
        >> in the unmanaged code.[/color]
        >
        >You shouldn't have to do anything, it should just marshal[/color]
        accross as an 8 bit null terminated char* that will work
        in C. eg[color=blue]
        >
        >[DllImport("user 32", EntryPoint= "MessageBox A")]
        >private static extern int MessageBox(IntP tr hwnd, String[/color]
        lpText, String lpCaption, int wType);[color=blue]
        >
        >--
        >Michael Culley
        >
        >
        >"Jim" <jime@kc.rr.com > wrote in message[/color]
        news:07aa01c3a9 8e$6b16c390$a30 1280a@phx.gbl.. .[color=blue][color=green]
        >> I have to access a third party api to work with their
        >> hardware. Everything was going great UNTIL I ran into[/color][/color]
        the[color=blue][color=green]
        >> problem of C# char being 16 bits and the C char being 8
        >> bits. I cannot seem to cast the string properly to use[/color][/color]
        it[color=blue][color=green]
        >> in the unmanaged code.
        >>
        >> I have tried to use the MarshalAs function in my[/color][/color]
        DllImport[color=blue][color=green]
        >> statement but that didn't work.
        >>
        >> What i need is to take a string of numbers and convert[/color][/color]
        it[color=blue][color=green]
        >> to a Zero Terminated C string or char. Can anyone[/color][/color]
        please[color=blue][color=green]
        >> help me.[/color]
        >
        >
        >.[/color]


        I have read that a string should marshal as a char* but
        for some reason mine is not doing it.
        this is my dll declaration
        [DllImport("Pika Api.dll")]
        public static extern int PK_VP_PlayDMTFS tring(uint hPort,
        string PK_PCHAR);

        then I call it from my code
        string x = textBox1.text;
        if(PK_VP_PlayDM TFString(hBoard Port, x) == PK_SUCCESS)

        but it doesn't work.

        any ideas?[color=blue]
        >[/color]

        Comment

        • Guest's Avatar

          #5
          Re: convert a C# string to a unmanaged C char

          [color=blue]
          >-----Original Message-----
          >What do you get if you specify CharSet=CharSet .Auto in[/color]
          the DllImport[color=blue]
          >declaration?
          >
          >HabibH.
          >
          >"Jim" <jime@kc.rr.com > wrote in message
          >news:07aa01c3a 98e$6b16c390$a3 01280a@phx.gbl. ..[color=green]
          >> I have to access a third party api to work with their
          >> hardware. Everything was going great UNTIL I ran into[/color][/color]
          the[color=blue][color=green]
          >> problem of C# char being 16 bits and the C char being 8
          >> bits. I cannot seem to cast the string properly to use[/color][/color]
          it[color=blue][color=green]
          >> in the unmanaged code.
          >>
          >> I have tried to use the MarshalAs function in my[/color][/color]
          DllImport[color=blue][color=green]
          >> statement but that didn't work.
          >>
          >> What i need is to take a string of numbers and convert[/color][/color]
          it[color=blue][color=green]
          >> to a Zero Terminated C string or char. Can anyone[/color][/color]
          please[color=blue][color=green]
          >> help me.[/color]
          >
          >
          >.[/color]

          I have not tried that.....[color=blue]
          >[/color]

          Comment

          • jim

            #6
            Re: convert a C# string to a unmanaged C char

            I have read that the string will marshal over to a char*
            but for some reason it didn't owrk for me.

            here is my DllImport code

            [DllImport("Pika API.dll")]
            public static extern uint PK_VP_PlayDTMFS tring(uint
            BoardPort, string PK_PCHAR);

            then I call it using

            string x = textBox1.text;
            PK_VP_PlayDTMFS tring(hPort, x);

            but not luck. any ideas?


            [color=blue]
            >-----Original Message-----
            >"Jim" <jime@kc.rr.com > wrote in message[/color]
            news:07aa01c3a9 8e$6b16c390$a30 1280a@phx.gbl.. .[color=blue][color=green]
            >> I have to access a third party api to work with their
            >> hardware. Everything was going great UNTIL I ran into[/color][/color]
            the[color=blue][color=green]
            >> problem of C# char being 16 bits and the C char being 8
            >> bits. I cannot seem to cast the string properly to use[/color][/color]
            it[color=blue][color=green]
            >> in the unmanaged code.[/color]
            >
            >You shouldn't have to do anything, it should just marshal[/color]
            accross as an 8 bit null terminated char* that will work
            in C. eg[color=blue]
            >
            >[DllImport("user 32", EntryPoint= "MessageBox A")]
            >private static extern int MessageBox(IntP tr hwnd, String[/color]
            lpText, String lpCaption, int wType);[color=blue]
            >
            >--
            >Michael Culley
            >
            >
            >"Jim" <jime@kc.rr.com > wrote in message[/color]
            news:07aa01c3a9 8e$6b16c390$a30 1280a@phx.gbl.. .[color=blue][color=green]
            >> I have to access a third party api to work with their
            >> hardware. Everything was going great UNTIL I ran into[/color][/color]
            the[color=blue][color=green]
            >> problem of C# char being 16 bits and the C char being 8
            >> bits. I cannot seem to cast the string properly to use[/color][/color]
            it[color=blue][color=green]
            >> in the unmanaged code.
            >>
            >> I have tried to use the MarshalAs function in my[/color][/color]
            DllImport[color=blue][color=green]
            >> statement but that didn't work.
            >>
            >> What i need is to take a string of numbers and convert[/color][/color]
            it[color=blue][color=green]
            >> to a Zero Terminated C string or char. Can anyone[/color][/color]
            please[color=blue][color=green]
            >> help me.[/color]
            >
            >
            >.
            >[/color]

            Comment

            • jim

              #7
              Re: convert a C# string to a unmanaged C char

              I have not tried that, I will try it first thing tomorrow.

              [color=blue]
              >-----Original Message-----
              >What do you get if you specify CharSet=CharSet .Auto in[/color]
              the DllImport[color=blue]
              >declaration?
              >
              >HabibH.
              >
              >"Jim" <jime@kc.rr.com > wrote in message
              >news:07aa01c3a 98e$6b16c390$a3 01280a@phx.gbl. ..[color=green]
              >> I have to access a third party api to work with their
              >> hardware. Everything was going great UNTIL I ran into[/color][/color]
              the[color=blue][color=green]
              >> problem of C# char being 16 bits and the C char being 8
              >> bits. I cannot seem to cast the string properly to use[/color][/color]
              it[color=blue][color=green]
              >> in the unmanaged code.
              >>
              >> I have tried to use the MarshalAs function in my[/color][/color]
              DllImport[color=blue][color=green]
              >> statement but that didn't work.
              >>
              >> What i need is to take a string of numbers and convert[/color][/color]
              it[color=blue][color=green]
              >> to a Zero Terminated C string or char. Can anyone[/color][/color]
              please[color=blue][color=green]
              >> help me.[/color]
              >
              >
              >.
              >[/color]

              Comment

              • Michael Culley

                #8
                Re: convert a C# string to a unmanaged C char

                I just tried this code and it worked ok. Try this on your system and see what you get. Maybe there is a global option that causes it
                to marshal as unicode or maybe it thinks your function is unicode because it doesn't end in a capital A. Try using
                CharSet=CharSet .Ansi

                [DllImport("user 32", EntryPoint= "MessageBox A")]
                private static extern int MessageBoxAPI(I ntPtr hwnd, string lpText, string lpCaption, int wType);

                string x = "This is a test!";
                string y = "Test caption!";
                MessageBoxAPI(t his.Handle, x, y, 0);

                --
                Michael Culley


                "jim" <jime@kc.rr.com > wrote in message news:0f1601c3a9 92$34e2e430$a00 1280a@phx.gbl.. .[color=blue]
                > I have read that the string will marshal over to a char*
                > but for some reason it didn't owrk for me.
                >
                > here is my DllImport code
                >
                > [DllImport("Pika API.dll")]
                > public static extern uint PK_VP_PlayDTMFS tring(uint
                > BoardPort, string PK_PCHAR);
                >
                > then I call it using
                >
                > string x = textBox1.text;
                > PK_VP_PlayDTMFS tring(hPort, x);
                >
                > but not luck. any ideas?
                >
                >
                >[color=green]
                > >-----Original Message-----
                > >"Jim" <jime@kc.rr.com > wrote in message[/color]
                > news:07aa01c3a9 8e$6b16c390$a30 1280a@phx.gbl.. .[color=green][color=darkred]
                > >> I have to access a third party api to work with their
                > >> hardware. Everything was going great UNTIL I ran into[/color][/color]
                > the[color=green][color=darkred]
                > >> problem of C# char being 16 bits and the C char being 8
                > >> bits. I cannot seem to cast the string properly to use[/color][/color]
                > it[color=green][color=darkred]
                > >> in the unmanaged code.[/color]
                > >
                > >You shouldn't have to do anything, it should just marshal[/color]
                > accross as an 8 bit null terminated char* that will work
                > in C. eg[color=green]
                > >
                > >[DllImport("user 32", EntryPoint= "MessageBox A")]
                > >private static extern int MessageBox(IntP tr hwnd, String[/color]
                > lpText, String lpCaption, int wType);[color=green]
                > >
                > >--
                > >Michael Culley
                > >
                > >
                > >"Jim" <jime@kc.rr.com > wrote in message[/color]
                > news:07aa01c3a9 8e$6b16c390$a30 1280a@phx.gbl.. .[color=green][color=darkred]
                > >> I have to access a third party api to work with their
                > >> hardware. Everything was going great UNTIL I ran into[/color][/color]
                > the[color=green][color=darkred]
                > >> problem of C# char being 16 bits and the C char being 8
                > >> bits. I cannot seem to cast the string properly to use[/color][/color]
                > it[color=green][color=darkred]
                > >> in the unmanaged code.
                > >>
                > >> I have tried to use the MarshalAs function in my[/color][/color]
                > DllImport[color=green][color=darkred]
                > >> statement but that didn't work.
                > >>
                > >> What i need is to take a string of numbers and convert[/color][/color]
                > it[color=green][color=darkred]
                > >> to a Zero Terminated C string or char. Can anyone[/color][/color]
                > please[color=green][color=darkred]
                > >> help me.[/color]
                > >
                > >
                > >.
                > >[/color][/color]


                Comment

                Working...