FAO Mark, Passing vars to max DLL makes host crash

BlitzMax Forums/BlitzMax Programming/FAO Mark, Passing vars to max DLL makes host crash

Tom(Posted 2005) [#1]
**EDIT!**

Solved thanks to Peters hint on calling conventions and this article: http://www.codeproject.com/cpp/calling_conventions_demystified.asp

Adding '__stdcall' in my function declaration seems to work.

extern __stdcall char * bb_getCmd();

Cheers anyway!
Tom


snip-----------------------------------------------------

Hi,

I've been testing the idea of creating plugins for Lightwave3D using max (wild idea I know!)

Anyways, I can make my Max DLL functions callable from Lighwtave, and doing something like getting a simple string works fine so far, but if I try to pass ANYTHING to the DLL, Lightwave crashes *after* the function returns.

Here's the working code:
C
-
#include "lwserver.h"
#include "lwcmdseq.h"
   
extern char * bb_getCmd();

XCALL_( int )
Activate( long version, GlobalFunc *global, LWModCommand *local, void *serverData )
{
  if ( version != LWMODCOMMAND_VERSION ) return AFUNC_BADVERSION;

  char * cmd;
  cmd = bb_getCmd(); // Grab the CommandSequence from the DLL

  local->evaluate( local->data, cmd );
  return AFUNC_OK;
}
ServerRecord ServerDesc[] = {{ LWMODCOMMAND_CLASS, "Tutorial_Box1", Activate },{ NULL }}


BlitzMax
--------
Framework BRL.Basic
Import BRL.Retro

Import "include/C:\lw75sdk\include\*.h"
Import "C:\lw75sdk\source\libserver.a"
Import "C:\lw75sdk\source\servmain.c"
Import "C:\lw75sdk\source\box.c"

'This is needed just so LW knows this is a plugin
Function _mod_descrip() 'EXPORT
End Function

Function getCmd:Byte Ptr()"Win32" 'EXPORT
  Local cmd$ = "MAKEBOX <-1 -1 -1> <1 1 1> <5 5 5>" 'Create a 2x2x2 box with 5segments each side
  Return cmd.toCstring()
End Function

Here's the proof it works :)


Now, as a test of the problem, if I change bb_getCmd() to expect an Int in both C and Max

C
-
extern char * bb_getCmd(int i);
...
int i = 1234;
char * cmd;
cmd = bb_getCmd(i); // Grab the CommandSequence from the DLL
...

Max
---
Function getCmd:Byte Ptr(i:int)"Win32" 'EXPORT

Lightwave crashes when the function returns. I know that the Int is being passed, because if I have the bb_getCmd() function create a text file and print the value of 'i', it prints '1234' as expected.

I'm wondering if the garbage collector could be messing with any incoming vars?

b.t.w I've never been so impressed at the creation of a box before :P

Cheers
Tom


Difference(Posted 2005) [#2]
Is it calling convention? Have you tried leaving out "Win32" or replacing it with something else?