Restored from previous forum. Originally posted by Paul.
I don't know how you can get any simpler than the example that's provided with PB but here you go...
Start PB and type this:
Code: Select all
ProcedureDLL message(text.s)
MessageRequester("Test DLL",text,#MB_ICONINFORMATION)
ProcedureReturn 1
EndProcedure
This says we have a procedure called "message" and we pass a string to it. This string is then displayed in a MessageRequester and returns 1 if all is ok.
Now select "Compiler/Compiler" Options from the PB editor menu bar. Change the EXEC Format to "Shared DLL" and press OK.
Then select Compiler/Create Executable. Doesn't matter what you call it because after it is created it will be located in the PureBasic/Compilers Folder called PureBasic.DLL (you can manually rename this file to whatever you want)
Now start a new project and type this in:
Code: Select all
If OpenLibrary(0,"purebasic.dll")
If CallFunction(0,"_message","Hello World")=0
MessageRequester("Error","DLL Call Failed",#MB_ICONERROR)
EndIf
CloseLibrary(0)
EndIf
Now select "Compiler/Compiler" Options from the PB editor menu bar. Make sure the EXEC Format is "Windows" and press OK.
Then select Compiler/Create Executable to create your executable. Put your created EXE in the same folder as your DLL (or make sure your DLL is in the Windows/System Folder)
So what happens here is when you start the EXE, it opens the DLL which we have named "purebasic.dll".
Remember in your DLL we named the Procedure "message" ?
So we call this function using CallFunction and start our little procedure "message" and pass the string "Hello World".
It is very important to notice that when calling procedures inside a DLL created with PureBasic, you must start your procedure name with "_" ... so "message" becomes "_message"
So, if you call your procedure and all goes well, it will display your text in a MessageRequester and return the value of 1 (meaning success). If it returns 0 then something went wrong and it will tell you.
Hope this helps a little.
