Solved

DLL> passing a string to VB

Posted on 1999-01-04
7
255 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting to C# also VS2015 5 117
c++ syntax question 9 44
Eclipse IDE - Cannot copy/paste from console output 8 127
Header of docx file 17 97
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. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

910 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

20 Experts available now in Live!

Get 1:1 Help Now