Page 1 of 2

Need help turning this code into DLL!

Posted: Sun Aug 17, 2014 3:08 pm
by Mythros
Hi all. I'm trying to create a DLL for Blitz3D of the function "MP_SaveMesh". I don't THINK I'm doing anything wrong, but then again, I've been wrong in the past. I would like to know when I create my DECLs file and call the function in Blitz3D, why it says "userlib not found", even though the DLL file is in the same folder as the Blitz3D code & the DECLs is tucked tightly in the userlibs folder.

Here's my DECLs:

Code: Select all

.lib "Save_X_Mesh.dll"

SaveXMesh(filename$, entity%) : "Save_X_Mesh"
And...

Here's my source:

Code: Select all

#TESTDLL = 0

CompilerIf #TESTDLL = 0

  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
  
  Prototype.i ProtoSave_X_Mesh(filename.s, entity.i)
  
  ProcedureDLL Save_X_Mesh(filename.s, entity.i)
    MP_SaveMesh(filename.s, entity.i)
  EndProcedure

  If OpenLibrary(0, "Save_X_Mesh.dll")
   
    SaveXMesh.ProtoSave_X_Mesh = GetFunction(0, "Save_X_Mesh")
    
    Save_X_Mesh(filename.s, entity.i)
    
  EndIf

CompilerElse
  
  Prototype

  If OpenLibrary(0, "Save_X_Mesh.dll") Or OpenLibrary(0, "Save_X_Mesh.so")
    CallFunction(0, "Save_X_Mesh", @filename.s, @entity.i)
  EndIf
    
CompilerEndIf
Thank You again! :)

~Mythros

Re: Need help turning this code into DLL!

Posted: Mon Aug 18, 2014 1:32 am
by Mythros
Can someone please help?

Thank You!

Mythros

Re: Need help turning this code into DLL!

Posted: Mon Aug 18, 2014 8:27 am
by Bananenfreak
I think no one can help you.
Your code is chinese or something similar...

Re: Need help turning this code into DLL!

Posted: Mon Aug 18, 2014 8:57 am
by applePi
Mythos, do your experiment with "SaveMesh" function because it is available natively in purebasic then if it fails then provide full source code for the dll and the full blitz3d code in the Code questions forum. there are many dll experts visiting Code forum. also many who have used Blitz3D before. i have used it before occasionaly but i forgot it completely. it is now freeware and the people can download it after registering for free from their site.

Re: Need help turning this code into DLL!

Posted: Mon Aug 18, 2014 2:51 pm
by Mythros
It shouldn't be in Chinese. I didn't write it in nothing BUT english...

Applepi, SaveMesh doesn't save animated, textured X Meshes does it?

Thanks!

Re: Need help turning this code into DLL!

Posted: Mon Aug 18, 2014 2:57 pm
by IdeasVacuum
Your code is chinese or something similar...
It's largely from the PB Help doc example.

Re: Need help turning this code into DLL!

Posted: Mon Aug 18, 2014 2:59 pm
by Mythros
Yea, it is. I'm still learning.

Re: Need help turning this code into DLL!

Posted: Mon Aug 18, 2014 3:02 pm
by Bananenfreak
@Mythros:
What I mean with chinese:
What is this AttachProcess(),... for?
Why CompilerIf?
Why Prototype in your DLL?
What is MP_SaveMesh()?
Why are you open your DLL in your DLL?

Your DLL should be like this:

Code: Select all

ProcedureDLL Save_X_Mesh(filename.s, entity.i)
    MP_SaveMesh(filename.s, entity.i)
EndProcedure
If you want to test your DLL with PB, you use this part of your code:

Code: Select all

Prototype.i ProtoSave_X_Mesh(filename.s, entity.i)

If OpenLibrary(0, "Save_X_Mesh.dll")
    SaveXMesh.ProtoSave_X_Mesh = GetFunction(0, "Save_X_Mesh")    
EndIf
Now you can use

Code: Select all

Save_X_Mesh(filename.s, entity.i)
in your program.

EDIT: ProtoSave_X_Mesh() must be defined. done ^^

Re: Need help turning this code into DLL!

