Re: Howto: Passing 'C' Array From Unmanaged to Managed by Reference

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



>See function declarations below:
>
>.NET DECLARATION:
>public bool GetInt([MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)]
>int[] NumArray, int size)
> NumArray[0] = 45; // This modification is not reflected in the unmanaged
>side!
>
>VC 6.0 C++:
> int NumArray[100];
> NumArray[0] = 78;
> m_SmartPointer->GetInt(&NumArray, 100);
> // NumArray[0] still is 78, thought it would of been 45?
>
>The above works for passing a 'C' array however altering the contents of the
>array in the managed code does not
>get reflected in the ummanaged side.


Did you try adding the Out attribute to the parameter?

public bool GetInt([In, Out, MarshalAs(UnmanagedType.LPArray,
SizeParamIndex=1)] int[] NumArray, int size)




Mattias

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



Relevant Pages

  • (patch for Bash) regex case statement
    ... Following up on my previous patch for regex conditional tests, ... /* Return an array of strings; ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: Strategy or Iterator?
    ... It would be possible to write a class that returns the variations ... GNU General Public License for more details. ... protected CombinatoricOperator(Telements, int r) { ... An integer array backing up the original one to keep track of the ...
    (comp.lang.java.programmer)
  • (patch for Bash) regex conditional tests
    ... 'regex' are returned in array variable SUBMATCH. ... Skipping of positional parameters, array elements, string ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: Warning on assigning a function-returning-a-pointer-to-arrays
    ... This declares pfunc as a function taking no arguments and returning ... int x, y; ... Presumably pfuncwill return a pointer to a single int, ... or the first of a sequence of "array 5 of int"s. ...
    (comp.lang.c)
  • Re: Memory Allocation Problem, please help
    ... typedef struct word_tag{ ... array is not an array. ... static int total_word_count; ... static int word_index(const char *word); ...
    (comp.lang.c)