Results 1 to 15 of 15

Thread: C++ DLL for VB

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    Toronto, Ontario
    Posts
    280

    Question C++ DLL for VB

    I was just wondering how I would go about writing a DLL in C++ for VB6. I know how to make basic DLLs, but I need to be able to do complicated stuff, such as OpenGL API.

  2. #2
    Junior Member Earl of Gonds's Avatar
    Join Date
    Sep 2001
    Location
    marky mark's house
    Posts
    16
    You need to press the Alt-Gorilla key
    MonkeyPoop

  3. #3
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    You make a DLL in C++ for VB exactly as you would make a DLL in C++ for anything else. C++ has no idea how you are going to use the DLL and doesn't care. The issue is how do you get VB to get at the functions you put in the DLL, and that's a bit messy.

    You have to identify the name of the function in the DLL as it exists after C++ mangles it, and then you have to look up the "alias" statement in VB and figure out how to alias your local VB function name to the mangled C++ (DLL) name.

    That's all there is to it. You do, of course, have to be very careful that you pass the right parameters of the right type and in the right order else the alias won't work. An BE CAREFUL about int vs long when talking from VB to C++, and watch out for pass by ref vs pass by val. It is possible to get a valid linkage between VB and the DLL but get weird results if you're not careful.

    Have fun

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The other method, if you want an API that behaves about like the DX8 VB API, is to learn about Automation. It is a weird concept for very advanced programmers that involves a lot of COM, IDispatch and VARIANTs. But it's the only way to get objects from a DLL to any VB version prior to 6.0 (6.0 understands normal COM objects as well, so you could learn those, which is required anyway if you want to learn about automation).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    New Member
    Join Date
    May 2002
    Posts
    8
    yeah.. unfortunately i haven't figure out how to export classes.

    anyone have a clue?

    Thanks

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Exporting classes is different to creating a COM DLL, since C++ links to them differently (and a LOT more efficiently, you just don't get some of the distributed benefits though).
    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

  8. #8
    New Member
    Join Date
    May 2002
    Posts
    8
    so would it be possible for me to use a c++ DLL in my VB app?

    I've been trying for days without success. Any help would be appreciated.

    THANKS!

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Sadly not, C++ DLLs are for C++ only.

    If you want to learn about COM, its best to look at the documentation in the Microsoft Platform SDK about COM, and ATL. You have a lot of reading to do
    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

  10. #10
    New Member
    Join Date
    May 2002
    Posts
    8
    actually i meant to type CLASS instead of DLL.

    linking c++ dlls to VB is easy.

    Public Declare Function TestFunc Lib "testDLL" (byval sSomeVar as string) as Long

    where testDLL.dll is in the same folder with a function TestFunc that takes a string as a paramater.. compiled with c++.

    and in vb you just
    Call TestFunc(sSomeStr)

    at any rate.. after exporting functions and classes from c++ DLLs i have only managed to successfully import functions.. not classes.

    i've heard it can't be done, but if anyone knows for sure, let me know.

    thanks

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    That's what I meant.

    Normal functions (non-class) can easily be exported because it's just a case of calling a function which VB can do. What VB can't do is to interpret the binary layout of a C++ class, along with all the name mangling that is required.

    Either way, you can't use a C++ class from VB unless you're using .NET in which case the whole playing field's been rebuilt
    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

  12. #12
    New Member
    Join Date
    May 2002
    Posts
    8

    Smile

    word.. .NET ehh? hmm... onwards and upwards i suppose. hehe.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Actually it is theoretically possible to export classes to VB - just not as classes:
    Code:
    class ex {
    long l1, l2;
    void DoSomething();
    };
    VB Code:
    1. Private Type classex
    2.   l1, l2 As Long
    3. End Type
    4.  
    5. Declare Sub ex_Construct Alias "ex::ex mangled" Lib "whatever.dll" (pThis As classes)
    6. Declare Sub ex_DoSomething Alias "ex::DoSomething mangled" Lib "whatever.dll" (pThis As classex)
    7. Declare Sub ex_Destruct Alias "ex::~ex mangled" Lib "whatever.dll" (pThis As classes)

    Should work, because machine code doesn't know classes...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I think I'll leave you the amusing job of deciphering MS's mangling strategy and writing an auto-converter for VB (templates as well, lets not skimp ).
    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

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yeah, sure. I already know that C++ mangles always start with ?
    I think I'm nearly done.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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