Closing DLL LIB

Started by Hardcoal, December 29, 2020, 07:57:50

Previous topic - Next topic

Derron

Reading your code I assume you compile with "legacy" ?

Asking as you return something in a function without defining the return type.


@ DLL
Yes it is executing everything in the file ... this is why you need to wrap stuff in functions.

Also something of importance is that (at least in NG) the GC will not work within the DLL code until you called "InitBRL()" once.

So my BlitzMax-DLL (which controls a 3rd party DLL and wraps stuff for a example client written in BlitzMax NG) uses stuff like this:
Code (Blitzmax) Select

SuperStrict

Framework brl.standardIO

Global versionPtr:Byte Ptr = "My DLL. Version 0.1 [2020/12/29]".ToCString()

'Retrieve string with name, version and build date
'returns: Pointer to a constant string
Function MyDLL_GetVersion:Byte Ptr() Export "Win32"
InitDLL()
Return versionPtr
End Function


Global dllInitDone:Int = False
Function InitDLL:Int()
If dllInitDone Then Return True

InitBRL()
dllInitDone = True
Return True
End Function


Using the DLL:
Code (BlitzMax) Select

SuperStrict

Framework Brl.StandardIO
Import pub.win32

Function LoadLib:Byte Ptr(url:String)
Local handle:Byte Ptr = LoadLibraryW(url)
If Not handle
Print "Failed to load library ~q"+url+"~q"
'Notify "Failed to load library ~q"+url+"~q"
End
Else
Print "Loaded library ~q"+url+"~q"
EndIf
Return handle
End Function

Print "Loading DLLs"
Global dllHandle:Byte Ptr = LoadLib("mydll.dll")

Global MyDLL_GetVersion$z() = GetProcAddress(dllHandle, "MyDLL_GetVersion")

Print "myDLL version: " + MyDLL_GetVersion()


This also shows how to transport a string from the DLL to the client in a way that the (BlitzMax) client does the necessary memfree of this string afterwards.


I do not have to "close" the file - but of course I need to handle closing stuff like open device connections (my DLL controls a DLL which communicates with some hardware, a second DLL I wrote communicates directly with some hardware to set it up and run measurements).

Instead of replacing DLLs when doing mockups I have my DLL do the mockup stuff for me - so I cannot say if replacing the DLLs works flawlessly or needs some additional steps not done yet in NG for you.


bye
Ron

col

@Hardcoal
For clarity... what language is the dll written in?
https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

Hardcoal

this is a dll made on blitzmax.. i thought its clear :)
Code

Hardcoal

The reason i want to close the library is because i want to reload it after making changes..
none of the examples above worked.. its still gets stuck..

ill keep trying to figure it out lol :)
Code

Hardcoal

#19
Ok i just realized that it doesnt crash, it keeps playing the code.. until it reaches the command Until... which suppose to make it loop
and than it ignores it lol..

but only in my Dll Case.. not on external dlls.. which is not made with blitzmax



Strict

Import pub.Win32

?Win32

'Import "-lshell32"
   
Extern "Win32"
Function FreeLibrary:Int(hModule:Int)
End Extern

?



Graphics (800, 600)

Global WantedLib:String = "DllsToBeCompiled/MyDllLib.dll"

Local CurrentText:String

#danny
Repeat

     Cls

Local A

If KeyHit(KEY_1) Then
  Local Library
 
  DrawText ("Reading Lib", 50, 50)

Library = LoadLibraryA (WantedLib)
If Not Library Then LibError(WantedLib)

'Local PrintSomething:String() = GetProcAddress (Library, "PrintSomething")
'Print PrintSomething()
'PrintSomething = Null

FreeLibrary(Library)

Delay (100)

Print "DoneFree"

A = 1

  End If

  DrawText("Press 1 to load Lib", 10, 10)
  DrawText(CurrentText, 10, 30)
 
  If A = 1 Then Print "A=1"
 
  Flip
 
Until KeyDown(KEY_ESCAPE)

'--------------------------------'

Function LibError(LibURL:String)
Notify "Library not Loaded or not Found:   " + LibURL
End Function

Code

Hardcoal

Ok i managed to make it work.. after i do freelibrary i just load it again..
but before loading i did a compile function so.. it compiles the dll and when i reload it, its already modified..

so now i can modify a dll while the program is running and get different outcome.

lets see if i can put it into a good use
Code

DruggedBunny

Quote from: Henri on December 29, 2020, 16:10:32
You can navigate around this by ending your declaration with  ! . This means 'use this above all else'
Ohhh, that's interesting!

I've run into this Win32 duplicate definition error so many times in NG and never worked out how to get around it! Seems like a bug that it has the Win32 defines but doesn't let you use them.

Henri

Its not a bug as such but a consequence of transpiling BMax code to C code as I understand it (in C those definitions are in a header file that are then included in the c-file where they are needed). In order for external function to be available in the scope of your app it needs to be included/imported or defined in your app file. Some of the Windows API function are already defined in Blitzmax modules like LoadLibraryW, which is why you do not have to define again (in my example I use it but do not define it as the Framework keyword is omitted thereby all modules are imported).

If I need external Windows function I usually define them in a file and import that file in my main app file (or whatever file needs to use them) and sometimes you may find the needed function in pub.mod/win32.mod - folder and you just need to import one of those.

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Hardcoal

Henry..

like i said im making my own dll's and i needed to solve the problem which i have..
and thats all what matters to me..

maybe you didnt understand what i was trying to do..

ill post the code in the future when its ready
Code

Henri

Its okay, I was just answering to DruggedBunny.

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Hardcoal

Code

Hardcoal

#26
Ok Ive managed to recompile a Type while the program is running..
and reuse it. making changes.. to it.. without stopping the run process.

but I need one more step to make it what i really desire..
Ill be surprised if it actually works..


Type Example_Type

    Method Play()
        Print "Testing"
    End Method

End Type

Code

Hardcoal

#27
I made a little DLL Maker..
Its basic.. so dont expect too much

https://1drv.ms/u/s!ArSvOuhm7L3kr5c-rKPPA2hc7miivw?e=VaP2vl
Code

Dabz

Seems to work! :)

Though, for anyone else that wonders why a DLL doesnt turn up and only a def file, after first run, you'll need to change the "D:\PORTABLE\BlitzMax\bin" bit in the Compiler.bat file that appears, to the path to the blitzmax/bin folder on your machine, then, execute the bat file as normal and everything will appear.

Dabz
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 16Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit

Hardcoal

#29
ah lol right I forgot about that TY Dabz..

I still didnt implement a process to ignore Remarks when reading the Source BMX File. for example.. ( ' or Rem, End Rem )
and other types of parsing error preventers are still not Added..

but by the complaints ill get ill fix errors and make improvements..

Thats why I called it version 0.1

much easier than actually thinking :)

[Hour Later] Ok I fixed it and improved it..
waiting for more remarks..
If you like what I did, Ive got more surprises
Code