January 16, 2021, 09:16:56 PM

Author Topic: Closing DLL LIB  (Read 913 times)

Offline Hardcoal

  • Hero Member
  • *****
  • Posts: 531
  • nothing is personal
Closing DLL LIB
« on: December 29, 2020, 07:57:50 AM »
Hi.. is it possible to close a reload a dll lib after being called using LoadLibraryA?

Thanks
Lets make the world a better place

Offline col

  • Sr. Member
  • ****
  • Posts: 474
Re: Closing DLL LIB
« Reply #1 on: December 29, 2020, 12:02:10 PM »
Hi Hardcoal,

You can unload a dll using the FreeLibrary function.

https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-freelibrary
To be is to be perceived.

https://github.com/davecamp

Offline Hardcoal

  • Hero Member
  • *****
  • Posts: 531
  • nothing is personal
Re: Closing DLL LIB
« Reply #2 on: December 29, 2020, 12:10:11 PM »
i dont know how to add the freelibrary command..
can you post here please so i will learn next time how to call a new function? TY
Lets make the world a better place

Offline Hardcoal

  • Hero Member
  • *****
  • Posts: 531
  • nothing is personal
Re: Closing DLL LIB
« Reply #3 on: December 29, 2020, 12:13:31 PM »
Oh  i think i got it thanks :)

is this the way?

for some reason program shuts down after executing freelibrary.. though

?Win32

   Import "-lshell32"
   
   Extern "Win32"
      Function LoadIcon:Int(hWnd:Int, file$z, index:Int) = "ExtractIconA@12"
      Function SetClassLong:Int(hWnd:Int, nIndex:Int, dwNewLong:Int) = "SetClassLongA@12"
      Function SetWindowText:Int(hWnd:Int, lpString$z) = "SetWindowTextA@8"
      Function GetLastError:Int()
      Function FreeLibrary:Int(hModule:Int)
   End Extern
?
Lets make the world a better place

Offline col

  • Sr. Member
  • ****
  • Posts: 474
Re: Closing DLL LIB
« Reply #4 on: December 29, 2020, 12:28:44 PM »
Quote
LoadIcon:Int(hWnd:Int, file$z, index:Int) = "ExtractIconA@12"

This looks wrong to me, unless you meant to change the real function being called - bad idea to do that if that's the case.

Quote
for some reason program shuts down after executing freelibrary.. though
Have you tried:

Function FreeLibrary:Int(hModule:Int) = "FreeLibrary@4"
To be is to be perceived.

https://github.com/davecamp

Offline col

  • Sr. Member
  • ****
  • Posts: 474
Re: Closing DLL LIB
« Reply #5 on: December 29, 2020, 12:38:58 PM »
.
To be is to be perceived.

https://github.com/davecamp

Offline Hardcoal

  • Hero Member
  • *****
  • Posts: 531
  • nothing is personal
Re: Closing DLL LIB
« Reply #6 on: December 29, 2020, 02:16:41 PM »
i tried adding = "FreeLibrary@4"

I think there is no match between the freelibrary command and the LoadLibraryA
but its just a guess..
Lets make the world a better place

Offline Henri

  • Sr. Member
  • ****
  • Posts: 288
Re: Closing DLL LIB
« Reply #7 on: December 29, 2020, 04:10:32 PM »
Hi HC,

in NG you do not declare params size in bytes anymore (the @4 = 4 bytes part).

In theory you wouldn't need to add anything after function declaration, but in practice Windows api functions are already declared somewhere else, producing a duplicate declaration error.

You can navigate around this by ending your declaration with  ! . This means 'use this above all else'

Like:

Code: BlitzMax
  1. Extern "Win32"
  2.         Function FreeLibrary:Int(hModule:Byte Ptr) = "INT FreeLibrary(BYTE *)!"
  3. EndExtern
  4.  
  5. Local libShell:Byte Ptr = LoadLibraryA("shell32")
  6. If Not libShell Then Print "Error: No library loaded!"; End
  7.  
  8. Print "Shell loaded. Now closing.."
  9. Print FreeLibrary(libShell)
  10.  

-Henri

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

Offline Hardcoal

  • Hero Member
  • *****
  • Posts: 531
  • nothing is personal
Re: Closing DLL LIB
« Reply #8 on: December 29, 2020, 05:06:44 PM »
LoadLibraryA accepts Int and not Byte ptr so this produce an error

Compile Error: Unable to convert from 'Int' to 'Byte Ptr'
Build Error: failed to compile C:/Users/Danny/Documents/BLIde Framework/TMP/bmx_file.bmx
Lets make the world a better place

Offline Henri

  • Sr. Member
  • ****
  • Posts: 288
Re: Closing DLL LIB
« Reply #9 on: December 29, 2020, 05:17:25 PM »
Regular or NG ?

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

