Returning Visual Basic strings from a DLL

Windows specific forum
Tipperton
Addict
Addict
Posts: 1286
Joined: Thu Jun 19, 2003 7:55 pm

Returning Visual Basic strings from a DLL

Post 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!
User avatar
the.weavster
Addict
Addict
Posts: 1581
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Post 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
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

Typical: "As long it runs i don't have to verify my bad code" right?
User avatar
the.weavster
Addict
Addict
Posts: 1581
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Post by the.weavster »

We eagerly await your vastly cleverer solution.
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post 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..
User avatar
the.weavster
Addict
Addict
Posts: 1581
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Post 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.
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post 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 :)
User avatar
the.weavster
Addict
Addict
Posts: 1581
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Post by the.weavster »

You are so far up your own rectum!
Why don't you just say what's wrong with it?
Tipperton
Addict
Addict
Posts: 1286
Joined: Thu Jun 19, 2003 7:55 pm

Post 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... ;))
Post Reply