Solved

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

Posted on 2000-03-08
5
314 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now