shared memory

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

    shared memory

    Hy guys!!

    I have an structures array, i need to send it memory address to a dll
    function, is it possible in c# ?

    my dll function gets a pointer to structures array

    im using

    [DllImport("hilo dll.dll")] public static extern void main(INSTANCIA
    *myInstance, int numInstanacia); <<< at this line compiler sends error,
    why?

    INSTANCIA [ ] id = new INSTANCIA[10];

    main(id , 10);

    but parameter myInstance never gets array address, compiler says it couldn't
    get array address



  • Pavel Minaev

    #2
    Re: shared memory

    On Jul 4, 9:08 pm, "Rick" <zatank...@gmai l.comwrote:
    Hy guys!!
    >
    I have an structures array, i need to send it memory address to a dll
    function, is it possible in c# ?
    >
    my dll function gets a pointer to structures array
    >
    im using
    >
    [DllImport("hilo dll.dll")] public static extern void main(INSTANCIA
    *myInstance, int numInstanacia);   <<< at this line compiler sends error,
    why?
    >
    INSTANCIA [ ] id = new INSTANCIA[10];
    >
    main(id , 10);
    >
    but parameter myInstance never gets array address, compiler says it couldn't
    get array address
    If you really want to do it that way, you need three things:

    1. Compile with /unsafe (you can't use pointers otherwise).
    2. Declare the [DllImport] method as "unsafe".
    3. Pin the array using "fixed" statement:

    fixed (INSTANCIA [ ] id = new INSTANCIA[10]) { main(id, 10); }

    Unlike C/C++, there is no implicit array->pointer conversion in C#; it
    is only allowed within the scope of initializer expression in "fixed"
    statement. Read the MSDN regarding the latter on a more detailed
    explanation.

    On the other hand, a simpler way to do the same is to declare the
    imported method as taking an array rather than a pointer:

    [DllImport("hilo dll.dll")] public static extern void main([In, Out]
    INSTANCIA[] myInstance, int numInstanacia);

    Note the [In, Out] thingy - it indicates that the function will both
    read from and write into the array. If this is not the case, you will
    need to drop In or Out as needed.

    MSDN articles with more info and examples:


    Marshal different array types, like integers by value or reference, 2-dimensional integers by value, strings by value, and structures with integers or strings.

    Comment

    • Pavel Minaev

      #3
      Re: shared memory

      On Jul 4, 9:52 pm, Pavel Minaev <int...@gmail.c omwrote:
      fixed (INSTANCIA [ ] id = new INSTANCIA[10]) { main(id, 10); }
      Sorry, this should read:

      fixed (INSTANCIA* id = new INSTANCIA[10]) { main(id, 10); }

      Comment

      • Rick

        #4
        Re: shared memory


        "Pavel Minaev" <int19h@gmail.c omescribió en el mensaje
        news:ee568b85-8e31-475e-b3e2-fad3f1a7112f@79 g2000hsk.google groups.com...
        On Jul 4, 9:52 pm, Pavel Minaev <int...@gmail.c omwrote:
        fixed (INSTANCIA [ ] id = new INSTANCIA[10]) { main(id, 10); }
        Sorry, this should read:

        fixed (INSTANCIA* id = new INSTANCIA[10]) { main(id, 10); }


        Thanks a lot Pavel, i'll start to read right now =)


        Comment

        Working...