#Byval vs. ref for DLL's
3 messages in this thread
Help,
I am using VB to call a DLL (EHHLAPI) and I have some questions
about when to use "Byval". It seems that the keyword ByVal has
different effects when used to pass numbers (Integer or Float) and
when it is used to pass a string.
When used with the numbers the ByVal keyword works as I expect.
But when used with string it seems to act in just the opposite way.
That is, without byval the 'Value' of the string is passed to the DLL,
with ByVal a pointer to the string is passed??? Is this correct.
BTW, is it possible to 'overlay' a string in memory with a structure in
VB. Or maybe pass a pointer to a structure when the DLL is expecting
a pointer to a string (Same length).
Any Idea's would be of help. Thanks, Cliff
There is 1 Reply.
Cliff –
Yes, unfortunately VB uses ByVal to mean two different things… sort
of. ByVal for numbers does mean the actual value is passed, not a pointer
to it. But ByVal for strings passes a long pointer to a null-terminated
string. The term is still appropriate, though, because in both cases the
item passed has been "dereferenced". Numeric variables have only pointers,
which yield values when deref'd. But strings have handles, which yield
pointers when deref'd.
To pass a structure in a field which is properly "ByVal X$", just
change the Declare to read "X As Any". As long as the receiving procedure
knows what it's getting, you can pass just about anything. "As Any" passes
a long pointer to a variable (exactly what a ByVal string is).
— Jim
There is 1 Reply.
Jim,
Thank-you!
That is just what I needed to know.
Cliff