Functions in a C DLL returning strings as char *

E

Erich Neuwirth

I am calling a function in a DLL from VB6, and I need to convert the
code to VB.NET.
The function in the DLL is written in C and returns a char *
By googling around I found that the way to handle this:
(dllNewLispEval is the C function returnin a char *)


Public Declare Function dllNewlispEval Lib "newlisp" _
Alias "newlispEvalStr" (ByVal LExpr As String) As Long
Declare Function lstrLen Lib "kernel32" Alias "lstrlenA" _
(ByVal lpString As Long) As Long
Declare Function lstrCpy Lib "kernel32" _
Alias "lstrcpyA" (ByVal lpString1 As String, _
ByVal lpString2 As Long) As Long


Function newlispEval(LispExpression As String) As String
Dim Result As String
Dim ResHandle As Long
Dim ResultCode As Long
ResHandle = dllNewlispEval(LispExpression)
Result = Space$(lstrLen(ByVal ResHandle))
lstrCpy ByVal Result, ByVal ResHandle
newlispEval = Result
End Function

This technique works.

I have not been able to convert this technique to VB.NET.
How can I call the C function in VB.NET
(I cannot change anything in the DLL)?
I know that I have to convert Long to Integer.
I think I already managed to use lstrLen correctly,
but I have not been able to convert the declaration of
lstrCopy successfully.

I found some code in this newsgroup dealing with a similar problem,
but there the string was a parameter, not a return value of the function.
That solution used System.Text.Stringbuilder,
and it assigned a buffer length before it did the function call. My
returned string can become arbitrarily large, therefore I would
like to dynamically create the buffer just as big as needed
for each function call.
 
I

Imran Koradia

Try this:

Private Declare Unicode Function lstrcpy Lib "kernel32" _
Alias "lstrcpyW" (ByVal strBuff As System.Text.StringBuilder, _
ByVal str As String) As IntPtr
Dim str As String = "test string"
Dim strbuff As New System.Text.StringBuilder
Call lstrcpy(strbuff, str)
MessageBox.Show(strbuff.ToString)

you don't need to pre-assign any buffer length.

hope that helps..
Imran.
 
E

Erich Neuwirth

This works,
but I cannot adapt it to my needs.
The variable str from the example should get a return value
from a C function which in C returns a char *.
Perhaps lstrCpy is is not even needed and the Declare statement for the
C function can ba adapted in a way that the return value can immediately
be used as a VB.NET string.
 
I

Imran Koradia

hmm..since your function is returning a char* (LPSTR), you can try defining
your function as:

Public Declare Function dllNewlispEval Lib "newlisp" _
Alias "newlispEvalStr" (ByVal LExpr As String) As
System.Text.StringBuilder

and then use your function as:

Function newlispEval(LispExpression As String) As String
Dim Result As System.Text.StringBuilder
Result = dllNewlispEval(LispExpression)
Return Result.ToString
End Function

Give that a try and see if it works..

hope this helps..
Imran.
 
E

Erich Neuwirth

Thanks, that solved the problem!


Imran said:
hmm..since your function is returning a char* (LPSTR), you can try defining
your function as:

Public Declare Function dllNewlispEval Lib "newlisp" _
Alias "newlispEvalStr" (ByVal LExpr As String) As
System.Text.StringBuilder

and then use your function as:

Function newlispEval(LispExpression As String) As String
Dim Result As System.Text.StringBuilder
Result = dllNewlispEval(LispExpression)
Return Result.ToString
End Function

Give that a try and see if it works..

hope this helps..
Imran.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top