News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Basic DLLing?

Started by Fluorescent, August 25, 2005, 03:01:10 AM

Previous topic - Next topic

Fluorescent

I was thinking about writing a DLL-file for some stuff I want to experiment with. But I wonder if I have to register the DLL for play basic to find it, or is it enough to put it in the same folder as the exe ??

empty

In case it is a standard DLL you could just place it in the exe folder. However, ActiveX/COM DLLs (the ones VB produces) are currently not supported.

Fluorescent

#2
Hmmm... I've created a MFC-Dll, used the wizard in Visual Studio 2003. But Playbasic claims that the DLL doesn't exist. Might there be something wrong with my dll or my pb-code ??

PlayBASIC Code: [Select]
PB_DLL = GETFREEDLL()

IF GETDLLEXIST("PB_DLLTest.dll") <> 1
PRINT "DLL Doesn't exist"
SYNC
WAITKEY
END
ENDIF

LOADDLL "PB_DLLTest.dll", PB_DLL

IF GETDLLCALLEXIST(PB_DLL, "Message") <> 1
PRINT "Can't find function"
SYNC
WAITKEY
END
ENDIF

PRINT "Messagebox"
CALLDLL(PB_DLL, "Message", 0)
SYNC
WAITKEY : WAITNOKEY
SYNC

DELETEDLL PB_DLL




kevin

#3
It's your usage of the GetDLLExist command.  As this function returns if this dll has been loaded into memory, not does it exist on disc.  

See-> GetDllExist always returns 0 (login required)



Fluorescent

AHA!
Then I think it might be a small error in the documentation that I have. Because the if-statement for GetDllExist is before  LoadDll.

kevin

#5
Nah, that's correct.  As it's checking to see if this DLL has been loaded previously (which it can't have been in the example)!.   so if it hasn't, you load it, if it has, you don't.. it's already in memory.

kevin

actually make sure your using PB1.088 ?

Fluorescent

Hmm I got it working. Although I'm using PB 1.08.

Can I pass Types to and from my DLL functions ??

empty

#8
QuoteHmm I got it working. Although I'm using PB 1.08.
The GETDLLEXIST works a bit different in 1.08.
While 1.08 returns 1 if a DLL has been loaded (or 0 if it hasn't), 1.088 PB returns the actual index number

QuoteCan I pass Types to and from my DLL functions ??
You can pass types to DLLs. They are passed as memory pointers.

Fluorescent

You dont' happen to have a neat example of passing a type to C++ ?? Passing single variables of int, float and string was no problem. But I want to be able to pass a Type with a mixture of variable types. I tried to create mytype in Play Basic
TYPE mytype
a
b#
c$
ENDTYPE


and in C++ I created a struct
struct mytype {
int a;
float b;
char *c;
};

But it didn't work. To my supprise it didn't crash either :-)

empty

#10
Make sure that in C++ the exported function takes the parameter like this:

void aTestFunction(mytype &thistype)

Fluorescent

Am I on the right track trying to use structs when passing Types to and from Play Basic and C++ DLL ??

When I use the &-operand on the parameter the program crashes. If I remove the &, the program runs and the string variable c$ that I've set to "MARTIN" becomes MZ and some sort of square that I can't reproduce.

I admit to not being the world's greatest Cpluspluser, particulary when it comes to having the C++ code interact with code from other languages.

empty

#12
QuoteI admit to not being the world's greatest Cpluspluser
Neither am I (being an old (object) pascal desciple and all ;) )

However, this works for me:
PlayBasic code
PlayBASIC Code: [Select]
Type TMyType
a, b#, c$
EndType

Dim myType as TMyType

myType.a = 42
myType.b# = 1.234
myType.c$ = "Hello"

LoadDll "test.dll", 1
CallDll 1, "testfunc", myType.TMyType
DeleteDll 1



C++ code

struct TType {
 int a;
 float b;
 char *c;
};

BOOL APIENTRY DllMain( HANDLE hModule,
                      DWORD  ul_reason_for_call,
                      LPVOID lpReserved
     )
{
  return TRUE;
}



void _stdcall testfunc(TType &myType) {
  int myInt = myType.a;
  float myFloat = myType.b;
  char *myChar = myType.c;

  char buffer[20];
  itoa(myInt, buffer, 10);
  MessageBox(0, buffer, "Test", 0);
  gcvt(myFloat,4,buffer);
  MessageBox(0, buffer, "Test", 0);
  MessageBox(0, myChar, "Test", 0);
}

Fluorescent

Dude! you make it look so easy ;-)

I didn't use the myType.TMyType in the function call!!