DLL import works - then "Error 111: Crash in script"

Posted By: AndrewAMD

DLL import works - then "Error 111: Crash in script" - 04/08/17 13:51

First of all, this works in zorro script alone (zorro 1.54.5):

Code:
void hellozorro (char* message)
{
	strcpy(message, "Hello, Zorro!");
	return;
}

void nl()
{
	printf("\n");
	return;
}

void dothis()
{
	char message[101];
	
	hellozorro(message);printf("%s",message); nl();
	strcpy(message,"test1"); printf("%s",message); nl();
	
	hellozorro(message);printf("%s",message); nl();
	strcpy(message,"test2"); printf("%s",message); nl();
	
	hellozorro(message);printf("%s",message); nl();
	strcpy(message,"test3"); printf("%s",message); nl();
	
	return;
}

int main()
{
	dothis();
	printf("We made it to the end!"); nl();
	return(0);
}


My objective is to replace the first function with a DLL (as a learning exercise).

I built a dll in VC++2017 with this in the main file:

Code:
#include "stdafx.h"
#include <string>
#define DLLEXPORT extern "C" __declspec(dllexport)

DLLEXPORT void hellozorro(char* output)
{
	strcpy(output, "Hello, Zorro!");
	return;
}


In zorro, I made hellozorro.h:

Code:
#include <windows.h>
void WINAPI hellozorro(char*);
API(hellozorro,hellozorro)


Finally, my zorro script looks like this:

Code:
#include <hellozorro.h>

void nl()
{
	printf("\n");
	return;
}

void dothis()
{
	char message[101];
	
	hellozorro(message);printf("%s",message); nl();
	strcpy(message,"test1"); printf("%s",message); nl();
	
	hellozorro(message);printf("%s",message); nl();
	strcpy(message,"test2"); printf("%s",message); nl();
	
	hellozorro(message);printf("%s",message); nl();
	strcpy(message,"test3"); printf("%s",message); nl();
	
	return;
}

int main()
{
	dothis();
	printf("We made it to the end!"); nl();
	return(0);
}


Outputs attached. I get an "Error 111: Crash in script". If I'm lucky, I'll even crash Zorro too at random!

Here's what I tried, to no avail:
  • Switch between /MD and /MT build modes
  • Rebuild solution in VC++2013.
What am I doing wrong? Also, how am I supposed to diagnose this?




Description: zorro only output
Attached picture before.PNG

Description: zorro with DLL output
Attached picture after.PNG

Description: zorro script
Attached File
hellozorro4.c  (70 downloads)

Description: dll file
Attached File
hellozorro.zip  (54 downloads)
Posted By: AndrewAMD

Re: DLL import works - then "Error 111: Crash in script" - 04/10/17 11:11

I found the solution here: Use a function from an external DLL

Originally Posted By: Ch40zzC0d3r
Stop using stdcall in your typedef. The lite-c compiler may not know any other callingconventions then cdecl so stack is cleaned up 2 times (by caller and callee) and will crash you.
Try cdecl.


before:
Code:
void WINAPI hellozorro(char*);

after:
Code:
void __cdecl hellozorro(char*);


This works like a charm.

Conclusion: WINAPI is defined as __stdcall in windows.h. stdcall is not compatible with my DLL for reasons unknown. Instead, use cdecl.
Posted By: AndrewAMD

Re: DLL import works - then "Error 111: Crash in script" - 04/10/17 11:38

Can someone clarify when I would need __stdcall (WINAPI) and when I would need __cdecl? I don't see any mention of cdecl in the Zorro manual.
Posted By: jcl

Re: DLL import works - then "Error 111: Crash in script" - 04/10/17 14:12

The manual has a search function. Normally you use WINAPI for calling Pascal-style functions, as in the Win32 API, and you use nothing (__cdecl is default) for calling C style functions.

http://manual.zorro-project.com/function.htm
Posted By: AndrewAMD

Re: DLL import works - then "Error 111: Crash in script" - 04/10/17 14:48

Originally Posted By: jcl
The manual has a search function.
So it does.

Also, thanks for the clarification.
© 2024 lite-C Forums