dev-C++ dll howto

Just starting out? Need help? Post your questions and find answers here.
kinglestat
Enthusiast
Enthusiast
Posts: 746
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

dev-C++ dll howto

Post by kinglestat »

Folks,

I have some C functions I want to use in PB 4. But any dll I make I get invalid memory access.

this is a simple C code


#define API __stdcall

DLLIMPORT void API helloworld ( void )
{
MessageBox (0, "Hello World from DLL!\n", "Hi", MB_ICONINFORMATION);
}

and in PB

OpenLibrary(0, "arrays.dll")
helloworld = GetFunction(0, "helloworld")
CloseLibrary(0)


CallCFunctionFast(helloworld)



any ideas what I am doing wrong ?


cheers

KingLestat
Fred
Administrator
Administrator
Posts: 18169
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Did you checked than "helloword" pointer isn't null ? And you need to use CallFunctionFast() if your function is STDCALL.
kinglestat
Enthusiast
Enthusiast
Posts: 746
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

Post by kinglestat »

no pointer is not null, and I tried with callcfunctionfast with same results
kinglestat
Enthusiast
Enthusiast
Posts: 746
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

Post by kinglestat »

fixed problem - nothing wrong with the C code
notived the debate in my previous post - mainly freeing the dll before calling the func.....yes pretty obvious....yes pretty dumb but hey it now works

cheers and thanks for all the fish

KingLestat
johnfinch
User
User
Posts: 45
Joined: Thu May 11, 2006 1:45 am
Location: florida
Contact:

Post by johnfinch »

Could you show me how you called it correctly from PB. I don't understand freeing the DLL you mention? I am very intersted. Thanks.
Leopard-parallels-XP-Vista
KarLKoX
Enthusiast
Enthusiast
Posts: 681
Joined: Mon Oct 06, 2003 7:13 pm
Location: France
Contact:

Post by KarLKoX »

"Qui baise trop bouffe un poil." P. Desproges

http://karlkox.blogspot.com/
johnfinch
User
User
Posts: 45
Joined: Thu May 11, 2006 1:45 am
Location: florida
Contact:

Post by johnfinch »

Thanks!
Leopard-parallels-XP-Vista
johnfinch
User
User
Posts: 45
Joined: Thu May 11, 2006 1:45 am
Location: florida
Contact:

Post by johnfinch »

I could not have asked for a better example! Everything I ever wanted to know about DLL calling including C source. Many thanks for this KarLKoX.
Leopard-parallels-XP-Vista
kinglestat
Enthusiast
Enthusiast
Posts: 746
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

Post by kinglestat »

this is my test code with some forum help

OpenLibrary(0, "msvcrt.dll")
sprintf = GetFunction(0, "sprintf")
CloseLibrary(0) <-- this should not be here

OpenLibrary(0, "arrays.dll")
helloworld = GetFunction(0, "helloworld")
;CloseLibrary(0)



a = 5
n = @"A: %d"

buffer.s = Space(50)

CharsWritten = CallCFunctionFast(sprintf, buffer, n, a)
CallCFunctionFast(helloworld)


Debug buffer
Debug CharsWritten
Debug Len(buffer)


CloseLibrary(0)
Poplar
User
User
Posts: 16
Joined: Sun Apr 30, 2017 12:27 pm

Re: dev-C++ dll howto

Post by Poplar »

In pb V5.60:

Code: Select all

EnableExplicit
Define lib.l, a.i, n.s, buffer.s, CharsWritten.i
lib = OpenLibrary(#PB_Any, "msvcrt.dll")
PrototypeC.i ProtoSprintf(Buffer.s, Format.s, Arg1.l = 0, Arg2.l = 0, Arg3.l = 0, Arg4.l = 0)
Global sprintf.ProtoSprintf = GetFunction(lib, "swprintf")
CloseLibrary(lib)

a = 5
n = "About: %d"

buffer = Space(50)

CharsWritten = sprintf(buffer, n, a)

Debug buffer
Debug CharsWritten
Debug Len(buffer)
__________________________________________________
Code tags added
17.06.2017
RSBasic
Post Reply