Page 1 of 2
Visual Basic and PureBasic DLLs
Posted: Fri Jun 04, 2004 10:48 am
by neomancer
How do i make a DLL for a Visual Basic program?
i need to return a string, but VB keeps crashing. 8O
any help?
Posted: Fri Jun 04, 2004 11:08 am
by fweil
neomancer,
Do you need a bytes string or a BSTR ?
Returning the string passes the address to this string.
If you need a BSTR instead of regular ASCII string, you will have to use a conversion procedure, easy to find here.
Rgrds
Posted: Fri Jun 04, 2004 11:12 am
by neomancer
well basically i want to see if i can pass a text file out of the dll
so it would be ASCII, i dont know much about Purebasic.
Its a project i wrote years back, you pass a whole readme.txt and it jumbles up the text file and passes it back
<vb code>
newstring$ = MyFunction(textfromfile$)
(myfunction is from the PureBasic Dll declaration)
if it is the address of the string by this you mean pointer?
Posted: Fri Jun 04, 2004 12:19 pm
by fweil
the Procedure or ProcedureDLL would look like that :
Code: Select all
Procedure.s MyFunction(FileName.s)
Result.s = ""
If ReadFile(99, FileName)
LengthOfFile.l = Lof()
*Buffer = AllocateMemory(LengthOfFile)
ReadData(*Buffer, LengthOfFile)
CloseFile(99)
Result.s = PeekS(*Buffer)
FreeMemory(*Buffer)
EndIf
ProcedureReturn Result
EndProcedure
;
; test code
;
Debug MyFunction("C:\test.txt")
Posted: Fri Jun 04, 2004 12:26 pm
by neomancer
wow i realy dont know whats going on here
ok.
how do i get a procedureDLL to return the string "Your string is returned"
this is for a VB project
Posted: Fri Jun 04, 2004 12:52 pm
by Edwin Knoppert
1) Create mem using: SysAllocStringByteLen() API.
2) The resulting pointer should be returned in your proceduredll.L << Note the L
3) MAKE SURE VB is declared as string, this will release the created memory by VB as should.
So pb returns a long, the address of the memory.
Yes, the 100% desire BSTR that is!
(Which pb should implement imo)
Posted: Fri Jun 04, 2004 12:53 pm
by Edwin Knoppert
Forgot to mention, you should pass desired .s data to this sysalloc.. api.
or use movememory(), whatever..
Posted: Fri Jun 04, 2004 1:25 pm
by neomancer
have you got an example?
for both pb and vb
i dont know how this stuff work at all! i come from a background of BlitzBasic (they dumbed me!)
Posted: Fri Jun 04, 2004 1:52 pm
by Edwin Knoppert
A bit bloathed, used PBDev
Purebasic part:
Code: Select all
;=========================================================
; PBDev PureBasic Developer, 1.00, 2004-06-04, 15:04
;=========================================================
#PBD_DLL = 1
Global PBD_App_hInstance.l
Global PBD_App_Title.s
Global PBD_LasthWnd.l
ProcedureDLL.l TEST()
Protected pBSTR.l
Protected s.s
Protected nLen.l
s = "1234567890"
pBSTR = SysAllocStringByteLen_( s, Len( s ) )
nLen = SysStringByteLen_( pBSTR )
MessageRequester( "Purebasic", PeekS( pBSTR, nLen ), 0 )
ProcedureReturn pBSTR
EndProcedure
Procedure.l PBD_Initialize()
PBD_LasthWnd = 0
PBD_App_hInstance = GetModuleHandle_(0)
PBD_App_Title = ""
EndProcedure
Procedure.l PBD_LibMain( hInstance, nReason )
Select nReason
Case 1 ; DLL_PROCESS_ATTACH
Case 0 ; DLL_PROCESS_DETACH
Case 2 ; DLL_THREAD_ATTACH
Case 3 ; DLL_THREAD_DETACH
EndSelect
ProcedureReturn 1
EndProcedure
ProcedureDLL AttachProcess( hInstance )
PBD_App_hInstance = hInstance
ProcedureReturn PBD_LibMain( hInstance, 1 )
EndProcedure
ProcedureDLL DetachProcess( hInstance )
ProcedureReturn PBD_LibMain( hInstance, 0 )
EndProcedure
ProcedureDLL AttachThread( hInstance )
ProcedureReturn PBD_LibMain( hInstance, 2 )
EndProcedure
ProcedureDLL DetachThread( hInstance )
ProcedureReturn PBD_LibMain( hInstance, 3 )
EndProcedure
VB part:
Code: Select all
Option Explicit
Private Declare Function TEST Lib "D:\TEMP\PB70E6\unsaved.dll" () As String
Private Sub Form_Load()
MsgBox TEST()
End Sub
Posted: Fri Jun 04, 2004 2:06 pm
by fweil
I am not sure of what you need exactly, so I suggest you look at this code and come back with questions if needed.
Here you have some ways to manipulate strings and pointers that should fit you needs.
Rgrds
Code: Select all
;
; ANSI2UNI
;
; Converts an ANSI string to a Unicode compatible string
;
; in <= ansi string
; out => unicode string pointer
;
ProcedureDLL.l ANSI2UNI(ansi.s)
size.l=MultiByteToWideChar_(#CP_ACP,0,ansi,-1,0,0)
Dim unicode.w(size)
MultiByteToWideChar_(#CP_ACP, 0, ansi, Len(ansi), unicode(), size)
ProcedureReturn @unicode()
EndProcedure
;
; UNI2ANSI
;
; Converts a Unicode string to an ANSI string
;
; in <= unicode string pointer
; out => ansi string
;
ProcedureDLL.s UNI2ANSI(*Unicode.l)
size.l = WideCharToMultiByte_(#CP_ACP, 0, *Unicode, -1, #Null, #Null, #Null, #Null)
ansi.s=Space(size)
WideCharToMultiByte_(#CP_ACP, 0, *Unicode, -1, @ansi, size, #Null, #Null)
ProcedureReturn ansi
EndProcedure
;
; FileToString
;
; Loads a full file in a single ANSI string
;
; in <= Filename
; out => Pointer to the string containing the file's content
;
ProcedureDLL.s FileToString(FileName.s)
Result.s = ""
If ReadFile(99, FileName)
LengthOfFile.l = Lof()
*Buffer = AllocateMemory(LengthOfFile)
ReadData(*Buffer, LengthOfFile)
CloseFile(99)
Result.s = PeekS(*Buffer)
FreeMemory(*Buffer)
EndIf
ProcedureReturn Result
EndProcedure
ProcedureDLL.l StringToPointer(String.s)
ProcedureReturn @String
EndProcedure
;
; test code
;
Debug FileToString("C:\test.txt")
*Pointer = ANSI2UNI("Ben ça alors, dis donc, si j'avais su.")
Debug UNI2ANSI(*Pointer)
String.s = "azertyqwertyqwertz"
Debug StringToPointer(String)
Debug @String
Posted: Fri Jun 04, 2004 2:17 pm
by Edwin Knoppert
1) your calls use swapped names like uni2ansi but returns uni
2) VB seems to prefer ansi data (BSTR)
The example posted is used a lot..
Posted: Fri Jun 04, 2004 2:22 pm
by fweil
post updated ... sorry for the mix ...
Posted: Fri Jun 04, 2004 2:42 pm
by neomancer
I got a Syntax error! Protected lPSTR.l
[edit]
WORKS!!!!!!!!!!
your lovely! wow and its so simple!
Re: Visual Basic and PureBasic DLLs
Posted: Sat Jun 02, 2012 11:21 pm
by TeraByte
Addict,
Thank you for your great VB to PB example. It is much appreciated and works perfect.

Re: Visual Basic and PureBasic DLLs
Posted: Sat Jun 02, 2012 11:43 pm
by swan
Tell me that it didn't take U 8 years to figure out it worked ???
