Solved

DLL> passing a string to VB

Posted on 1999-01-04
7
254 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
Comment Utility
0
 

Author Comment

by:SergeD
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:SergeD
Comment Utility
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
Comment Utility
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
Comment Utility
Beki, BSTR == OLECHAR == 2 bytes character
0
 

Author Comment

by:SergeD
Comment Utility
Yahooo...


Thanx
Serge
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 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

11 Experts available now in Live!

Get 1:1 Help Now