Solved

Call C++ DLL from VB6 using user-defined data type

Posted on 2000-03-08
5
347 Views
Last Modified: 2010-04-02
From VB, I'm calling a DLL created with C++.  When I pass in a string ByVal, the DLL funstions fine.  When I pass, ByRef, a user-defined data type including strings and longs, the DLL fails.

I've setup an identical structure in Vb to match what is defined in the DLL (including the use of fixed length strings).

I believe using fixed length VB strings will give me a null terminated string in the C-based DLL.

The problem is complicated because I cant find a way to get into debug mode to test the DLL by stepping into it from the VB call.  As a result, I first created and debugged the code with a C-based EXE, then, moved the functions into the DLL.
0
Comment
Question by:GregoryGr
  • 2
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
dlmartz earned 200 total points
ID: 2597701
You need to use BSTR's for your strings in the structures used to pass between VB and C/C++

The knowledge base article Q194609 defines this specifically.

As for debugging, you can debug your DLL in the C++ environment. Assuming your using VisualC++ (if not, others are simular)... go to the settings on your project and assign your VB application as the executable to debug. You may need to add your DLL to the pre-loaded DLL listing (same screen, different tab). They press F5 to begin debugging. It will warn you the process does not contain debug information, set your break points and continue anyway. Your be debugging your DLL.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2599374
listen...
0
 

Author Comment

by:GregoryGr
ID: 2600767
The UDT contains fixed length strings and integers into the DLL, not variable length strings.  With the debugging info you gave me, I found the issue to be VB doesn't NULL terminate strings by default.  I ran into problems trying the BSTR data type so I was planning just to NULL terminate the incoming VB strings.  

Do you think that is a bad idea and I must use BSTR data types?
0
 

Author Comment

by:GregoryGr
ID: 2601960
Using the fixed length strings instead of BSTR within the UDT seems to work for me once I NULL terminated the strings.

Your instructions to test the DLL from C++ helped a great deal!

Thanks!
0
 
LVL 1

Expert Comment

by:dlmartz
ID: 2606616
Glad I could help
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question