?
Solved

DLL> passing a string to VB

Posted on 1999-01-04
7
Medium Priority
?
260 Views
Last Modified: 2010-05-18
Hi,

I'm calling a function in a C++ DLL from VB with the following declaration.

VB>
Public Declare Function MyFunction Lib "MyDLL.dll" (ByRef MyString As String) As Long

C++>
long __stdcall CWordAPIApp::MyFunction(                                                                  char** MyString)

It works fine except that the string returned from the DLL to VB contains a NULL character at the end (e.g: "exemple¦")
I'd like to modify the DLL in order to return a standard VB string (e.g: "exemple"). I don't want to truncate the last character of the string in the VB code.

Any idea?

thanx
Serge
0
Comment
Question by:SergeD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 

Author Comment

by:SergeD
ID: 1181466
Sorry it doesn't work as specified in VB knowledge base.

> wsprintf( *MyString, "%s", "Exemple") returns me an exception error in VB.

Here is the contents of my DLL function:

long __stdcall CWordAPIApp::MyFunction(char** MyString)
{
      CString sPath("Exemple");
      long lResponse = 0L;

      char *sTmp=new char[strlen(sPath) - 1];
      strcpy(sTmp,sPath);
      sTmp[strlen(sTmp)] = 0x0;
      (*MyString) = sTmp;

      lResponse = 0;
      return (lResponse);
}


I hope it helps.
Serge
0
 

Expert Comment

by:beki
ID: 1181467
You should switch to BSTR-s. Declare your function in C as:

long ReturnAString(BSTR *outValue)
{
  *outValue = SysAllocString(L"ABCDEF");
  return 0;
}

In VB you declare it like:

Declare Function ReturnAString Lib "xxx.dll" (outValue As String) As Long


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:SergeD
ID: 1181468
Sorry Beki,

it still does work. The result in VB is> "A¦B¦C¦D¦E¦"

Thanx anyway
Serge
0
 

Accepted Solution

by:
beki earned 80 total points
ID: 1181469
That is because you should return ASCII instead of Unicode. Try:

long ReturnAString(BSTR *outValue)
{
  char *stringToReturn = "ABCDEF";
  *outValue = SysAllocStringByteLen(stringToReturn,strlen(stringToReturn));
  return 0;
}

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1181470
Beki, BSTR == OLECHAR == 2 bytes character
0
 

Author Comment

by:SergeD
ID: 1181471
Yahooo...


Thanx
Serge
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 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 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.
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 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