Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Calling Delphi DLL from ExcelVB

Posted on 1997-07-18
7
Medium Priority
?
329 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)
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 280 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Suggested Courses

610 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