Solved

Calling Delphi DLL from ExcelVB

Posted on 1997-07-18
7
304 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA error replacing data 6 40
Run code from text file in vb 1 71
VBA: Select SQL query based on a config Sheet v2 11 49
Powerpoint 2013: Change cell reference in excel link 3 81
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

856 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