VB to DLL
Q: How Do I pass the following struct from VB to a Delphi created DLL **by reference** would be appreciated (what should the parameters on the Delphi side look like?). I chose this structure to sort of represent most scenarios (if I can successfully pass this one, anything should be OK)
Type MyType
Value1 As Double
Value2 As Integer
Value3 As String * 20
Value4 As String
End Type
A: You will have some trouble with Value4. Passing VB strings to/from DLLs takes special handling. The easiest way to do it is to pass a pre-declared VB string to the DLL and have the DLL return the length and the resulting string back.
The others are easy.
Value1 As Double -> Value1 : Longint;
Value2 AS Integer -> Value2 : integer;
Value3 As String * 20 -> Value3 : array[0..19] of char;
User defined structures in Pascal are called records.
type
myStructure = record
Value1 : longint;
Value2 : integer;
Value3 : array [0..19] of char;
end;
I think you could define Value4 as a PChar in Pascal, but when you got it back in VB, you would have to search it for the ASCII 0 end byte and change the length of your string to the number of characters before the zero.
If I remeber correctly, from my evaluation 'beta' copy with source code, there were some functions in the VCL that handle VB strings. Can't say if they are in the shipping version because I havn't recieved the VCL source yet. A quick search of the online docs don't reveal them.
Here's some general type conversions from VB to Pascal (in table form; I hope this formats correctly on CIS.):
VB Declare As VB Call With Translates to Pascal Type
------------- ------------ -------------------------
By Val S As String Any String or Variant PChar
I As Integer Any Integer ^Integer (pointer to integer)
L As Long Any Long ^Longint (pointer to longint)
S As Rect Any Variable of same type ^TRect (pointer to TRect)
By Val As Integer Any Integer BOOL (word boolean)
" " " Word
" " " Integer
" " " hWnd
" " " hDC
" " " ...and so on; all word types.
By Val As Long Any Long Longint
I As Integer the first element of I(0) ^array of integer
As Any Any Variable (By Val when
String) Pointer (PChar when string)
As Any By Val 0& nil
Q: This would probably work, but I'd still really like to know how to return a string (even a null-terminated one) from a DLL function to VB.
A: The problem with returning PChars from DLLs is that the DLL has to be responsible for cleaning up the memory, but it doesn't know when the caller finishes. Most people use the following style, which is NOT bullet proof, but works in most instances:
Var ReturnBuffer :Array [0..255] of Char; { Must be outside function! }
function Dir_Get(ACaption, AInitDir, DirText: PChar ) : PChar;
Begin
...
Result := StrPCopy(ReturnBuffer, 'Result Text');
End;
The only time you have a problem is when someone calls the function while someone else is still using the ReturnBuffer. (Just about guaranteed not to happen, except maybe under Windows 95, but could under 3.1 if VB doesn't copy the string).