P/Invoke Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TWlrZSBN?=

    #1

    P/Invoke Problem

    Hello,

    I have the following C functions exported from a DLL:

    void copyString2(con st char * sSrcBuf, long nSrcBufSize, char ** sDestBuf);
    void freeString(char * sBuf);

    The idea is to copy the source buffer into the dynamically allocated
    destination buffer returning the pointer to the buffer to C# then pushing the
    pointer back to the DLL for deallocation.

    I'm having a tough time declaring the appropriate type for the char **
    sDestBuf parameter. I've been trying to use various combination of IntPtr
    declarations but no matter what I define at runtime the pointer passed to the
    C library is NULL for the sDestBuf.

    For example:
    [DllImport("nati velibtest.dll", CharSet = CharSet.Ansi)]
    public static extern unsafe
    bool copyString2(str ing sSrcStr, long nSrcSize, IntPtr * sDestBuf);

    IntPtr pPtr = new IntPtr();

    bool bSuccess = NativeLibTest.c opyString2(sMsg , sMsg.Length, &pPtr);

    In the debugger, &pPtr certainly has an address but that address is not
    passed to the DLL.

    Any help would be appreciated.

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: P/Invoke Problem

    Mike,

    You don't need unsafe code to do this. You can just pass the last
    parameter as a ref IntPtr, and then the IntPtr should have a value written
    to it. You then have to pass that IntPtr to the static PtrToStringAnsi
    method on the Marshal class to get a String instance you can work with.

    Of course, you have to make sure you pass the IntPtr back to freeString.
    You can just declare the parameter of that function as an IntPtr, and it
    should work fine.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m


    "Mike M" <Mike M@discussions.m icrosoft.comwrote in message
    news:9BE7B596-3720-483E-AB46-6E328EFC2E13@mi crosoft.com...
    Hello,
    >
    I have the following C functions exported from a DLL:
    >
    void copyString2(con st char * sSrcBuf, long nSrcBufSize, char **
    sDestBuf);
    void freeString(char * sBuf);
    >
    The idea is to copy the source buffer into the dynamically allocated
    destination buffer returning the pointer to the buffer to C# then pushing
    the
    pointer back to the DLL for deallocation.
    >
    I'm having a tough time declaring the appropriate type for the char **
    sDestBuf parameter. I've been trying to use various combination of IntPtr
    declarations but no matter what I define at runtime the pointer passed to
    the
    C library is NULL for the sDestBuf.
    >
    For example:
    [DllImport("nati velibtest.dll", CharSet = CharSet.Ansi)]
    public static extern unsafe
    bool copyString2(str ing sSrcStr, long nSrcSize, IntPtr * sDestBuf);
    >
    IntPtr pPtr = new IntPtr();
    >
    bool bSuccess = NativeLibTest.c opyString2(sMsg , sMsg.Length, &pPtr);
    >
    In the debugger, &pPtr certainly has an address but that address is not
    passed to the DLL.
    >
    Any help would be appreciated.
    >

    Comment

    • Mattias Sjögren

      #3
      Re: P/Invoke Problem

      >void copyString2(con st char * sSrcBuf, long nSrcBufSize, char ** sDestBuf);
      [...]
      >[DllImport("nati velibtest.dll", CharSet = CharSet.Ansi)]
      >public static extern unsafe
      >bool copyString2(str ing sSrcStr, long nSrcSize, IntPtr * sDestBuf);
      nSrcSize should be an int in C# to match the long in C. And how did
      the void return type became bool on the C# side?


      Mattias

      --
      Mattias Sjögren [C# MVP] mattias @ mvps.org
      http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      Please reply only to the newsgroup.

      Comment

      • =?Utf-8?B?TWlrZSBN?=

        #4
        Re: P/Invoke Problem

        Oh, that was just me switching back and forth between copy/paste and
        typing...the both should have been bool.

        Comment

        • =?Utf-8?B?TWlrZSBN?=

          #5
          Re: P/Invoke Problem

          Nope, not having too much luck.

          Anybody have any p/invoke samples calling a library function with a
          parameter using 2 level of indirection where the C function allocates memory
          and returns the pointer back to C#? Then the C# caller is responsible for
          calling a "free" function exposed by the native library.



          Comment

          • Willy Denoyette [MVP]

            #6
            Re: P/Invoke Problem

            "Mike M" <Mike M@discussions.m icrosoft.comwrote in message
            news:2BDA9FEE-F5FB-4A86-9965-BE8580D321E1@mi crosoft.com...
            Nope, not having too much luck.
            >
            Anybody have any p/invoke samples calling a library function with a
            parameter using 2 level of indirection where the C function allocates
            memory
            and returns the pointer back to C#? Then the C# caller is responsible for
            calling a "free" function exposed by the native library.
            >
            >
            >

            // C# code
            [DllImport("xxxx xx"), SuppressUnmanag edCodeSecurity] static extern void
            Test(out IntPtr s);

            ....
            IntPtr buffer;
            Test(out buffer);
            Console.WriteLi ne(Marshal.PtrT oStringAnsi(buf fer));

            ....

            // C code

            extern "C"
            {
            void __declspec(dlle xport) __stdcall Test(void **ptr)
            {
            int bufferSize = 25;
            *ptr = new unsigned char(bufferSize );
            strcpy_s((char* )*ptr, bufferSize, "Hello from C");
            }
            }

            Willy.

            Comment

            • =?Utf-8?B?TWlrZSBN?=

              #7
              Re: P/Invoke Problem

              Thank you for your time.

              My problem was the bit width of the nSrcSize integer parameter in my C#
              declaration. Should have been using int as Mattias pointed out earlier.

              Thanks again,
              Mike

              Comment

              Working...