Passing arguments from VBA to DLL
Hi!
NickHK wrote:
groups.com...
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?
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.
The C function accepts a pointer to type char; you can interpret this
as a pointer to the string in memory, if you like to.
[...]
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
Very fine example. I will reuse it for further examination regarding
this problem.
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.
For now, this will be the way I'll do it.
Regards,
Matthias
|