EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of DLL Questions #Next question

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;
}

C exported fucntions

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

VB Call By Value

VB MessageBox

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

VB Call By Reference

VB MessageBox

You have viewed 1 page out of 27. Your DLL learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

# DLL Frequently Asked Questions
# Frequently Asked Questions - DLL-files.com
# Dependency Walker Frequently Asked Questions (FAQ)
# Top 10 DLL Questions !
# DLL COM DCOM Interview Questions and Answers

* #1 webmaster Wed 24 Dec/2014 23:54:09(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.