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
Solved

DLL> passing a string to VB

Posted on 1999-01-04
7
257 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1181465
0
 

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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 

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 20 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

856 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