• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

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

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?
  • 5
  • 4
1 Solution
your missing reversez in the call
eneateAuthor Commented:

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

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)
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

eneateAuthor Commented:
I still get the same error
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.
If you compile to an exe do you still get the same error?

eneateAuthor Commented:

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?
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.
eneateAuthor Commented:
Thanks for the feedback at least I know where the problem is.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now