Offline Hardcoal

  • Hero Member
  • *****
  • Posts: 531
  • nothing is personal
Re: Closing DLL LIB
« Reply #10 on: December 29, 2020, 06:11:25 PM »
Regular
Lets make the world a better place

Offline Henri

  • Sr. Member
  • ****
  • Posts: 288
Re: Closing DLL LIB
« Reply #11 on: December 29, 2020, 06:39:21 PM »
Yeah, my example was for ng. For regular you just change the Byte Ptr to Int.

This isn't a language issue its a definition issue. Somewhere inside the source code of Blitzmax the LoadLibrary has been defined differently.
You could use the Byte Ptr version in regular as well and it would be totally valid if you defined it again to have return type of Byte Ptr..

In ng you can't use the Int version due to a fact that the Int is not 64-bit.

The FreeLibrary returns  a non zero value if it is successful.

Code: [Select]
Extern "Win32"
        Function FreeLibrary:Int(hModule:Int)
EndExtern
 
Local libShell:Int = LoadLibraryA("shell32")
If Not libShell Then Print "Error: No library loaded!"; End
 
Print "Shell loaded. Now closing.."
Print FreeLibrary(libShell)

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

Offline Hardcoal

  • Hero Member
  • *****
  • Posts: 531
  • nothing is personal
Re: Closing DLL LIB
« Reply #12 on: December 29, 2020, 06:57:14 PM »
Thanks Henry..

Ok it appears that its some error concern my own dll lib..
I made a test DLL lib.. and i guess im doing something wrong because when i try to close it than it crashes..

this is all i tested.. and compiled with bmk.exe makelib

henry your example worked well.. which indicates an erro on my own dll experiment

Code: [Select]
Strict

Function Multiply (A, B)
  Return A * B
EndFunction

Function Minus (A, B)
  Return A - B
EndFunction

Function RotateElement(Element, X, Y, Z)
Return X + Y + Z
End Function

Lets make the world a better place

Offline Dabz

  • Sr. Member
  • ****
  • Posts: 348
  • Is it cos I is ginger?
    • dabzy.co.uk
Re: Closing DLL LIB
« Reply #13 on: December 29, 2020, 07:16:53 PM »
Quote
I made a test DLL lib.. and i guess im doing something wrong because when i try to close it than it crashes.

In my little Oculus faffy DLL, I have something like this:-

oculusDLL.h
Code: [Select]
#pragma once

#ifdef OCULUSDLL_EXPORTS
#define OCULUSDLL_API __declspec(dllexport)
#else
#define OCULUSDLL_API __declspec(dllimport)
#endif

extern "C" OCULUSDLL_API int InitOculus(int(*BM_VRLoop)());

Then in my oculusDLL.cpp file, I have:-

Code: [Select]
int InitOculus(int (* BM_VRLoop)())
{
// Initializes LibOVR, and the Rift
ovrInitParams initParams = { ovrInit_RequestVersion | ovrInit_FocusAware, OVR_MINOR_VERSION, NULL, 0, 0 };
ovrResult result = ovr_Initialize(&initParams);
VALIDATE(OVR_SUCCESS(result), "Failed to initialize libOVR.");

VALIDATE(DIRECTX.InitWindow(NULL, L"OculusDLL"), "Failed to open window.");
BMVRLoop = BM_VRLoop;
DIRECTX.Run(MainLoop);

ovr_Shutdown();
return(1);
}

Which, obviously in my Blitzmax NG code I have:-

Code: [Select]
Global InitOculus:Int(pointer:Int Ptr)"Win32"
Local VRLoopPointer:Int() = VRLoop 'BltzmaxNG callback function used by Oculus

Global lib:Byte Ptr = LoadLibraryW(CurrentDir$()+"/OculusDLL.dll")

If lib Then
InitOculus = GetProcAddress(lib, "InitOculus")
Else
Notify "OculusDLL not found.!"
End
EndIf


InitOculus(VRLoopPointer)

'Worker function, this will be handed over to Oculus as a callback, so all action in BM
'must be orchestraded in here somewhat.
Function VRLoop:Int()

EndFunction

The DLL was setup using the VS2017 wizard, and the project still contains the auto-generated dllmain.cpp file which I've just ignored and it all performs lovely, even on shutting it all down.

Dabz

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

Offline Hardcoal

  • Hero Member
  • *****
  • Posts: 531
  • nothing is personal
Re: Closing DLL LIB
« Reply #14 on: December 29, 2020, 07:41:18 PM »
This whole dll thing is weird..

I made a Code without function.. and it ran when i loaded the lib..

so its not only running functions, it runs anything that is in the bmx file..

i just need to know why it crashes when i do closelibrary..

its something i need to write in my bmx dll source.. which i dont know what it is
Lets make the world a better place

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal