Interfacing C functions
VB, VBA, VBScript, JavaScript, are programming languages and scripting languages to write and automate tasks in Office and Windows operating systems. They are easy to implement and good for rapid development. These have API support for doing all sorts of works provided by Windows and Office. Some APIs that are not available in the built-in libraries or some APIs which are custom needed by a user cannot be fulfilled. These types of situations call for a custom-made library file. C programming language does not have any restrictions to access low-layer system calls and utility functions. Thus user requires to implement the required function in C programming and that function can be called from Visual Besic.
VB calls DLL
Visual Basic compiler /linker generates the same type of output binary as of C compiler. Both compilers push arguments from right to left and call the function. Low level machine instructions are the same. So applications written in Visual Basic can access C functions. Functions have to reside inside a dynamic link library(dll). Program loader loads dynamic link library either from current path or from absolute location or searchs all locations given in PATH variable.
VB vs C function prototypes
Apart from dll function address, Visual Basic also need function prototype so that it can push input variables in stack before calling the function. Calling C Functions should be of type stdcall. Stdcall cleans its own stack or callee cleans the stack. So all cleanups are done in Dll side. All Windows DLL works like this. We have below example showing how VB can call a function using call by value and call by reference.
C function prototypes
VB function prototypes
C DLL project
We are creating a Win32 Dynamic Link Library VC++ project. We are selecting this project as Simple Dynamic Link Library Project. VC++ editor will create the necessary project files including header and C/C++ implementation file. DllMain() function will be implemented by default by the editor. We need to add CallCByVal, CallCByRef prototypes and the body of the functions.
VB Project
We are creating a simple VB application project to call the C function. We have added a button in the main form and added the calling code inside the click event of the button. This is however not limited to VB. VBA, VB script and Javascript can call the function in the same way as VB does.
About our authors: Team EQA
You have viewed 1 page out of 27. Your DLL learning is 0.00% complete. Login to check your learning progress.
Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions
Compilers & Editors
Download Visual Studio Download XCode Download Visual Studio Code Android studio install sdk Eclipse installer Best C compilers IDEs
Development system setup
Windows media creation tool MSDN subscription Ubuntu virtualbox
New updated posts
Why learn C? Calculate weighted average