Solution: Calling C++ dll from Visual Basic 
Author Message
 Solution: Calling C++ dll from Visual Basic

>Solution! After hours and hours of banging our heads against the wall and
>reading 100s of posts in this forum, we finally were able to call a C++ dll
>from VB.

/me too...but with some small additions:
>(1) Use a .def file. Yes, this is archaic, but do you want it to work?

I still feel sick about this because I never needed when using Borland C++
with VB - but anyway...
> DoSomething

Defining the HEAPSIZE explicitly is not really necessary (in most cases).

>(2) Since you're using the .def, you don't need to use the
>__declspec (dllimport) in the function definition (see below)

True, but if you do so, you may leave out the far Pascal instead (at least
that worked for my functions). So, its probably the easier way to leave the
functions as they are and just add the .def-File, if you are porting from
another compiler where your dll already worked properly.

>(3) If your functions have any parameters, you do need to add "far pascal"
>the function definition in order to avoid that {*filter*} "bad calling
>message. For details, check out MSDN article Q85108, but basically, "far
>pascal" makes the dll use the Pascal/fortran/BASIC calling convention
>of the default C one. Oh, and you don't need a prototype, but it doesn't


Im not totally sure about this, but I think you could use APIENTRY
instead of  far pascal which would be more Windows-like. Well, I have to
try this...

>(1) If you need to pass strings, you HAVE to pass them "ByVal" even if the
>function changes them. Passing them "ByVal" just adds a null at the end of
>the string. Any changes the DLL makes will still be reflected in the
>after the call.

Thats not really true. Again, Im not sure if this works in VC++ but in
Borland C++ Ive found a way to use the strings ByRef. However, when I did
it the first time, it took some extra head-against-the-wall hours.
So heres what I do:

      Public Declare Function DoSomething Lib "my.dll" (ByVal TestString1,
ByRef TestString2 As String) As Long
      Dim TestString1, TestString2 as String
      Dim Result as Long
      TestString1 = Hello World
      TestString2 = String(256, Chr$(0))
      Result = DoSomething(TestString1, TestString2)

Borland C++ (the weird part, should be same in VC++):

      extern "C" long __declspec(dllexport) DoSomething(LPSTR TestString1,
LPSTR *TestString2){
            LPSTR TestString2 = *TestString2;
            strcpy(TestString2, TestString1);
            return 1;

I dont really remember why I did it this way. The important part when
modifying the string in the dll, however, is that the string is declared by
String(XXX, Chr$(Y)) in VB, so you can be sure the allocated memory for the
string is big enough.

>(2) Some types in C don't match exactly with VB types. Make sure you're
>the right type. Search for help on "byval" and you'll get a nice table that
>explains how to match the types up.

Correct. Thats why I try to use mainly common types. E.g. I use Long
for all types of integers since I hope that M$ wont develop a 6-byte Long
value ;-)

>(3) It's safest to use the full path of the DLL in case you are unlikely
>enough to choose a dll name that matches an existing one in
>and you get the "can't find entry point in dll"

Thats right, but using the complete name can get very {*filter*} if you get a
large dll with more than 20 functions. In any case I expect that you want to
place the dll in the same directory as the .exe when you finally build a
release version. If so, you will have to change all those complete paths.
So, my trick is to set the C-compilers options to build the dll in the
vb-directory as long as I am developing the app. Later, I simply change the
build path.
The question is: Is there any possibility to make VB use the dlls in the sou
rce-code directory  instead of the VB directory?

>(4) Yes, everything is case sensitive.

True for C++.

>Good luck!

Same to you.



Mon, 03 Sep 2001 03:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. How do you call a Visual C++ (DLL) function from Visual Basic

2. Calling Visual C++ 5.0 DLL Functions From Visual Basic 5.0

3. How to write Visual C++ DLL's and call them from Visual Basic

4. Calling a Visual Basic DLL from C++

5. More on calling Vis C++ DLLs from Visual Basic

6. Calling a C++ Dll in Visual Basic.

7. Calling C++ DLLs from Visual Basic

8. visual basic 4.0 call to borland c++ 5.0 dll

9. To perform file operations in a c++ DLL called by visual basic

10. Calling 32bit C++ DLL from Visual Basic

11. adapting Visual C++ dll for Visual Basic

12. Visual C++ DLL in Visual Basic


Powered by phpBB® Forum Software