By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,556 Members | 1,633 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,556 IT Pros & Developers. It's quick & easy.

Functions in a C DLL returning strings as char *

P: n/a
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.

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

"Erich Neuwirth" <er************@univie.ac.at> wrote in message
news:Oc*************@TK2MSFTNGP09.phx.gbl...
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.

Nov 21 '05 #2

P: n/a
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.
Imran Koradia wrote:
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.

"Erich Neuwirth" <er************@univie.ac.at> wrote in message
news:Oc*************@TK2MSFTNGP09.phx.gbl...
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.


Nov 21 '05 #3

P: n/a
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.
"Erich Neuwirth" <er************@univie.ac.at> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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.
Imran Koradia wrote:
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.

"Erich Neuwirth" <er************@univie.ac.at> wrote in message
news:Oc*************@TK2MSFTNGP09.phx.gbl...
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.



Nov 21 '05 #4

P: n/a
Thanks, that solved the problem!
Imran Koradia wrote:
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.
"Erich Neuwirth" <er************@univie.ac.at> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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.
Imran Koradia wrote:
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.

"Erich Neuwirth" <er************@univie.ac.at> wrote in message
news:Oc*************@TK2MSFTNGP09.phx.gbl...
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.

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.