DLLimport in grasshopper using VB.net (HRESULT: 0x8007000B)

Hi~…
(There are many C# topics, but I couldn’t find how to import DLL in GH, using VB.net.)
I made a vb.net module & c++ dll(mfc). it works fine without error.
but when I use the code in Gh-component. it does not work.

my code is :slight_smile:

Friend Class NativeMethods
<DllImport(“C:\Users\FLOWER\AppData\Roaming\Grasshopper\Libraries\myDLL.dll”, CallingConvention:=CallingConvention.Cdecl)>
Friend Shared Function TestArrayOfInts(
<MarshalAs(UnmanagedType.LPArray), [In], Out> ByVal myArray() As Integer, ByVal size As Integer) _
As Integer
End Function

' Declares managed prototype for an array of integers by reference.
' The array size can change, but the array is not copied back 
' automatically because the marshaler does not know the resulting size.
' The copy must be performed manually.
<DllImport("C:\Users\FLOWER\AppData\Roaming\Grasshopper\Libraries\myDLL.dll", CallingConvention:=CallingConvention.Cdecl)>
Friend Shared Function TestRefArrayOfInts(
    ByRef myArray As IntPtr, ByRef size As Integer) As Integer
End Function

End Class

<SecurityPermission(SecurityAction.Demand, UnmanagedCode:=True)>
Protected Overrides Sub SolveInstance(DA As IGH_DataAccess)

    ' If (Not DA.GetData(0, ct)) Then Return

    ''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim array1(9) As Integer

    Console.WriteLine("Integer array passed ByVal before call:")
    Dim i As Integer
    For i = 0 To array1.Length - 1
        array1(i) = i
        Console.Write(" " & array1(i))
    Next i

    Dim sum1 As Integer = NativeMethods.TestArrayOfInts(array1, array1.Length)
    Console.WriteLine(ControlChars.CrLf & "Sum of elements:" & sum1)
    Console.WriteLine(ControlChars.CrLf & "Integer array passed ByVal after call:")
    For Each i In array1
        Console.Write(" " & i)
    Next i

    ' array ByRef
    Dim array2(9) As Integer
    Dim arraySize As Integer = array2.Length
    Console.WriteLine(ControlChars.CrLf & ControlChars.CrLf &
        "Integer array passed ByRef before call:")
    For i = 0 To array2.Length - 1
        array2(i) = i
        Console.Write(" " & array2(i))
    Next i
    Dim buffer As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(
        arraySize) * array2.Length)
    Marshal.Copy(array2, 0, buffer, array2.Length)
    Dim sum2 As Integer = NativeMethods.TestRefArrayOfInts(buffer,
        arraySize)
    Console.WriteLine(ControlChars.CrLf & "Sum of elements:" & sum2)

    If arraySize > 0 Then
        Dim arrayRes(arraySize - 1) As Integer
        Marshal.Copy(buffer, arrayRes, 0, arraySize)
        Marshal.FreeCoTaskMem(buffer)

        Console.WriteLine(ControlChars.CrLf & "Integer array passed ByRef after call:")
        For Each i In arrayRes
            Console.Write(" " & i)
        Next i
    Else
        Console.WriteLine(ControlChars.CrLf & "Array after call is empty")
    End If


    '''''''''''''''''''''''''''''''''''''''''''''

    ' Finally assign the spiral to the output parameter.

End Sub

I think it should work, but error happened. (HRESULT: 0x8007000B)
Please, help me to fix it.
Thank you.

vbModule1 and cpp and def and Grasshoppervb.zip (4.3 KB)

Are both dlls in the same folder of grasshopper lib folder? Both .dlls are built for 64bit? And can you run both .dlls from console app in the same folder?

Try to build both grasshopper and c++ .dll not any cpu, not 86x but 64 bit in both .dlls.

Thank you Very Much. Mr. Petras1.
I fixed settings.
It works fine!

Merry Christmas !! :smiling_face_with_three_hearts:

Solved… Thank you