CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Guest

    Calling DLL from VB

    Hi, everyone !
    I am a beginner to Visual Basic programming. Please help me.

    What I want to do is to call a function in a dll created by C++.

    C++ source code :

    extern "C" __declspec(dllexport) long DllTest2(int *pMode);
    __declspec(dllexport) long DllTest(int *pMode)
    {
    *pMode = 221;
    return 725;
    }

    My Basic Code :
    Private Declare Function DllTest Lib "dtest.dll" (ByRef mode as Long) As Long

    Dim mode As Long

    Private Sub Command1_Click()
    res = DllTest(Mode)
    End Sub


    It's very simple code, but I always got runtime error #49, telling me that I violated calling rule.
    (Help tells me that something is wrong in argument type).

    Please help me !
    What's wrong with my code ?


    Thanks in advance.
    J. Lee



  2. #2
    Join Date
    Oct 1999
    Location
    CA
    Posts
    91

    Re: Calling DLL from VB

    I hardly know C++, so this may be way way off, but...

    It looks like you are declaring the variable "*pMode" as an integer, and in your vb code your declaring it as a long.


    private Declare Function DllTest Lib "dtest.dll" (byref mode as Integer) as Long




    Brewguru99

  3. #3
    Join Date
    May 1999
    Location
    Republic of Korea
    Posts
    74

    Re: Calling DLL from VB

    Thank you for your answer.

    Yes, it was my mistake to declare mode as long.
    I modified it, but the situation is the same.

    Any other suggestions ?

    Thanks in advance,
    J. Lee



  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: Calling DLL from VB

    IMHO you need to specify __stdcall in your C declaration of the function.
    long vs. integer is not your problem:
    a C int is equivalent to a VB long!


  5. #5
    Join Date
    Sep 1999
    Location
    Northern Utah, U.S. of America
    Posts
    38

    Re: Calling DLL from VB

    Hey Jayson Lee:

    Yes, as stated in the next email msg, there some rules you must follow:

    E.g.: From a "C" DLL to VB:

    char to char
    short to int But be very careful, or the DLL File will 'burn' you.
    int to long
    long to long < from my experience.

    I made a simple, yet straight forward VC++ DLL File(32-bit) without any MFC stuff and there is no name mangling. I have made a VB front end program that calls my newly made DLL file and it works really well!. I interested, drop me an email msg and we can work something out.

    VBFINGERS.
    RSVP...



  6. #6
    Join Date
    May 1999
    Location
    Republic of Korea
    Posts
    74

    Re: Calling DLL from VB

    Thank you for the answer.

    I solved the problem by refering MSDN site of MS.
    You are right that I should have used __stdcall prefix for the C++ function.
    But in that case, the compiled name of the C++ function is changed to strange one like _DllTest@12.
    So VB could not find the entry point for the function DllTest, because the name DllTest does not
    exist in the dll.
    To solve this problem, I needed to use Alias at the declaration of function in VB as follows.

    Declare Function DllTest lib "Dtest.dll" Alias "_DllTest@12" (......) As Long

    Otherwise, I could add the alias to the DEF file of the C++ project as follows.

    Exports
    DllTest = _DllTest@12

    Of course, in this case, no alias is needed in VB part.

    Thanks again.



  7. #7
    Join Date
    May 1999
    Posts
    3,332

    Re: Calling DLL from VB

    No, sir, you don't need an Alias.
    Just create a DEF file for your C DLL and define a "clean" name for your function in the EXPORTS section.


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

Featured