none
Pointer of String in C# RRS feed

  • Question

  • Hello guys,

    I need to use an external non com dlls (made in C) which as a method with the following signature: public static external void logic_open(long xpto).

    In this method the long atribute is a reference to a string which by the way is a ip adress of a machine.

    I cannot change the c api, so how can I in c#, get the pointer of such string and pass it to the function?

    I have already tried marshalling and all that stuff but nothing...

    Thanks


    Rui Machado

    Wednesday, April 24, 2013 2:13 PM

Answers

  • Depending on whether xpto is an Ansi or Unicode string, try a fragment like this:

    String s = "123.45.67.89";
    IntPtr p = Marshal.StringToCoTaskMemAnsi(s);
    long xpto = p.ToInt64();

    See the documentation about how to free the strings after logic_open.
    • Marked as answer by ruimachado Wednesday, April 24, 2013 7:32 PM
    Wednesday, April 24, 2013 7:29 PM

All replies

  • Depending on whether xpto is an Ansi or Unicode string, try a fragment like this:

    String s = "123.45.67.89";
    IntPtr p = Marshal.StringToCoTaskMemAnsi(s);
    long xpto = p.ToInt64();

    See the documentation about how to free the strings after logic_open.
    • Marked as answer by ruimachado Wednesday, April 24, 2013 7:32 PM
    Wednesday, April 24, 2013 7:29 PM
  • that was it. i noticed that my c function accepts IntPtr aswell so i just made the folowing:

    var p = Marshal.StringToBST("192.125.12.14");

    ....myfunction_open(p);

    And it worked!!

    Thanks a lot


    Rui Machado

    Wednesday, April 24, 2013 7:39 PM