How to DllImport function with "char**" parameter

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

    #1

    How to DllImport function with "char**" parameter

    Excuse me!!

    I have a DLL file named "TESTDLL.DL L".
    In TESTDLL.DLL
    export function :
    void GetPChar(char** pstr);

    In .NET C# ,
    How to call "GetPChar(char* * pstr)" function ?

    Thanks!!


  • Mattias Sjögren

    #2
    Re: How to DllImport function with "char**&qu ot; parameter

    Dennis,
    [color=blue]
    >Excuse me!!
    >
    >I have a DLL file named "TESTDLL.DL L".
    >In TESTDLL.DLL
    >export function :
    >void GetPChar(char** pstr);
    >
    >In .NET C# ,
    >How to call "GetPChar(char* * pstr)" function ?[/color]

    What does the function do? If it returns a single string pointer, try
    it like this

    [DllImport("test dll.dll")]
    static extern void GetPChar(out IntPtr pstr);

    ---
    IntPtr pstr;
    GetPChar(out pstr);
    string s = Marshal.PtrToSt ringAnsi(pstr);

    Then you might have to free the returned buffer somehow, depending on
    how its allocated by the DLL.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Dennis

      #3
      Re: How to DllImport function with "char**&qu ot; parameter

      Dear Mattias Sjögren :

      Thanks for your answer.

      But "char** pstr" is a string array.
      Such as
      pstr[0] ---> string a
      pstr[1] ---> string b
      pstr[2] ---> string c

      In C# ,
      How to declare and call it ?

      Thanks a lot!


      "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg>
      ???????:uzo4w5J VDHA.1748@TK2MS FTNGP12.phx.gbl ...[color=blue]
      > Dennis,
      >[color=green]
      > >Excuse me!!
      > >
      > >I have a DLL file named "TESTDLL.DL L".
      > >In TESTDLL.DLL
      > >export function :
      > >void GetPChar(char** pstr);
      > >
      > >In .NET C# ,
      > >How to call "GetPChar(char* * pstr)" function ?[/color]
      >
      > What does the function do? If it returns a single string pointer, try
      > it like this
      >
      > [DllImport("test dll.dll")]
      > static extern void GetPChar(out IntPtr pstr);
      >
      > ---
      > IntPtr pstr;
      > GetPChar(out pstr);
      > string s = Marshal.PtrToSt ringAnsi(pstr);
      >
      > Then you might have to free the returned buffer somehow, depending on
      > how its allocated by the DLL.
      >
      >
      >
      > Mattias
      >
      > --
      > Mattias Sjögren [MVP] mattias @ mvps.org
      > http://www.msjogren.net/dotnet/
      > Please reply only to the newsgroup.[/color]


      Comment

      • Pieter Philippaerts

        #4
        Re: How to DllImport function with &quot;char**&qu ot; parameter

        "Dennis" <ryu2@ms6.hinet .net> wrote in message[color=blue]
        > But "char** pstr" is a string array.
        > Such as
        > pstr[0] ---> string a
        > pstr[1] ---> string b
        > pstr[2] ---> string c
        > In C# ,
        > How to declare and call it ?[/color]

        Here's how I would do it [warning: haven't tested the code below]

        [DllImport("test dll.dll")]
        static extern void GetPChar(out IntPtr pstr);

        string[] ret = new string[count];
        IntPtr str;
        GetPChar(str);
        for(int i = 0; i < count; i++) {
        ret[i] = Marshal.PtrToSt ringAnsi(Marsha l.ReadIntPtr(st r, i *
        IntPtr.Size));
        }

        The above code asumes you know the number of items in the returned array
        ['count'] in advance.

        Regards,
        Pieter Philippaerts
        Managed SSL/TLS: http://www.mentalis.org/go.php?sl


        Comment

        Working...