Hi, I'm tearing my hair out trying to solve this one, any insights will be much appreciated:
I have a C++ exe which acquires data from some hardware in the main thread and processes it in another thread (thread 2).
I use a c++ dll to supply some data processing functions which are called from thread 2.
I have a requirement to make another set of data processing functions in VB6. I have thus created a VB6 dll, using the add-in vbAdvance to create a standard dll.
When I call functions from within this VB6 dll from the main thread, everything works exactly as expected.
When I call functions from this VB6 dll in thread 2, I get an access violation.
I've traced the error to the CopyMemory command, it would seem that if this is used within the call from the main thread, it's fine but in a call from the process thread, it causes an exception. Why should this be so? As far as I understand, threads share the same address space.
Here is the code from my VB dll
Public Sub UserFunInterface(ByVal in1ptr As Long, ByVal out1ptr As Long, ByRef nsamples As Long)
Dim myarray1() As Single
Dim myarray2() As Single
Dim i As Integer
ReDim myarray1(0 To nsamples - 1) As Single
ReDim myarray2(0 To nsamples - 1) As Single
With tsa1din(0) ' defined as safearray1d in a global definitions module
.cDims = 1
.cbElements = 4
.cElements = nsamples
.pvData = in1ptr
End With
With tsa1dout
.cDims = 1
.cbElements = 4
.cElements = nsamples
.pvData = out1ptr
End With
CopyMemory ByVal VarPtrArray(myarray1), VarPtr(tsa1din(0)), 4
CopyMemory ByVal VarPtrArray(myarray2), VarPtr(tsa1dout), 4
For i = 0 To nsamples - 1
myarray2(i) = myarray1(i) * 2
Next i
ZeroMemory ByVal VarPtrArray(myarray1), 4
ZeroMemory ByVal VarPtrArray(myarray2), 4
End Sub
UPDATE:-
Thanks for all your comments, I have actually found a work around that does what I need it to do. I've put all the pointer manipulations for the arrays into a seperate function in the VB dll, I call this function from the main thread of the C++ app during the setup routines. The processing thread then calls another routine in the dll which contains only the calculations to be performed on the array data (and no CopyMemory commands). This appears to work.