Solved

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

Posted on 2000-03-08
5
340 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 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