Results 1 to 9 of 9

Thread: SHORT Translation from C++ to VB

  1. #1

    Thread Starter
    Lively Member HeVa's Avatar
    Join Date
    Jul 2001
    Location
    DC
    Posts
    115

    SHORT Translation from C++ to VB

    Hi everyone:

    I'm very new to C/C++ and working with some C++ code that calls some functions from a compiled DLL that was written in C++ v.2.0 (I think). I need to call these same functions from VB. I can see the functions that are available (in Dependency Walker) but I can't get the parameters. So I thought that I would just go to the C++ code that calls the DLL functions to find out what kinds of parameters were expected.

    Here's the C code:

    Code:
    int GridExists(char *grid_name);
    Here's the code that I used:

    Code:
    Private Declare Function GridExists Lib "evildll.dll" (ByRef grid_name As String) As Integer
    When I call the function like this:

    Code:
    i = GridExists("C:\MyDirectory\my_grid_file.grd")
    I get the "Bad DLL Calling Convention" (error 49) message.

    Anyone have a clue as to what I am doing wrong?
    H e*V a

  2. #2
    I'm trying to learn DLLs myself, but I think using a dynamically-sized char array (char*) will not work.

  3. #3
    Yes, I know both VB and C++, but I am more strong with VB. I believe there is a macro definition for a string that you put in your DLL. But, as I am learning DLLs myself, I don't know much more.

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    First the function should be declared for export

    int __stdcall GridExists(char *grid_name);

    and put it in a *.def file. Also in C++ int is 4 bytes long, so in VB the return type will be long not integer.
    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

  5. #5

    Thread Starter
    Lively Member HeVa's Avatar
    Join Date
    Jul 2001
    Location
    DC
    Posts
    115

    Lightbulb A light comes on...

    Vlatko:

    THANK You. I didn't know that integers were 4 bytes in C -- that clarifies a lot of things. I wondered how pointers would fit into an integer variable...

    I'll give the def file a try...
    H e*V a

  6. #6
    Sc0rp
    Guest
    Also, from my experience with external DLL fucntion calling from VB, even if the function receives a pointer or a reference you should pass it ByVal in VB.

    I have NO idea why.

  7. #7

    Thread Starter
    Lively Member HeVa's Avatar
    Join Date
    Jul 2001
    Location
    DC
    Posts
    115
    Filburt1, Vlatko, Sc0rp:

    Thanks for your input ...

    Your suggestions were all very good, and I learned a lot from them (enough to solve some other issues I've been having), but I still haven't gotten it working. I'm not asking for additional help, because I think I just need to gather more information.

    I did learn a few things, though. One thing that was wrong with my vb declaration was passing a string as the parameter. A "char" variable in C is not, in fact, equivalent to a vb string. If the function were expecting a string, the C declaration would have been LPSTR or LPCSTR. Where there is a char, it's expecting a byte value (or array).

    Also, for everyone who has ever had a need like mine, I stumbled across an entry in the vb help files titled "Converting C Declarations to Visual Basic." It is as handy as it sounds, but still wasn't enough to help me get it working. Thanks again so much for your help, each of you. I'll post again, if I ever resolve it...
    H e*V a

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    actually, LPSTR is just an alias name for char*, and LPCSTR is an alias for const char* ( meaning an unchangeable string)
    those names are only used under windows (and declared i some subinclude of windows.h)
    somewhere in the reference is stated that you should pass C "char*" arguments "ByVal As String"
    but maybe the problem is that the old dll functions are not declared as __stdcall. If so, all you tries to get it working are futile unless you have the original source code.
    CornedBee

  9. #9

    Thread Starter
    Lively Member HeVa's Avatar
    Join Date
    Jul 2001
    Location
    DC
    Posts
    115
    Thanks, CornedBee for clearing that up. The C code that I posted in the beginning actually calls the dll successfully. I still wonder, why is it possible to access the DLL from C and not from VB?
    H e*V a

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