argument not optional in a dll error. Does anyone know why me code is generating this error

Posted on 2006-04-07
Last Modified: 2010-05-01
I am using the following code to call functions from a dll

Private Declare Function CreateFile Lib "SynMF.dll" ( _
                                ByVal createfilename As String, _
                                ByVal magic As Long, _
                                ByVal appname As String, _
                                ByVal username As String, _
                                ByVal usercomments As String, _
                                ByVal sourcepath As String, _
                                ByVal sourcename As String, _
                                ByVal firstsourcenumber As Long, _
                                ByVal lastsourcenumber As Long, _
                                ByVal sourcetype As String, _
                                ByVal originx As Double, _
                                ByVal originy As Double, _
                                ByVal originz As Double, _
                                ByVal reversex As Boolean, _
                                ByVal reversey As Boolean, _
                                ByVal reversez As Boolean, _
                                ByVal factorx As Double, _
                                ByVal factory As Double, _
                                ByVal unitsxy As Long, _
                                ByVal factorz As Double, _
                                ByVal unitsz As Long, _
                                ByVal unitsdisplay As Long) As Boolean

When I include the following call

Dim createfilename As String
    createfilename = FileNameBox.Text
    Dim magic As Long
    magic = 1
    Dim appname As String
    appname = App.Title & " v" & App.Major & "." & App.Minor & "." & App.Revision
    Dim username As String
    username = UserNameBox.Text
    Dim usercomments As String
    usercomments = UserCommentsBox.Text
    Dim sourcepath As String
    sourcepath = SourcePathBox.Text
    Dim sourcename As String
    sourcename = SourceNameBox.Text
    Dim firstsourcenumber As Long
    firstsourcenumber = FirstSourceBox.Text
    Dim lastsourcenumber As Long
    lastsourcenumber = LastSourceBox.Text
    Dim sourcetype As String
    sourcetype = FileTypeCombo.Text
    Dim originx As Double
    originx = XOriginBox.Text
    Dim originy As Double
    originy = YOriginBox.Text
    Dim originz As Double
    originz = ZOriginBox.Text
    Dim reversex As Boolean
    reversex = XReverseCheck.Value
    Dim reversey As Boolean
    reversey = YReverseCheck.Value
    Dim reversez As Boolean
    reversez = ZReverseCheck.Value
    Dim factorx As Double
    factorx = XYFactorBox.Text
    Dim factory As Double
    factory = XYFactorBox.Text
    Dim unitsxy As Long
    unitsxy = 3
    Dim factorz As Double
    factorz = ZFactorBox.Text
    Dim unitsz As Long
    unitsz = 3
    Dim unitsdisplay As Long
    unitsdisplay = 2
   Dim result As Boolean
'result = CreateFile(createfilename, magic, appname, username, usercomments, sourcepath, sourcename, firstsourcenumber, lastsourcenumber, sourcetype, originx, originy, originz, reversex, reversey, factorx, factory, unitsxy, factorz, unitsz, unitsdisplay)
     result = CreateFile(createfilename, magic, appname, username, usercomments, sourcepath, sourcename, firstsourcenumber, lastsourcenumber, sourcetype, originx, originy, originz, reversex, reversey, factorx, factory, unitsxy, factorz, unitsz, unitsdisplay)
    If result = True Then
        MsgBox "File created"
        MsgBox "Create FAILED"
    End If

I have tried several configurations with no siccuess anyone any iadea why?
Question by:eneate
    LVL 28

    Expert Comment

    your missing reversez in the call

    Author Comment


    Thanks for the feedback, but I now get the error bad dll calling convention, any idea why?
    LVL 28

    Expert Comment


    Maybe try:

    result = CreateFile(ByVal createfilename, ByVal magic, ByVal appname, ByVal username, ByVal usercomments, ByVal sourcepath, ByVal sourcename, ByVal firstsourcenumber, ByVal lastsourcenumber, ByVal sourcetype, ByVal originx, ByVal originy, ByVal originz, ByVal reversex, ByVal reversey, ByVal reversez, ByVal factorx, ByVal factory, ByVal unitsxy, ByVal factorz, ByVal unitsz, ByVal unitsdisplay)

    Author Comment

    I still get the same error
    LVL 28

    Expert Comment

    When you get the error, can you click debug to see what line is being highlighted? Is it the call to CreateFile?  I can't test any further because I don't have "SynMF.dll" on my system.
    LVL 28

    Expert Comment

    If you compile to an exe do you still get the same error?;en-us;Q153586

    Author Comment


    Sorry, been away, you are right when I compile and run the program I don't get the error, I guess it must be a vb 6 thing?
    LVL 28

    Accepted Solution

    From the above link:

    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

    The fact that the EXE version allows you to call such functions has been confirmed to be a bug by Microsoft. You should not rely on this behavior as this might change in future versions of Visual Basic.

    Author Comment

    Thanks for the feedback at least I know where the problem is.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now