Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VB DLL Import/Export Help

0 views
Skip to first unread message

Adam

unread,
Nov 5, 2001, 9:11:46 AM11/5/01
to
First of all I am a newbie. Originally I posted this
question to the Novell's devforums. They suggested that
you guys might be able to help out with this one. To
explain what I am tring to do is I have a dll which I
would like to access in my VB app. Basically, I am trying
to get VB6 to import a few libraries from this sample
dll. I am looking to iron a few of these syntax
problems. Currently I am getting an error message
indicating that test32 cannot be found. This is the same
syntax as is in some of the sample code.

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.


Adam

unread,
Nov 5, 2001, 9:11:31 AM11/5/01
to

Ben A L Jemmett

unread,
Nov 5, 2001, 10:29:04 AM11/5/01
to
> Originally I posted this question to the Novell's devforums.
Eep! I knew there was a server I meant to start reading again after I'd
moved to this new machine!

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/)


0 new messages