Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Calling a windows DLL function

Posted on 2000-05-04
Medium Priority
Last Modified: 2010-05-02
I have been sent a DLL and a snippet of C code that calls into its function.

I try to port this into Visual Basic 6.0 without much success.

Sample source will be appreciated

Here is the snippet for the C code if it is useful
if (LoadLibrary("ETIWRD32.DLL"))!= NULL)
  lpFunc = (lpFunction) (GetProcAddress(hLibrary, (LPSTR) "ConvertFile"));
  if (lpFunc != NULL)
    iRes = (*lpFunc)((LPSTR) "aaa.doc", (LPSTR) "aaa.txt", (FARPROC) NULL);
Question by:crooz21

Expert Comment

ID: 2779728
You need to declare the API first.

Declare Function GetProcAddress Lib "kernel32" Alias "GetProcAddress" (ByVal hModule As Long, ByVal lpProcName As String) As Long

The API takes the handle to the module and the name of the process. (function)

To get the handle to the module, either use LoadLibrady or getModuleHandle

Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long


Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long

Below is an example using the functions:

The version of DCOM95 or DCOM98 installed on a Windows 9x system may be determined by checking the registry using Visual Basic. If DCOM is installed on the system, the default value of the registry key:


will contain the version of DCOM installed on the system. The version number in the registry is stored in the format "a,b,c,d", where a, b, c, and d are numeric values. These values form the version number of the DCOM install:

InstalledVersion DCOM Version
"4,71,0,3328" DCOM95 and DCOM98 1.3
"4,71,0,2618" DCOM95 1.2 Web Release
"4,71,0,2612" DCOM 1.2 DCOM98.exe shipped with VS6.0
"4,71,0,1719" DCOM Win98 Gold
"4,71,0,1718" DCOM95 1.1
"4,71,0,1120" DCOM 1.x

The following steps outline this process from Visual Basic:

Start a new Visual Basic Standard EXE project. Form1 is created by default.

Add a CommandButton (Command1) to Form1.

Add the following code to the General Declarations section of Form1:

Option Explicit

Private Const ERROR_SUCCESS = 0&

Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_CLASSES_ROOT = &H80000000

Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_NOTIFY = &H10
Private Const KEY_CREATE_LINK = &H20
Private Const SYNCHRONIZE = &H100000

Private Const KEY_ALL_ACCESS = _
     KEY_NOTIFY Or _
    ) _
    And (Not SYNCHRONIZE) _

Private Declare Function RegOpenKeyEx _
   Lib "advapi32.dll" Alias "RegOpenKeyExA" _
   (ByVal hKey As Long, _
    ByVal lpSubKey As String, _
    ByVal ulOptions As Long, _
    ByVal samDesired As Long, _
    phkResult As Long) _
   As Long

Private Declare Function RegCloseKey Lib _
   "advapi32.dll" _
   (ByVal hKey As Long) _
   As Long

Private Declare Function RegQueryValueEx Lib _
   "advapi32.dll" Alias "RegQueryValueExA" _
   (ByVal hKey As Long, _
    ByVal lpValueName As String, _
    ByVal lpReserved As Long, _
    lpType As Long, _
    ByVal lpData As String, _
    lpcbData As Long) _
   As Long
' Note that if you declare the lpData
' parameter as String, you must pass it
' By Value as is done here.

Private Declare Function GetProcAddress _
   Lib "kernel32" _
   (ByVal hModule As Long, _
    ByVal lpProcName As String) _
   As Long

Private Declare Function GetModuleHandle _
   Lib "kernel32" Alias "GetModuleHandleA" _
   (ByVal lpModuleName As String) _
   As Long
Private Function DCOMEnabled() As Boolean
' We need to check two things
' 1- OLE32 supports free threading
' 2- DCOM is enabled in the registry
' See Inside COM, Rogerson, pp 276-277

   DCOMEnabled = False
