473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Functions in a C DLL returning strings as char *

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
4 5824
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: c_monty | last post by:
I am used to Delphi and VB, where functions can return strings. I recently starting learning C and my findings are that you can have external functions build strings, but the function cannot return...
12
by: LongBow | last post by:
Hello all, From doing a google serach in the newsgroups I found out that a string can't be returned from a function, but using a char* I should be able to do it. I have spent most of the day...
19
by: Rick | last post by:
Hi, I was wondering, can it be safely assumed that any function that returns a *char will return a NULL terminated string? Or does the function explicitly mention this always (and the ones that...
2
by: MJL | last post by:
I am working on a small project that involves the manipulation of dynamically allocated memory for strings. I was wondering why the string.h functions are the way they are and why not as follows:...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
7
by: wonderboy | last post by:
Hey guys, I have a simple question. Suppose we have the following functions:- //-----My code starts here char* f1(char* s) { char* temp="Hi"; return temp;
6
by: karthika.28 | last post by:
Hi, I am writing a function that needs to return an array of strings and I am having some trouble getting it right. Can you help me answer the following questions? 1. How does the function...
3
by: John Smith | last post by:
I wrote some code in C in a dll which I would like to call from C#. However I'm stuck because of the strongly typed behavior of C# which makes limitations. Here are the prototypes for two...
10
by: ptq2238 | last post by:
Hi, Tried this code to assist my understanding of strings and functions but I'm not sure why the errors are occurring and hope someone can shed some light to my learning. #include <stdio.h>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.