PureBasicでDLLに配列を渡すメモ


PureBasicで作ったDLLにPureBasicで作ったアプリケーションから配列を渡すのに手こずって、なんとか解決したのでそのメモ

※配列を渡す際に、配列のアドレスへのアドレス、配列へのポインタへのポインタというべきか。それをパラメータに入れて呼び出さないといけない。

呼び出し側


Dim MyArray.d(4)

MyArray(0) = 123.0
MyArray(1) = 123.1
MyArray(2) = 123.2
MyArray(3) = 123.3
MyArray(4) = 123.4

If OpenLibrary(0, "mydll.dll") Or OpenLibrary(0, "mydll.so")
 ptr = @MyArray()
 CallFunction(0, "DispArray", @ptr )
EndIf

DLL側

CompilerIf #PB_Compiler_OS = #PB_OS_Windows

 ; These 4 procedures are Windows specific
 ;

 ; This procedure is called once, when the program loads the library
 ; for the first time. All init stuffs can be done here (but not DirectX init)
 ;
 ProcedureDLL AttachProcess(Instance)
 EndProcedure

 ; Called when the program release (free) the DLL
 ;
 ProcedureDLL DetachProcess(Instance)
 EndProcedure

 ; Both are called when a thread in a program call or release (free) the DLL
 ;
 ProcedureDLL AttachThread(Instance)
 EndProcedure

 ProcedureDLL DetachThread(Instance)
 EndProcedure

CompilerEndIf

; Real code start here..
;
ProcedureDLL EasyRequester(Message$)

 MessageRequester("EasyRequester !", Message$)

EndProcedure

ProcedureDLL DispArray( Array MyArray.d(1) )
 strArray.s = ""

 MessageRequester("ArraySize", Str(ArraySize(MyArray(), 1)) )

 For x.l = 0 To ArraySize( MyArray(), 1)
 strArray = strArray + StrD(MyArray(x)) + #CRLF$
 ;MessageRequester("MyArray("+Str(x)+")", StrD(MyArray(x)) )
 Next x

 MessageRequester("DispArray", strArray)

EndProcedure

参考にしたサイト

Array parameter in procedures (PureBasic Forum)
Building a DLL (PureBasic.com Documentation)

シェアする

  • このエントリーをはてなブックマークに追加

フォローする