What's wrong with this DLL?

BlitzMax Forums/BlitzMax Programming/What's wrong with this DLL?

JoshK(Posted 2007) [#1]
When 3D World Studio loads this plugin, a notification for "PluginClass" comes up, then the program crashes on the MemCopy line.

The current version of 3D World Studio was written in BlitzPlus.

Here is the source:
Framework brl.system

Strict

Const PLUGIN_MESHLOAD=8

Function PluginClass(inbuffer:Byte Ptr,insize:Int,outbuffer:Byte Ptr,outsize:Int) "win32"
	GCEnter()
	Notify "PluginClass"
	MemCopy outbuffer,Int Ptr PLUGIN_MESHLOAD,4
	Notify "OK"
EndFunction

Function PluginDescription(inbuffer:Byte Ptr,insize:Int,outbuffer:Byte Ptr,outsize:Int) "win32"
	GCEnter()
	Notify "PluginDescription"
	Local s$,cs:Byte Ptr
	s="Load Leadwerks models"
	cs=s.tocstring()
	MemCopy outbuffer,Varptr s,s.length+1
	MemFree cs
EndFunction

Function PluginFileExtension(inbuffer:Byte Ptr,insize:Int,outbuffer:Byte Ptr,outsize:Int) "win32"
	GCEnter()
	Notify "PluginFileExtension"
	Local s$,cs:Byte Ptr
	s="mdl"
	cs=s.tocstring()
	MemCopy outbuffer,Varptr s,s.length+1
	MemFree cs
EndFunction

Function PluginMeshLoad(inbuffer:Byte Ptr,insize:Int,outbuffer:Byte Ptr,outsize:Int) "win32"
	GCEnter()
	Notify "PluginMeshLoad"
EndFunction


Here is a similar plugin written in PureBasic:
;Indicate what the plugin does
ProcedureDLL PluginClass(*inbuffer,insize,*outbuffer,outsize)
  PokeL(*outbuffer,#PLUGIN_MESHLOAD)
EndProcedure

;Text to describe plugin
ProcedureDLL PluginDescription(*inbuffer,insize,*outbuffer,outsize)
  PokeS(*outbuffer,"Load Leadwerks meshes.")
EndProcedure

;Extension for program to use
ProcedureDLL PluginFileExtension(*inbuffer,insize,*outbuffer,outsize)
  PokeS(*outbuffer,"mesh")
EndProcedure



Azathoth(Posted 2007) [#2]
Why are you creating a C-String if you're not using it? You should be using MemCopy outbuffer, cs, s.length+1

Also your first memcopy is reading the contents at address 8, it should be something like: Int Ptr(outbuffer)[0]=PLUGIN_MESHLOAD


JoshK(Posted 2007) [#3]
Okay, that is all working.

My main program loads a dll each time it is needed, and then frees it. The program crashes when I free the BlitzMax-made dll. How can I prevent this?


Azathoth(Posted 2007) [#4]
I haven't used Blitzmax all that much for making DLLs and there seems to be little documentation on it.


Dreamora(Posted 2007) [#5]
The DLL support is still not officially in and it might be that the GC just didn't get the needed modifications to work like this (dynamic unloading while it is potentially still running)


You could try to create a Stop function that calls End to shut the GC down forcefully.