Advertisement

C DLL and VB's ByRef vs. ByVal

Started by December 07, 2000 04:27 PM
1 comment, last by Mongo 24 years, 1 month ago
Sorry if this is off-topic, but I''ve always been curious... I quote from an MSDN knowledge base entry titled ''HOWTO: Write C DLLs and Call Them from Visual Basic'': "... (When passing by reference, Visual Basic supplies a 32-bit far address.)" OK... So we''re talking about a pointer, here, right? Why, then, is it necessary for VB to pass parameters by Value to a C function that accepts a pointer? A C declaration from the same article: void __export CALLBACK GetDiskInfo (char *cDrive, char*szVolumeName, unsigned long *ulFreeSpace); And the VB Declaration: Declare Sub getdiskinfo Lib "c:\somepath\diskinfo.dll" _ (ByVal mydrive As String, ByVal myvolume As String, _ free As Long) Unless I''ve been doing things wrong all these years, szVolumeName is a pointer in the C definition, yet we''re passing it by value when calling from VB. What''s the deal? Thanks in advance
The deal is that VB internally represents a String as a char *, so when passing the String by value, you are passing a char *. If you tried passing it by reference you''d pass a char **.
Advertisement
Yeah, if you pass strings ByRef you pass a pointer to a UNICODE string, i.e. 2 bytes per character. ByVal on the other hand passes a reference to a char array. This means it''s readable by the C++ library.

- JQ
"Um, hi. Aaaaaaarrrgggghhhh" -me
~phil

This topic is closed to new replies.

Advertisement