Re: Calls to native dlls from C#

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: dln (dnadon_nospm_at_hotmail.com)
Date: 03/03/04


Date: Wed, 3 Mar 2004 16:39:58 -0600


...I figured it out. I needed to remove the "ref" specification on the
array and add a SizeParamIndex=1 to the MarshalAs declaration. Thanks
anyway.

"dln" <dnadon_nospm@hotmail.com> wrote in message
news:#jRn2EWAEHA.3048@tk2msftngp13.phx.gbl...
> Hey all. I'm a bit new to the language and I'm trying to figure out how
to
> have my c# application interact with native code that is exported via a
dll.
> I've run into a problem interfacing with a native dll method where one or
> more of the parameters in the native routine is an array or pointer to
block
> of contiguous memory. So, for example, the method as exported from the
> native Dll is declared as:
>
> extern "C" unsigned int __stdcall GetHandles(unsigned int*
handleArray,
> unsigned* size);
>
> Where "handleArray" is an array of unsigned integers and "size" is the
> number of elements in the array. My c# function import statement reads:
>
> [DllImport(DLL_NAME, CallingConvention=CallingConvention.StdCall)]
> public static extern UInt32
> GetHandles([MarshalAs(UnmanagedType.LPArray)] ref UInt32[] handles, ref
> UInt32 size);
>
> When I call the native method from my c# code, I can step into the native
> code and the method operates exactly as I would expect it to. However,
when
> returning from the native code, an exception is thrown which appears to be
> related to the passing of the unsigned integer array. If I remove the
"ref"
> specification for the array, the code is executed with no errors.
However,
> I see the native code filling in the array but the array returned to my c#
> code is empty (so it appears to me that the memory referenced by the c#
> array and the memory passed to the native routine are two different blocks
> of memory). I've also tried replacing the "ref" specifier with "out", but
> the same exception is thrown when returning from the native code (does
"out"
> and "ref" equate to the same thing in c#?).
>
> After going through the MSDN documentation on marshalling and calling
native
> routines from c#, I can't say that I fully understand how I can pass
arrays
> (or any data buffer for that matter) back and forth between native code
and
> c#. Can anybody tell me where I'm going wrong?
>
> Thanks.
>
>



Relevant Pages

  • Re: addEvent - The late entry :)
    ... for a native bind method. ... As the - args - array is only to be used when it has a non-zero length it would be better to only create that array in the branch that uses it. ... Having done that the - args - array creation can be moved into the branch that uses it, and so not executed at all when only one argument was used with the - bind - method. ... where fast native code creates the desired array and skips the first argument in the process. ...
    (comp.lang.javascript)
  • Calls to native dlls from C#
    ... have my c# application interact with native code that is exported via a dll. ... of contiguous memory. ... number of elements in the array. ... GetHandlesref UInt32[] handles, ref ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: do you need to pin on passing byte[] to native?
    ... > being pinned to be passed to native code. ... A byte array however is not. ... > uses a byte buffer that is never pinned. ... A object that is on the stack is not subject to being collected or moved. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: do you need to pin on passing byte[] to native?
    ... A framework object developed in c# by microsoft declares a byte array (not ... native code method that uses it just like it would a char. ... >> uses a byte buffer that is never pinned. ... > A object that is on the stack is not subject to being collected or moved. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: extract matching vales
    ... Ref no spaces in list & unique ... Name No spaces in list but has duplicates but not within the same package ref ... I used a "psuedo" error trap that effectively does the same thing but is ... This will return an array of TRUE's or FALSE's. ...
    (microsoft.public.excel.misc)