Solved

DLL> passing a string to VB

Posted on 1999-01-04
7
256 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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. …
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++.

832 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