Posted: Mon Aug 18, 2014 3:04 pm
by Mythros
Because for some strange reason, I like to use prototypes better than regular functions when it comes to DLLs. o.o The rest is from the documentation so as for AttachProcess(), I have no idea man. Ask the guide. LOL!

Re: Need help turning this code into DLL!

Posted: Mon Aug 18, 2014 3:14 pm
by Bananenfreak
You have to read more.
This means these 4 procedures names are reserved and can't be used by the programmer for other purposes.
I don“t know more about it, but it could be possible that you overwrite these functions...

Re: Need help turning this code into DLL!

Posted: Mon Aug 18, 2014 3:24 pm
by Mythros
What do you mean, "overwrote these functions" ?

Re: Need help turning this code into DLL!

Posted: Mon Aug 18, 2014 3:51 pm
by Mythros
Here's my whole DLL code.

I tested it and the function loaded fine, the filesize of the animated / textured x file is greater than 0, so I KNOW the mesh exists, and the function for some reason, still returns "0" on "MP_SaveMesh()" which means that it didn't save the mesh, even though the mesh was loaded...

Code: Select all

MP_Graphics3D(800, 600, 0, 2)

ProcedureDLL Save_X_Mesh(filename.s, entity.i)
  fipath.s = (GetCurrentDirectory()+filename.s)
  MessageRequester(fipath, fipath)
  saved.i = MP_SaveMesh(fipath.s, entity.i)
  MessageRequester(Str(saved), Str(saved))
  ProcedureReturn saved
EndProcedure

Prototype.i ProtoSave_X_Mesh(filename.s, entity.i)

;If OpenLibrary(0, "Save_X_Mesh.dll")
 ; SaveXMesh.ProtoSave_X_Mesh = GetFunction(0, "Save_X_Mesh")    
;;EndIf

Global fi.s = "models\dwarf\dwarf1.x"

MessageRequester(Str(FileSize(fi)), Str(FileSize(fi)))

Global ent.i = MP_LoadAnimMesh(fi)

saved = Save_X_Mesh("test1.x", ent)

While Not MP_KeyDown(1) And Not WindowEvent() = #PB_Event_CloseWindow
    
    MP_RenderWorld()
    MP_Flip()
    
  Wend

End
Thanks!

Mythros

Re: Need help turning this code into DLL!

Posted: Mon Aug 18, 2014 8:42 pm
by Mythros
Can someone please assist?

Thanks!

Mythros

Re: Need help turning this code into DLL!

Posted: Mon Aug 18, 2014 8:56 pm
by applePi
Mythos, don't think i am DLL mechanic , but i have made some experiments today.
you should compile the PB MP3D dll with dx9 Library subsystem since mp3d use dx9
Image

i don't understand your purpose, but here is a small PB mp3d dll to save a cube entity as mesh cube.x from inside a dll

dlltest.dll :

Code: Select all

ProcedureDLL Save_X_Mesh(*filename.String)
MP_Graphics3D(800, 600, 0, 2)
Mesh=MP_CreateCube() 

  fipath.s = (GetCurrentDirectory()+*filename\s)
  saved.i = MP_SaveMesh(fipath, Mesh)
  
EndProcedure
the calling purebasic program

Code: Select all

Define text.string
text\s="cube.x"
If OpenLibrary(0,"dlltest.dll")
CallFunction(0,"Save_X_Mesh",@text)
CloseLibrary(0)

EndIf

MessageRequester("finish", "cube.x created")
now if you want to know what procedures are there in some dll use this

Code: Select all

Define dll.l = OpenLibrary(#PB_Any,"dlltest.dll")
ExamineLibraryFunctions(dll)
For i = 1 To 20
NextLibraryFunction()
R.s = LibraryFunctionName()
Debug R
Next
FreeLibrary_(dll)
how you call this dll from Blitz3d ??

Re: Need help turning this code into DLL!

Posted: Mon Aug 18, 2014 9:32 pm
by IdeasVacuum
All components, libraries, and binaries are copyrighted by Fantaisie Software. The PureBasic license explicitly forbids the creation of DLLs whose primary function is to serve as a 'wrapper' for PureBasic functions.
It would be courteous to confirm with mpz that you can use his lib in Blitz3D.