Solved

Calling Delphi DLL from ExcelVB

Posted on 1997-07-18
7
286 Views
Last Modified: 2012-08-13
I have written a comms DLL in Delphi2. Which I have tested from another Delphi2 app. and it seems to work fine. I am now trying to call it from Visual Basic (For Excel 95) and it executes the DLL procedure ok but Excel crashes horribly when it returns. I have even tried the same workbook in Excel 97 and that crashes as well.The error message is: This program has performed an illegal operation and will be shutdown.Excel95 Crash Details:EXCEL caused an invalid page fault in
module MYDLL.DLL at 0137:00c242a7.
Registers:
EAX=00000000 CS=0137 EIP=00c242a7 EFLGS=00010246
EBX=00c40003 SS=013f ESP=0062eee4 EBP=00c453f8
ECX=0062f034 DS=013f ESI=00000000 FS=11c7
EDX=00000000 ES=013f EDI=00620000 GS=0000
Bytes at CS:EIP:
66 89 84 51 00 10 00 00 43 46 66 81 fe ff 07 72
Stack dump from Excel:
0062f034 0062f024 0062ef1c 00000001 00c453f8 00c2416e 0062f0ac 00c2419b 0062f024 0062f044 00446b72 0062f054 00c41ee4 00c41ef4 6f6c7055 64656461 Excel97 Crash Details:EXCEL caused an invalid page fault in
module KERNEL32.DLL at 0137:bff76630.
Registers:
EAX=8158a9b0 CS=0137 EIP=bff76630 EFLGS=00010246
EBX=005301cc SS=013f ESP=00530000 EBP=00530010
ECX=00530098 DS=013f ESI=005300dc FS=1077
EDX=bff76648 ES=013f EDI=005300c0 GS=0000
Bytes at CS:EIP:
ff 75 10 ff 75 0c ff 75 08 ff 55 18 83 c4 10 64
Stack dump from Excel:
00530098 005301cc bff76648 005301cc 005300a8 bff859d9 005300c0 005301cc 005300dc 00530098 bff76648 00530288 005302a4 0062e9a0 00000000 00000000 Heres the Visual Basic:Declare Sub MuploadData Lib "MyDll" (ByRef d, ByVal N As Long)
Public data(0 To 1, 0 To 2047) As Integer

'
' Upload from Delphi DLL
'
Sub Upload_Data()

  Call MuploadData(data(), 1)
 
End Sub
And heres the Delphi:procedure MuploadData (data: Pointer;prt: word);stdcall;Any help would be welcome.
0
Comment
Question by:Ray(2)
  • 4
  • 2
7 Comments
 
LVL 2

Expert Comment

by:vorlon
ID: 1428518
Ray(2),
Visual Basic does not handle pointer. What you must do, is assign a string long enough for the space of the variable. So, instead of calling your DLL function like this:

Sub Upload_Data()
   Call MuploadData(data(),1
   ...
End Sub

You have to call it like this:

Sub Upload_Data()
   dim dt as string
 
   dt = String(255,0)
   Call MuploadData(dt,1)
   ...
End Sub

Here, of course I'm assuming you're dealing with strings... The String(255,0) function here assigns a string of 255 null characters (0) to dt. When you pass dt to you're function, you're actually passing it room for 255 characters.

Hope this helps.
0
 

Author Comment

by:Ray(2)
ID: 1428519
Firstly thanks for the suggestion. Perhaps I have not made my intention clear. I tried your suggestion and it did the same thing. Also I can't see how I could do anything useful with the string afterwards without typecasting it as a two-dimensional array. I am pretty sure you must be able to pass a pointer to a variable from VB. I think it is something do with ByRef. Does anyone else have any idea?
0
 
LVL 2

Expert Comment

by:vorlon
ID: 1428520
Ray(2),
Have you tried:

Declare Sub MuploadData Lib "MyDll" (ByRef d As XXXXX, ByVal N As Long)

where XXXXX is the type of the variable d? That could also be the problem.

Hope this helps.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Ray(2)
ID: 1428521
Firstly thanks for the suggestion. Perhaps I have not made my intention clear. I tried your suggestion and it did the same thing. Also I can't see how I could do anything useful with the string afterwards without typecasting it as a two-dimensional array. I am pretty sure you must be able to pass a pointer to a variable from VB. I think it is something do with ByRef. Does anyone else have any idea?
0
 

Author Comment

by:Ray(2)
ID: 1428522
Thanks again for your second answer. I have tried everything! I think this problem may be something more fundamental than the syntax of VB. The DLL runs OK. Its only when it returns to Excel VB when it crashes. Maybe its something to do with calling convention (although both should be using stdcall).
0
 
LVL 3

Accepted Solution

by:
wolfcrag earned 70 total points
ID: 1428523
To pass an entire array of numeric data, you should pass just the first element of the array by reference, since array data of numeric type is always laid out sequentially in memory.

So, your call should be:

Call MuploadData(data(0,0), 1)

I know this works for 1D arrays, and I see no reason why it shouldn't work for 2D arrays. Make sure that your declaration has ByRef d in it.
0
 

Author Comment

by:Ray(2)
ID: 1428524
Success at last! My my, isn't VB twitchy?!? Many thanks to everyone who helped. That was really starting to bug me.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now