Passing double pointer to C DLL from Visual Basic .NET RRS feed

  • Question

  • I have created a C function with a double pointer as an argument. I built a DLL file and want to use it in my Visual Basic project. I am not sure how to deal with the double pointer. Is my code wrong ?

    Here is my sample code.

    My C file (The function updates out values, it is an 101 elements double array.):

    void myFunction(double* out)

    My header file :

    extern "C" __declspec(dllexport) void myFunction(double*);

    My VB file(DLL Import) :

    <DllImport("myDLL.dll", EntryPoint:="myFunction", CallingConvention:=CallingConvention.Cdecl)>    
    Private Sub myFunction(ByVal out As IntPtr)    
    End Sub

    My VB file(DLL call):

    Dim out As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(GetType(Double)) * 101)        
    myFunction(out)        ' Run my DLL function
    Dim res As Double = Marshal.ReadIntPtr(out, 100) '101th element

    • Edited by mbd124312 Tuesday, July 25, 2017 6:27 AM code correction
    Tuesday, July 25, 2017 6:24 AM

All replies

  • The last line does not seem correct.

    Using Marshall.Copy(), I could solve my problem.

    Tuesday, July 25, 2017 8:08 AM
  • Hi mdb1234312,

    Gold to hear this issue has been solved by yourself. Thanks for your sharing. Please mark your last post as answer, it is very beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, August 1, 2017 7:16 AM