' First, check to see if OLE32 supports free threading.
' You need to check for the CoInitializeEx function's
' presence in OLE32.
' 1- Get a handle to the OLE32 module
' 2- Try to get a ProcAddress for CoInitializeEx

   Dim OLE32ModuleHandle As Long
   Dim CoInitializeExProcAddress As Long
   OLE32ModuleHandle = GetModuleHandle("OLE32")
   Debug.Assert (Not OLE32ModuleHandle = 0)
   CoInitializeExProcAddress = GetProcAddress( _
           OLE32ModuleHandle, _
   Debug.Print "CoInitializeExProcAddress = " _
               & CoInitializeExProcAddress
   If CoInitializeExProcAddress = 0 Then
      DCOMEnabled = False
      Exit Function
   End If
' Now check the registry to see if DCOM is enabled.
   Dim lResult As Long
   Dim hKey As Long

   lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _
                   "SOFTWARE\Microsoft\Ole", _
                   0, _
                   KEY_ALL_ACCESS, _
   Debug.Assert (lResult = ERROR_SUCCESS)

   Dim rgch As String
   rgch = String(2, 0)
   Dim cbrgch As Long
   cbrgch = Len(rgch)
   lResult = RegQueryValueEx(hKey, "EnableDCOM", 0, 0&, rgch, cbrgch)

   Debug.Assert (lResult = ERROR_SUCCESS)

   Debug.Print "Mid$(rgch, 1) is " & Mid$(rgch, 1)

   If (Mid$(rgch, 1, 1) = "Y" Or Mid$(rgch, 1, 1) = "y") Then
      ' DCOM is Enabled
      DCOMEnabled = True
      DCOMEnabled = False
   End If
   lResult = RegCloseKey(hKey)
End Function

Public Function GetDCOMVersion() As String
    ' Check the registry key "InstalledVersion" to see which
    ' version of DCOM is installed.

   Dim hKey As Long
   Dim lResult As Long

' First confirm that DCOM is installed
   If Not DCOMEnabled() Then
       MsgBox "Can't check version.  DCOM is not installed."
       Exit Function
   End If

' open the the proper registry key
   lResult = RegOpenKeyEx(HKEY_CLASSES_ROOT, _
         "CLSID\{bdc67890-4fc0-11d0-a805-00aa006d2ea4}\InstalledVersion", _
         0, _
         KEY_ALL_ACCESS, _

   If Not lResult = ERROR_SUCCESS Then
       MsgBox "Could not open registry key"
       Exit Function
   End If

   Dim rgch As String
   rgch = String(64, 0)

   Dim cbrgch As Long
   cbrgch = Len(rgch)

   lResult = RegQueryValueEx(hKey, "", 0, 0&, rgch, cbrgch)

   lResult = RegCloseKey(hKey)

   Dim temp As String

   temp = Mid$(rgch, 1, cbrgch)

   GetDCOMVersion = temp

End Function

Private Sub Command1_Click()
   MsgBox GetDCOMVersion()
End Sub
Run the program and click Command1.

Result: If DCOM is installed on the system, the message box will display the version number.

The function DCOMEnabled may be used separately to determine if DCOM has been installed on a system without checking for the version.


Author Comment

ID: 2779911

You have suggested a long way.

I am trying to get it done in a simpler way, here is the sample code, and the error, please tell me what is wrong here

Private Declare Function ConvertFileA Lib "ETIWRD32.DLL" _
Alias "ConvertFile" (ByVal InputFile As String, ByVal OutputFile As String,
ByVal Callback As Long) As Long
Private Sub Form_Load()
    ConvertFileA "aaa.doc", "aaa.txt", 0
End Sub

The convert was called, but an error pops up when the function call
exits, here is the error message
Run-time error '49':
Bad DLL calling convention



Expert Comment

ID: 2780167
Are you sure of the syntax of the DLL.

The error number you describe is when the paramaters are incorrect.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 2780172
I have absolutely no idea if it will help, but you could try passing vbNull as the last parameter instead of 0...

Accepted Solution

BobbyOwens earned 100 total points
ID: 2780179
I've just found this:

It is not possible to directly call a C function in a DLL if that function uses the _cdecl calling convention. This is because Visual Basic uses the _stdcall calling convention for calling functions. This is a problem because if _cdecl is used, the calling function is responsible for cleaning up the stack. However, if _stdcall is used, the called function is responsible for cleaning up the stack.

NOTE: An .EXE file created in Visual Basic will allow you to call a DLL function that has been declared with the _cdecl calling convention without an error. It is only when you try to call such a function when running a program from the Visual Basic IDE, that Visual Basic generates the following error:

Run-time Error '49':
Bad DLL Calling Convention

You could try passing a variable as the last parameter instead of an actual number

Expert Comment

ID: 2780184
The calling convention is something that I don't believe you are going to be able to do anything about in VB. Essentially it is the way that registers get called and who handles clean up of memory the DLL or the calling application. Most better programmers use a convention called STDCALL (which is available to almost all development environments), but occasionally a C or C++ programmer will use CDECL. As far as I know VB makes no allowance for this convention. I know that STDCALL works, since UI use it all the time with Delphi and VB.

Good luck!!

Author Comment

ID: 2784982

Your are almost right. I compiled the VB into .EXE and it runs ok. But it works if compiled with No Optimization.

Note: vbNull doesn't help.

Thanks to other guys too.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question