Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Problems with callback from DLL invoked from VBA

I have to invoke a C++ function from VBA/Excel that does a callback. I've
isolated the problem I'm having in the code below.

In the C++ (complied & linked to a DLL) I have:

long __stdcall tryCB3(long n, long (*cbFunc)(long m))
{
long r;
r = cbFunc(n); // call back
return r;
}

In the VBA I have:

Private Declare Function tryCB3 Lib "SparkExUtilsDll.dll" _
(ByVal n As Long, ByVal theFunct As Any) As Long

Public Function theCBFunc3(ByVal m As Long) As Long
Debug.Print "In theCBFunc3"
theCBFunc3 = m * m
End Function

Public Function tryCBDrv3()
Dim n As Long
Dim q As Long
n = 2
q = tryCB3(2, AddressOf theCBFunc3)
Debug.Print "n= " & CStr(n)
Debug.Print "q= " & CStr(q)

End Function

When I step through tryCB3Drv it calls tryCB3, and then steps through
theCBFunc3, all working as expected, but then crashes with a C++
error dialog saying the ESP was not preserved through a function call etc.

Note that the it works without crashing if the callback function
has no arguments, so it must be some problem with the way VBA is setting
up the argument to theCBFunc3, vs. the way it's declared in C++.

Any ideas?

TIA

Ed


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Problems with callback from DLL invoked from VBA

Change your C++ function to

long __stdcall tryCB3(long n,long(__stdcall *cbFunc)(long m))


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Jag Man" wrote in message
...
I have to invoke a C++ function from VBA/Excel that does a

callback. I've
isolated the problem I'm having in the code below.

In the C++ (complied & linked to a DLL) I have:

long __stdcall tryCB3(long n, long (*cbFunc)(long m))
{
long r;
r = cbFunc(n); // call back
return r;
}

In the VBA I have:

Private Declare Function tryCB3 Lib "SparkExUtilsDll.dll" _
(ByVal n As Long, ByVal theFunct As Any) As Long

Public Function theCBFunc3(ByVal m As Long) As Long
Debug.Print "In theCBFunc3"
theCBFunc3 = m * m
End Function

Public Function tryCBDrv3()
Dim n As Long
Dim q As Long
n = 2
q = tryCB3(2, AddressOf theCBFunc3)
Debug.Print "n= " & CStr(n)
Debug.Print "q= " & CStr(q)

End Function

When I step through tryCB3Drv it calls tryCB3, and then steps

through
theCBFunc3, all working as expected, but then crashes with a

C++
error dialog saying the ESP was not preserved through a

function call etc.

Note that the it works without crashing if the callback

function
has no arguments, so it must be some problem with the way VBA

is setting
up the argument to theCBFunc3, vs. the way it's declared in

C++.

Any ideas?

TIA

Ed




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Problems with callback from DLL invoked from VBA

Yes, that works. Thanks, Chip.

Now, moving closer to what I really need to do, I need to add an
array argument to the callback function:

long __stdcall tryCB3(long n,long(__stdcall *cbFunc)(long m, long *))
{
....
long* x= new long[n];
....
r = cbFunc(n, x);
...

delete [] x;
return r;
}

And in the VB:

Public Function theCBFunc4(ByVal m As Long, ByRef x() As Long) As Long
Dim i As Integer
Dim s As Long
s = 0
For i = 0 To m - 1
s = s + x(i)
Next i
theCBFunc4 = s
End Function

But, it doesn't work. It crashes Excel when I call the tryCB3 function from
VB.

Any ideas?

TIA

Ed



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Problems with callback from DLL invoked from VBA

I'm not sure what the problem is. I suspect that it is because
VB/VBA uses SAFEARRAY structures rather than C-type arrays. You
might have more luck posting this in one of the Visual C
newsgroups.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Jag Man" wrote in message
...
Yes, that works. Thanks, Chip.

Now, moving closer to what I really need to do, I need to add

an
array argument to the callback function:

long __stdcall tryCB3(long n,long(__stdcall *cbFunc)(long m,

long *))
{
...
long* x= new long[n];
...
r = cbFunc(n, x);
..

delete [] x;
return r;
}

And in the VB:

Public Function theCBFunc4(ByVal m As Long, ByRef x() As Long)

As Long
Dim i As Integer
Dim s As Long
s = 0
For i = 0 To m - 1
s = s + x(i)
Next i
theCBFunc4 = s
End Function

But, it doesn't work. It crashes Excel when I call the tryCB3

function from
VB.

Any ideas?

TIA

Ed





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Problems with callback from DLL invoked from VBA

Thanks, Chip. When I get to the bottom of it I'll post the solution.

Ed


"Chip Pearson" wrote in message
...
I'm not sure what the problem is. I suspect that it is because
VB/VBA uses SAFEARRAY structures rather than C-type arrays. You
might have more luck posting this in one of the Visual C
newsgroups.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
More than one copy of EXCEL 2003 invoked zhj23 Excel Discussion (Misc queries) 1 May 4th 06 02:36 PM
Customized Menu Macro with Argument invoked twice Alan Excel Programming 4 February 6th 04 02:54 PM
Using Excel to handle Com callback procedures Aaron Graham Excel Programming 5 February 5th 04 04:00 PM
RTD Callback and CIS with firewall Marcosia Excel Programming 0 November 26th 03 10:31 AM
The object invoked has disconnected from its clinets Don[_7_] Excel Programming 2 August 20th 03 02:30 AM


All times are GMT +1. The time now is 01:29 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"