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.
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.