Need help passing array of UDT with string to C++ dll

amrizal

New Member
Joined
Jan 20, 2009
Messages
8
Hi. I'm trying to pass an array of UDT from Excel to a C++ dll but I have no idea how. My UDT looks like this
Code:
Private Type Student
    Age As Integer
    Name As String
    StudentID As String
End Type
And my dll function declaration..
Code:
Declare Function RegisterStudent Lib "SimpleDLL.dll" _
(arr() as Student) As String
.. which will return a unique Id in string
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I don't understand. Your function appears to take an array of Student types as an argument, so what is the string it returns?
My best guess would be something like:
Code:
   Dim arrStudents(1 To 3) As Student
   Dim n As Long
   Dim strID As String
   For n = 1 To 3
      With arrStudents(n)
         .Age = n
         .Name = "name " & n
         .StudentID = n * 4
      End With
   Next n
   strID = registerstudent(arrStudents)
 
Upvote 0
Yes. The dll function would do some processing and returns a unique processing id.

I can't find a way to pass an array of UDT with variable length string. :confused:
 
Upvote 0
Thanks for the link. I'm a beginner with VBA much less with VB. How do I create the VBA function and type declaration for the dll?

I've tried..
Vba Code
Code:
Private Type TestUDT
    l As Long
    str As String
End Type
 
Declare Sub ModifyStruct Lib "UDTArray.dll" _
(arr() As TestUDT, l As Long)
But gave me a 'type mismatch : array or user-defined type expected' error when I ran the code.
Code:
Dim t(0 To 1) As TestUDT
Dim i as long
t(0).l = 1
t(0).str = "test1"
t(1).l = 2
t(1).str = "Test2"
i = UBound(t) - LBound(t) + 1
ModifyStruct t(0), i '<== type mismatch error here

... Any ideas?
 
Upvote 0
Try changing that last line to:
Code:
ModifyStruct t, i
so you are passing the array of UDTs, not a single UDT.
 
Upvote 0
Thanks rorya. I don't think I can do that since the c++ code in the dll requires me to pass a pointer of a single item.

Code:
void __stdcall ModifyStruct(struct tagTestUDT *t, long nTotalItem)

When I tried calling

Code:
ModifyStruct t, i

gave me "Run-time error 453: Can't find DLL entry point ModifyStruct ..."

I'm guessing it's my function declaration for the dll. I copied the rest of the code from here
 
Upvote 0
Have you tried setting a reference rather than using Declare? I don't know enough about C to help you, but you could try:
Code:
Declare Function RegisterStudent Lib "SimpleDLL.dll" _
(arr as Student) As String
and then pass the first member of the array t(0)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top