Page 1 of 1
Returning Visual Basic strings from a DLL
Posted: Sun Feb 18, 2007 5:15 pm
by Tipperton
This could probably also be in coding questions but since Visual Basic is a Windows only programming language I thought it would fit better here.
I need to write some DLL functions that can return a string to a Visual Basic program. Any tips on where to look for how to do it?
I know I could use the method for getting strings from API calls, such as GetEnvironmentVariable where you create a stirng buffer to receive the data, then send a pointer to the string to the DLL.
But if possible, I'd prefer the function actually return a string to Visual Baic just like Visual Basic's built-in string functions do.
Thanks!
Posted: Mon Mar 05, 2007 9:24 am
by the.weavster
I don't think VB has an equivalent of a PeekS() command but you could create one, try this (to be added to a VB code module):
Code: Select all
Public Declare Function lstrlenA Lib "kernel32" (ByVal lpString As Long) As Long
Public Declare Sub RtlMoveMemory Lib "kernel32" (dest As Any, source As Any, ByVal bytes As Long)
Function PeekS(ByVal MemAddress As Long) As String
'...determine the length of the null terminated string at the stated memory address...
length = lstrlenA(MemAddress)
'...now make it available to VB...
PeekS = Space$(length)
RtlMoveMemory ByVal PeekS, ByVal MemAddress, length
End Function
Posted: Mon Mar 05, 2007 9:36 am
by Edwin Knoppert
Typical: "As long it runs i don't have to verify my bad code" right?
Posted: Mon Mar 05, 2007 9:49 am
by the.weavster
We eagerly await your vastly cleverer solution.
Posted: Mon Mar 05, 2007 12:27 pm
by Edwin Knoppert
The solution is to read (and learn) about VB strings so check MSDN (TIP: BSTR)
Problem is that i seen this kind of code in several occasions and no one seems to verify their code..
Posted: Mon Mar 05, 2007 12:46 pm
by the.weavster
Edwin Knoppert wrote:The solution is to read (and learn) about VB strings so check MSDN (TIP: BSTR)
I don't think I'll bother, I have no real interest in VB.
Edwin Knoppert wrote:Problem is that i seen this kind of code in several occasions and no one seems to verify their code..
Well, why don't you tell everyone what you think is wrong with it.
Posted: Mon Mar 05, 2007 12:53 pm
by Edwin Knoppert
>Well, why don't you tell everyone what you think is wrong with it
Not in this particulair case since it's imo a bad thing to dump some code which was obviously not verified..
and.. why didn't you read stuff instead of waiting for the correct answer?
>I don't think I'll bother, I have no real interest in VB.
That's my point, just dumping some "hey it works" code..
Problem is that this code remains in for years while it's working incorrect.
O well, a monday-moodswing came up

Posted: Mon Mar 05, 2007 1:00 pm
by the.weavster
You are so far up your own rectum!
Why don't you just say what's wrong with it?
Posted: Mon Mar 05, 2007 1:40 pm
by Edwin Knoppert
Posted: Mon Mar 05, 2007 2:16 pm
by Tipperton
Edwin, Do'h! BSTR! If I had remembered that's what VB strings were called I probably could have found this myself!
Anyway, Thank you! That gives me a good place to start.... (and get into trouble...

)