Visual Basic compiler /linker generates same type of output binary as of C compiler. Both compilers push arguments from right to left and calls the function. Low level machine instructions are same. So applications written in Visual Basic can access C functions. Functions has to reside in side 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.
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 Windows DLL (VBDLL.dll) Source code */ #include#include BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } int extern "C" __stdcall __declspec(dllexport) CallCByVal ( char c, short s, long l, float f, double d, LPCSTR str) { char str_m [100]; sprintf ( str_m, "Char %c, Short 0x%X, Long %ld, \nFloat %f, Double %lf, String \"%s\"", c, (int)s, l, f, d, str); MessageBoxA(NULL, (const char *)str_m, "Message from C", MB_OK); return 0; } int extern "C" __stdcall __declspec(dllexport) CallCByRef ( char *c, short *s, long *l, float *f, double *d, LPSTR *str) { char str_m[100]; sprintf ( str_m, "Char %c, Short 0x%X, Long %ld, \nFloat %f, Double %lf, String \"%s\"", *c, (int)*s, *l, *f, *d, *str); MessageBoxA(NULL, (const char *)str_m, "Values from VB", MB_OK); *c = *c + 1; *s = *s + 1; *l = *l + 1; *f = *f + 1; *d = *d + 1; strcpy(*str,"String from C"); return 0; }
Public Declare Function CallCByVal Lib "VBDLL.dll" ( ByVal c As Byte, _ ByVal s As Integer, _ ByVal l As Long, _ ByVal f As Single, _ ByVal d As Double, _ ByVal str As String) As Integer Dim c As Byte Dim i As Integer Dim l As Long Dim f As Single Dim d As Double Dim s As String Private Sub Command1_Click() c = CByte(65) i = CInt(12) l = CLng(200) f = CSng(1.2) d = CDbl(4.8) s = CStr("VB String") Label1.Caption = "Char " + CStr(Chr(c)) + _ ", Short " + CStr(i) + _ ", Long " + CStr(l) + vbCrLf Label1.Caption = Label1.Caption + _ "Float " + CStr(f) + _ ", Double " + CStr(d) + _ ", String " + s r = CallCByVal(c, i, l, f, f, s) Label2.Caption = "Char " + CStr(Chr(c)) + _ ", Short " + CStr(i) + _ ", Long " + CStr(l) + vbCrLf Label2.Caption = Label2.Caption + _ "Float " + CStr(f) + _ ", Double " + CStr(d) + _ ", String " + s End Sub
Public Declare Function CallCByRef Lib "VBDLL.dll" ( ByRef c As Byte, _ ByRef s As Integer, _ ByRef l As Long, _ ByRef f As Single, _ ByRef d As Double, _ ByRef str As String) As Integer Dim c As Byte Dim i As Integer Dim l As Long Dim f As Single Dim d As Double Dim s As String Private Sub Command2_Click() c = CByte(65) i = CInt(12) l = CLng(200) f = CSng(1.2) d = CDbl(4.8) s = CStr("String from VB") Label1.Caption = "Char " + CStr(Chr(c)) + _ ", Short " + CStr(i) + _ ", Long " + CStr(l) + vbCrLf Label1.Caption = Label1.Caption + "Float " + CStr(f) + _ ", Double " + CStr(d) + _ ", String " + s r = CallCByRef(c, i, l, f, d, s) Label2.Caption = "Char " + CStr(Chr(c)) + _ ", Short " + CStr(i) + _ ", Long " + CStr(l) + vbCrLf Label2.Caption = Label2.Caption + _ "Float " + CStr(f) + _ ", Double " + CStr(d) + _ ", String " + s End Sub
About our authors: Team EQA
You have viewed 1 page out of 248. Your C 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? Draw on printer Free alt softwares