Visual Basic and PureBasic DLLs
Visual Basic and PureBasic DLLs
How do i make a DLL for a Visual Basic program?
i need to return a string, but VB keeps crashing. 8O
any help?
i need to return a string, but VB keeps crashing. 8O
any help?
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
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
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
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?
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?
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")
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
-
- Addict
- Posts: 1073
- Joined: Fri Apr 25, 2003 11:13 pm
- Location: Netherlands
- Contact:
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)
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)
-
- Addict
- Posts: 1073
- Joined: Fri Apr 25, 2003 11:13 pm
- Location: Netherlands
- Contact:
-
- Addict
- Posts: 1073
- Joined: Fri Apr 25, 2003 11:13 pm
- Location: Netherlands
- Contact:
A bit bloathed, used PBDev 
Purebasic part:
VB part:

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
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
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
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
Last edited by fweil on Fri Jun 04, 2004 2:22 pm, edited 2 times in total.
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
-
- Addict
- Posts: 1073
- Joined: Fri Apr 25, 2003 11:13 pm
- Location: Netherlands
- Contact:
Re: Visual Basic and PureBasic DLLs
Addict,
Thank you for your great VB to PB example. It is much appreciated and works perfect.
Thank you for your great VB to PB example. It is much appreciated and works perfect.

Re: Visual Basic and PureBasic DLLs
Tell me that it didn't take U 8 years to figure out it worked ??? 
