Xojo Conferences
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/Util/softDeclares/SoftDeclare
Function:
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Util/softDeclares/SoftDeclare
This example is the version from Mon, 24th Sep 2017.
Project "SoftDeclare.rbp"
Class Window1 Inherits Window
Control ListBox1 Inherits ListBox
ControlInstance ListBox1 Inherits ListBox
End Control
EventHandler Sub Open() TestConstant TestDYLIB TestFramework TestLinux TestSpeech TestWindows End EventHandler
Protected Sub TestConstant() if TargetCarbon then dim c as SoftDeclareMBS dim s as CFStringMBS dim m as MemoryBlock // Test Mac OS X // Load the constant kABFirstNameProperty from the Addressbook framework w "Testing constant loading with Mac OS X Framework" c=new SoftDeclareMBS if c.loadlibrary("Addressbook.framework") then w "Loaded "+c.libname+" to "+format(c.libhandle,"-0")+"." if c.LoadConstant("kABFirstNameProperty") then w "Loaded constant "+c.ConstantName+" to "+format(c.ConstantPointer,"-0")+"." m=NewMemoryBlockFromPtrMBS(c.ConstantPointer) // I hope it's not nil! s=new CFStringMBS s.Handle=m.Long(0) s.RetainObject if s=nil then w "The constant is not a CFString!?" else w "Got value: "+s.str end if s=nil m=nil else w "Loading of constant "+c.ConstantName+" failed." end if else w "Loading of "+c.libname+" failed." end if c=nil end if End Sub
Protected Sub TestDYLIB() dim s as SoftDeclareMBS dim m as MemoryBlock w "Testing dylib loading on Mac OS X" s=new SoftDeclareMBS // void *malloc(size_t size); if s.LoadDylib("/usr/lib/libc.dylib") then if s.LoadFunction("malloc") then m=NewMemoryBlock(4) m.Long(0)=100000000 // you see this 100 MB allocation in Activity Monitor if s.CallFunction(1,m) then w "Success on malloc: "+hex(s.Result) else w "Failed on calling function." end if else w "Failed on loading function." end if else w "Failed on loading library." end if End Sub
Protected Sub TestFramework() dim c as SoftDeclareMBS w "Testing framework loading on Mac OS X" // Test Mac OS X c=new SoftDeclareMBS if c.LoadFramework("AppleShareClient.framework") then w "Loaded "+c.libname+" to "+format(c.libhandle,"-0")+"." if c.loadfunction("AFPLibraryVersion") then w "Loaded function "+c.FunctionName+" to "+format(c.functionPointer,"-0")+"." if c.CallFunction(0,nil) then w "Calling returned "+hex(c.result) else w "Failed to call function." end if else w "Loading of function "+c.FunctionName+" failed." end if else w "Loading of "+c.libname+" failed." end if End Sub
Protected Sub TestLinux() dim s as SoftDeclareMBS dim m as MemoryBlock w "Testing lib loading on Linux" s=new SoftDeclareMBS // void *malloc(size_t size); if s.LoadLibrary("libc.so.6") then if s.LoadFunction("malloc") then m=NewMemoryBlock(4) m.Long(0)=100000000 // you see this 100 MB allocation in Activity Monitor if s.CallFunction(1,m) then w "Success on malloc: "+hex(s.Result) else w "Failed on calling function." end if else w "Failed on loading function. " end if else w "Failed on loading library. "+s.Liberror end if End Sub
Protected Sub TestSpeech() dim p,m as MemoryBlock dim name as string dim c as SoftDeclareMBS c=new SoftDeclareMBS w "Testing carbon library loading on Mac OS" // make the string for the first parameter p=newmemoryBlock(256) p.pstring(0)="Hello World!" // make the memoryblock for the parameters m=newmemoryBlock(4) m.long(0)=p.AddressMBS(0) // set the first parameter to the address of the string buffer // Which library to call? if Targetcarbon then if RunningOnCarbonXMBS then name="Carbon.framework" else name="CarbonLib" // for Carbon inside Classic end if else name="SpeechLib" end if // Load librarys if c.loadlibrary(name) then w "Loaded "+c.libname+" to "+format(c.libhandle,"-0")+"." // Load function if c.loadfunction("SpeakString") then w "Loaded function "+c.FunctionName+" to "+format(c.functionPointer,"-0")+"." // Call function if c.CallFunction(1,m) then w "Calling returned "+hex(c.result) // result doesn't matter, but here is it. else w "Failed to call function." end if else w "Loading of function "+c.FunctionName+" failed." end if else w "Loading of "+c.libname+" failed." end if End Sub
Protected Sub TestWindows() if TargetWin32 then dim f as FolderItem dim p,m,b as MemoryBlock dim c as SoftDeclareMBS dim path as string // For Windows, we try GetShortPathName which can't be hard declared because it's not available on Windows NT 4 SP 6 // (This was a bug to hard link it to MBS Plugin 2.5, so I fixed this for MBS Plugin 2.6) // DWORD GetShortPathNameA(IN LPCSTR lpszLongPath,OUT LPSTR lpszShortPath, IN DWORD cchBuffer); w "Testing DLL loading on Windows" c=new SoftDeclareMBS f=ApplicationsFolderMBS(-32766) // get a folder... path=f.absolutePath b=newmemoryBlock(1024) b.long(0)=0 // make emptry C string p=newmemoryBlock(lenb(path)+10) p.cstring(0)=path m=newmemoryBlock(12+10) m.long(0)=p.AddressMBS(0) m.long(4)=b.AddressMBS(0) m.long(8)=b.size-1 if c.loadlibrary("KERNEL32") then w "Loaded "+c.libname+" to "+format(c.libhandle,"-0")+"." if c.loadfunction("GetShortPathNameA") then w "Loaded function "+c.FunctionName+" to "+format(c.functionPointer,"-0")+"." if c.CallFunction(3,m) then w "Calling returned "+hex(c.result) w "Short path is: "+b.cstring(0) else w "Failed to call function." end if else w "Loading of function "+c.FunctionName+" failed." end if else w "Loading of "+c.libname+" failed." end if end if End Sub
Sub w(s as string) listBox1.addrow s End Sub
End Class
MenuBar Menu
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = "File"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu0 = "Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class App Inherits Application
End Class
End Project

See also:

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS Util Plugin.




Links
MBS Xojo Chart Plugins