Anyway here is my dll import sample code in VB:
Private Declare Function DllMain Lib "test32" _
(ByVal num As Long) As Long
Private Sub Command1_Click()
Dim retCode As Long
retCode = DllMain(1)
MsgBox "This is from DLLMAIN: " +
Str(retCode), vbInformation
Exit Sub
End If
End Sub
And here is my (test32.dll)dll:
#include
#include
#define EXP __declspec(dllexport)
int pascal DllMain( int num )
{
num = num +1;
return num;
}
I have tried looking in sample code out there for
recommedded ways to do this but I am geting nowhere. Any
ideas?Thanks.
A few things to note:
* VB can only access DLL exports declared as __stdcall. The WINAPI/Pascal
convention is, to quote from the Book of Armaments, Right Out.
* Associated with this is __stdcall name mangling. The C compiler will
change the name you give the function by adding a _ at the front and @nnn at
the end, where nnn is the number of bytes occupied by the parameter list.
You can specify this with the Alias clause of the declaration
* VB Longs map directly to C long variables (on a 32-bit machine). VB
(pre-.NET) Integers at 16-bit would be a short in C on 32-bit machines. VB
doesn't support unsigned types so don't use them as parameters or returns
from C code otherwise you get weird results.
* char * or LPSTR parameters in C are ByVal ... As String in VB. char ** is
ByRef As String. Otherwise, long and long * are ByVal and ByRef as you
would expect.
* If your function returns a value (i.e. is non-void) make sure it's
Declared as a Function with the right return type; if it's void, Declare
Sub. Otherwise you app can crash with a Bad DLL Calling Convention.
Your code would work if it was:
-- test32.c
/* DllMain is a bad name for a function; I think Windows calls this itself
with some parameters when the DLL is loaded. */
long __declspec(dllexport) __stdcall TestFunction(long num)
{
return num+1;
}
-- test.bas
Option Explicit
Private Declare Function TestFunction Lib "test32.dll" Alias
"_TestFunction@4" (ByVal Num as Long) As Long
Public Sub Main()
Dim Num As Long, RetVal As Long
Num = 42
RetVal = TestFunction(Num)
Debug.Print "TestFunction returned " & CStr(Num)
End Sub
--
Regards,
Ben A L Jemmett.
(http://web.ukonline.co.uk/ben.jemmett/, http://www.deltasoft.com/)