Results 1 to 12 of 12

Thread: DLL Calling Problem?

  1. #1

    Thread Starter
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409

    Smile

    I have a problem when i call the dll from VB.The message box appears but after i click ok i get the error message "bad dll calling convention".How can i solve this.
    The Dll code
    Code:
    #include "stdafx.h"
    void car(HWND h)
    {
    	MessageBox(h,"OK","KO",MB_OK);
    }
    Def File
    Code:
    LIBRARY "brisi.DLL"
    EXPORTS
    	car @2
    And in VB a declare the function and call it:
    Code:
    Public Declare Sub car Lib "brisi.dll" (ByVal h As Long)
    car Form1.hWnd
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Try using:
    Code:
    #include "stdafx.h"
    void __declspec(dllexport) car(HWND h)
    {
    	MessageBox(h,"OK","KO",MB_OK);
    }
    for your code, and
    Code:
    LIBRARY "brisi.DLL"
    EXPORTS
    	car
    for your .def file.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446

    i get bad calling convention to dll

    If i dont pass a parameter to a function dll in c++ a can get a return from it. if i do this

    extern "C" __declspec( dllexport ) long returnNothing(long nDays)
    {
    return nDays;
    }

    i get bad dll calling convention.

    any help heres the declare.

    Declare Function returnNothing Lib "D:\DEVELOP\ALL16_32\myproj\Debug\myproj.dll" (ByVal nDays As Long) As Long

  4. #4
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446
    help!!!

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    extern "C" __stdcall __declspec(dllexport) long returnNothing(long nDays) {
        return nDays; 
    }
    The __stdcall may need to go after the __declspec.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    355
    extern "C" void __stdcall car(HWND h)
    {
    //code
    }


    will work. the extern "C" tells the compiler to do some name mangling thing so VB can understand it, and __stdcall means the func will clear up the stack afterwards, which VB requires.

    def file:

    LIBRARY "yourdll.dll"
    EXPORTS
    car


    buzzwords are the language of fools

  7. #7
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446

    here it is

    extern "C" int __declspec(dllexport) CALLBACK returnNothing (int nDays)
    {
    return nDays;
    }


    vb:

    Declare Function returnNothing Lib "D:\DEVELOP\ALL16_32\myproj\Debug\myproj.dll" Alias "_returnNothing@4" (ByVal nDays As Integer) As Integer


    ps:

    As a real problem with Long any suggestions

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you use a .DEF file then you don't actually need the extern "C", since it causes the linker to rename the external references anyway.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    355
    yeah, but its really weird, i've tried it with just __stdcall and a DEF file, and it doesn't work from VB for me. having extern "C" works though.
    buzzwords are the language of fools

  10. #10
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446

    well, got that working

    Yep, got that working extern "C" _cdecl and _dllexport

    Another problem arouse with VB, cannot send a LONG to c++ dll and return it .. any thoughts.?

    Cheers

  11. #11
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    355
    do you mean something like:

    Declare Sub AddOne Lib "lala.dll" (Num as Long)

    the c++ func should be:

    extern "C" void __stdcall AddOne(long *num)
    {
    *num ++;
    }

    the thing you're probably doing wrong is not declaring the number as a pointer in the C++ function if you simply do
    ..(long num) then any changes won't affect the original number.
    buzzwords are the language of fools

  12. #12
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Manchester
    Posts
    446

    neally

    extern "C" long __stdcall AddOne(long *num)
    {
    return *num;
    }

    What, does this work, i'll try it in VB.
    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width