Trying to mimic a specific DLL interface

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by morduun.

Having a little trouble with this, and my sorry newbie self can't quite work out the details. I'm close, I think, but I'm still at the 'no cigar' stage.'

Short story: I'm looking to use PB to produce DLL functionality for Blitz Basic. Blitz requires a very specific interface, as shown here in the C function prototype:

Code: Select all

_declspec(dllexport) int _cdecl my_dll_func( const void *in,int in_size,void *out,int out_sz );
As far as I can tell, the beginning pieces are unnecessary, but the arguments are key, and that's where I'm getting messed up. I have gotten as far as this in PB:

Code: Select all

ProcedureDLL bbdll (*BankIn.l, In_Size.l, *BankOut.l, Out_Size.l)
  
  in$ = "My In Size = " + Str(In_Size)
  MessageRequester("I'm a DLL", in$, #PB_MessageRequester_Ok)

EndProcedure
I am, in fact, able to successfully call the _bbdll procedure from within BB, and the message box pops up as expected, but the In_Size I'm getting is definitely not an expected value, and something tells me it must have to do with the way I'm declaring my arguments. I've tried changing the type of the BankIn/BankOut vars to both byte and string pointers (bytes work, string doesn't, predictably), but I still seem to have the same problem, so I'm guessing there's a nuance I'm missing.

A nudge in the right direction would be greatly appreciated. :wink:

Three may keep a secret, if two of them are dead.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

purebasic use an unlogical way to pass paramater to the functions which does a reverse when called from a DLL. It will be fixed in v3.0, as it's annoying. Anyway, you just have to reverse like this:

(*BankIn.l, In_Size.l, *BankOut.l, Out_Size.l) -> (*BankIn.l, Out_Size.l, *BankOut.l, In_Size.l)

Rules to follow:

Argument 1 always stay Argument 1.

after for a 6 arg functions:

arg1, arg6, arg5, arg4, arg3, arg2, arg1.





Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by morduun.

Fred,

Many thanks for having a look, that will definitely demystify things later on. Unfortunately I'm still not getting proper results; the results I'm getting look suspiciously like a memory address instead of a generic long int, but when I investigated that possibility, the contents of the mem address always ended up being 0, so that's a dead end.

I'll keep plugging away; I'm certain it's my implementation and I just don't have enough flight time logged to see it yet. Maybe I can beat it into my head with this nice wall over here. :wink:

Three may keep a secret, if two of them are dead.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by morduun.

There's another possibility that I've just become aware of with this. The C prototype declaration specifies the _cdecl option, which means (I'm told) it expects c-like dll calling conventions. If PB didn't support this sort of call, it could explain why argument values are getting munged up. Is PB's default behaviour to support c-like dll calls, is there an option, or is this currently unsupported?

Three may keep a secret, if two of them are dead.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

The difference between "_cdecl" and "_stdcall" (WinAPI)
is that you (the caller) has to clean the stack if you
use "_cdecl".
"_stdcall" clears/pops the stack itself.

cya,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Please post the whole code, so I could take a look to it.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by morduun.

Erm... what I wrote in the first example is everything I have; all I've changed is the order of the arguments to match the order you've said is necessary. I've just been working with a really basic example to try to successfully pass an argument. The C prototype does have an 'extern C' prepended to it in the Blitz example, but aside from that it's listed as a copy-paste straight from the help file.

Three may keep a secret, if two of them are dead.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Oups, in fact the parameters are fully reversed when passed to a procedure (hum, the other style is for internal PureBasic function..). The correct line for you is:

(*BankIn.l, In_Size.l, *BankOut.l, Out_Size.l) -> (Out_Size.l, *BankOut.l, In_Size.l, *BankIn.l)



Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by morduun.

Ah! I think that's got it! I'm now getting numbers I expect for the long ints. Thanks a mil, Fred, I couldn't have worked that out without you! :)

Three may keep a secret, if two of them are dead.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by morduun.

Final update: everything's working perfectly. If anyone's interested here I'll be happy to post a code snippet for a working interface to Blitz. I've already done so on the BB forums, so hopefully we'll see a little more cross-pollination around here. :) Thanks again Fred!

Three may keep a secret, if two of them are dead.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by mystik.

Great, I got it working too now :)

Thanks Fred and morduun

Steve Smith.



Edited by - Mystik on 30 January 2002 09:03:25
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Hehe :). I simply hope than you willn't do a simple wrapper for all PureBasic commands and call them on the Blitz side :). DLL support is NOT to intented to do that.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

It was your idea, Fred... :wink:

Edited by - Danilo on 30 January 2002 09:30:37
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by mystik.

Fred: No Blitz.lib for Purebasic then :)

Steve.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by morduun.

LOL! Nah, I don't think we're going to just wrap up all those cool PB commands and suck 'em up in BB. A few much-needed ones, sure, but now that you've suckered us into using it in the first place, I'm sure you'll find us doing a lot more in unadulterated PB. I for one love having a nice windows rapid app designer that doesn't come with VB's overhead and can think of LOTS of things I'll be using it for. Sneaky, sneaky Fred!

Three may keep a secret, if two of them are dead.
Post Reply