Page 1 of 1

dev-C++ dll howto

Posted: Sat Dec 30, 2006 9:26 pm
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

Posted: Sat Dec 30, 2006 10:14 pm
by Fred
Did you checked than "helloword" pointer isn't null ? And you need to use CallFunctionFast() if your function is STDCALL.

Posted: Sun Dec 31, 2006 12:03 am
by kinglestat
no pointer is not null, and I tried with callcfunctionfast with same results

Posted: Sun Dec 31, 2006 12:20 am
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

Posted: Sun Dec 31, 2006 7:42 pm
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.

Posted: Mon Jan 01, 2007 2:22 am
by KarLKoX

Posted: Mon Jan 01, 2007 2:28 am
by johnfinch
Thanks!

Posted: Mon Jan 01, 2007 2:54 am
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.

Posted: Mon Jan 01, 2007 10:05 am
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)

Re: dev-C++ dll howto

Posted: Sat Jun 17, 2017 4:28 am
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