Loading a dll from memory

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
piccaso
Posts: 27
Joined: Apr 05, 2006 6:44

Loading a dll from memory

Post by piccaso »

By using this library.

I've compiled the c library into a static lib because i'm not good in porting
things, but its probably possible in fbc too.

Here is an easy example which shows how it's done.

Code: Select all

#Include Once "MemoryModule/libMemoryModule.bi"

' Created by v1ctor's bin2bas, original Source of 
' the dll found in "TestDll_src.bas"
#Include Once "TestDll.bas"

Dim As HMEMORYMODULE hDll
Dim As Integer iRval

' Prototype of the Procedure 
Dim zStrPrint As Function Cdecl (zStr As ZString ptr) As Integer

' LoadLibrary
hDll = MemoryLoadLibrary(@TestDll_data(0))
If hDll = 0 Then
	Print "Error in MemoryLoadLibrary() Call"
	Sleep : System(1) 
EndIf

' GetProcAddress
zStrPrint = MemoryGetProcAddress(hDll,"szStringPrint")
If zStrPrint = 0 Then
	Print "Error in MemoryGetProcAddress() Call"
	Sleep : System(1)
EndIf

' Call It
iRval = zStrPrint(StrPtr("Test It")) ' Displays the zString and
Print "iRval =";iRval                ' returns the len() of it

' FreeLibrary
MemoryFreeLibrary(hDll)

Sleep
The static lib and everything needed to build the example can be
found in this archive (MemMod.zip).

Happy studying :)
Last edited by piccaso on May 08, 2007 17:35, edited 1 time in total.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

Looks neat =)
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

I compiled the memory lib loader self as a library
but this version will work only with realy simple types of DLL's
and with a short number of exported function.
if i use any COM interfaces in the dll MemoryLoadLibrary will crash.
same with your static version.

by the way the COM interfaces as file library loaded with
DyLibLoad() works without any trouble.

Joshy
piccaso
Posts: 27
Joined: Apr 05, 2006 6:44

Post by piccaso »

since the os isnt aware of the module there are many things can that go wrong.
Like the HINSTANCE passed to DllMain is just an address in memory and windows' api's dont know what to do with it...

And there is no magic inside so COM will never work with this :)

but there are some alternatives you could try:
Virtual PE - the example is for loading exe's but look inseide the zip...
Dll to Lib - not free but supports COM
oyster
Posts: 274
Joined: Oct 11, 2005 10:46

Post by oyster »

where can I get the code for fb? thanx?
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Post by voodooattack »

I remember I ported this before: http://www.freebasic.net/forum/viewtopi ... 948#104948

I never put it up in Tips & Tricks though.. :)
piccaso wrote:since the os isnt aware of the module there are many things can that go wrong.
Like the HINSTANCE passed to DllMain is just an address in memory and windows' api's dont know what to do with it...

And there is no magic inside so COM will never work with this :)
Yeah, but this lib is more for "Quick and dirty" implementations, nothing complex, but as long as you know what you're doing all should go well.. ;)
sanhen
Posts: 12
Joined: Jan 12, 2009 17:51

Post by sanhen »

Download failure. I now ask where to download?
Cherry
Posts: 358
Joined: Oct 23, 2007 12:06
Location: Austria
Contact:

Post by Cherry »

Post Reply