DLL and VB String help

Just starting out? Need help? Post your questions and find answers here.
neomancer
User
User
Posts: 14
Joined: Fri Jun 04, 2004 10:25 am

DLL and VB String help

Post by neomancer »

Hi, im having some luck with returning a string pstr value from a dll to VB
the question is now, how do i pass a string to the dll

it works when i declair the dll
eg. Declare Sub ReadString Lib "mydll.dll" (ByVal Length As string)


but i want to us it by using the api calls,
LoadLibrary and CallWindowProc
eg
lret = loadlibrary("mydll.dll")
xret = callwindowproc(me.hwnd, "ReadString", "hello", 0&)

but this crashes?



the pure basic code is a simple dll

proceduredll.l ReadString(string.s)
protected buffers.s
buffers = string.s + " << testing"
pBSTR = SysAllocStringByteLen_( buffers, Len( buffers ) )
procedurereturn pbSTR
endprocedure

the dll works, using declair, but not with the callwindowsproc

any help?
They can smash your cookie, but they cant take you fortune
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I'm not surprised it crashes! :D CallWindowProc() is for calling windows procedures only, and is usually used when subclassing controls etc.

The thing is that each language has it's own way of calling exported functions in a dll; Purebasic uses CallFunctionFast() after calling GetFunction() etc. (which will presumably be a wrapper for GetProcAddress() ). VB uses declares and so on.

I don't think Windows is in the business of calling an exported function for us as it knows little about the parameters involved etc.
I may look like a mule, but I'm not a complete ass.
neomancer
User
User
Posts: 14
Joined: Fri Jun 04, 2004 10:25 am

Post by neomancer »

the code works when i want the dll to return a string to me, (or pBSTR value) this works great, but the moment i want to pass parameters to a dll functions it crashes...

[vb bit]
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hModule As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Any, ByVal wParam As Any, ByVal lParam As Any) As Long
Private Declare Function lstrlenA Lib "kernel32" (ByVal lpString As Long) As Long
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal length As Long)

Private Sub Command1_Click()
Dim obj As Long
Dim lngRetProc As Long
Dim strData As String

Dim number As Long
Dim ret As Long

obj = LoadLibrary(App.Path & "\mydll.dll" & Chr(0))
lngRetProc = GetProcAddress(obj, "EngineInfo" & Chr(0))
ret = CallWindowProc(lngRetProc, Me.hWnd, "EngineInfo", ByVal 0&, ByVal 0&)

FreeLibrary obj
DoEvents

MsgBox PointerToString(ret)


End Sub

Private Function PointerToString(lngPtr As Long) As String

Dim strTemp As String
Dim lngLen As Long


If lngPtr Then
lngLen = lstrlenW(lngPtr) * 2
If lngLen Then
strTemp = Space(lngLen)
CopyMemory ByVal strTemp, ByVal lngPtr, lngLen
PointerToString = Replace(strTemp, Chr(0), "")
End If
End If
End Function

Private Sub Command2_Click()
Dim obj As Long
Dim lngRetProc As Long
Dim strData As String

Dim number As Long
Dim ret As Long

Dim sTemp As String
Dim lParam
Dim wParam

obj = LoadLibrary(App.Path & "\mydll.dll" & Chr(0))

lngRetProc = GetProcAddress(obj, "EnginesAvail" & Chr(0))

If lngRetProc <> 0 Then

ret = CallWindowProc(lngRetProc, Me.hWnd, "SayHello", "neochrome", 0&)

MsgBox PointerToString(ret) & lParam
End If
DoEvents
FreeLibrary obj
End Sub


[] Command 1 works, no problem

[PUREBASIC BIT]
;;;;; VERSION 1.0
;;;;; LAST UPDATE: 11-APRIL-2006
;;;;; BUILD : 1.0.1

#PBD_DLL = 1
Global PBD_App_hInstance.l
Global PBD_App_Title.s
Global PBD_LasthWnd.l

Global STRBUILD.s
Global STRDATE.s


ProcedureDLL.l EngineInfo()
Protected buffers.s

buffers = STRBUILD + Chr(10)+Chr(13) + STRDATE
pBSTR = SysAllocStringByteLen_( buffers, Len(buffers))
ProcedureReturn pBSTR
EndProcedure

ProcedureDLL.l SayHello(value.s)
Protected buffers.s

buffers = "HELLO: " + value
pBSTR = SysAllocStringByteLen_( buffers, Len(buffers))
ProcedureReturn pBSTR

EndProcedure

;--------- DLL SYSTEM FUNCTIONS -------------------------------------------------------------
Procedure.l PBD_Initialize()
PBD_LasthWnd = 0
PBD_App_hInstance = GetModuleHandle_(0)
PBD_App_Title = ""

EndProcedure


Procedure.l PBD_LibMain( hInstance, nReason )

STRBUILD = "BUILD: 1.0.1"
STRDATE = "DATE: 11-APRIL-2006"

StrURLUK = "http://uk.altavista.com/web/results?ita ... stq=*start*"

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



[end]
They can smash your cookie, but they cant take you fortune
Post Reply