View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
NickHK[_3_] NickHK[_3_] is offline
external usenet poster
 
Posts: 415
Default Passing arguments from VBA to DLL

I'm no C expert at all, but..
I would assume it has to do with how VBA sends the string to your DLL. In
VBA, internally, it is held as Unicode in a BSTR, which works fine when
passed ByVal to your function. IIRC, you are actually passing a pointer to a
pointer of the string.
However, going directly from the worksheet, your are not actually passing
the same thing (possibly not even in a BSTR) and hence only the first
character is resolved instaed of the whole string.

As a trivial example, using this Windows calls
Public Declare Function lstrlenA Lib "kernel32" (ByVal lpString As String)
As Long
in a VBA function gives lstrlenA("qwerty") equal to 6. Correct.

From the worksheet, lstrlenA("qwerty")=1. Wrong

So I would guess you have to give VBA chance to create a variable so it can
pass the string correctly. i.e. create a dummy VBA function to call each of
your DLL functions that invlove strings.

NickHK


groups.com...
Hi!

NickHK schrieb:
wrote in message
s.com...
sjoo wrote:
" wrote:
[...]

I noticed that foo didn't work internally as I expected and added
writing *t to a file on each call of foo as a debugging measure.
According to this output, only the first character of String t
seems
to be passed to foo when called directly from Excel (putting
=foo("xyz") in a cell).

you can't directly call the dll function on the worksheet.

In fact, I can, but lacking the correct result. Any guesses for the
technical reason?

if you like to do, you have to make XLL library that is a kind of
dll.
XLL is a special programming model for EXCEL.

please refer this

http://support.microsoft.com/default...NoWebContent=1

Sounds interesting, but I don't have the mentioned "Microsoft Excel 97
Developer's Kit" available. Plus, Excel 97 is pretty old. What do I
have to do when developing for more recent versions?


does this help ?
http://support.microsoft.com/kb/106553/EN-US/


No, sorry. The example DLL and VB code looks like mine, and I have no
problems when calling the DLL from VBA functions or macros. The problem
arises when calling it from wihtin Excel.

Regards,
Matthias