Gamestudio Links
Zorro Links
Newest Posts
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
LPDIRECT3DCUBETEXTUR
E9

by Ayumi. 04/12/24 11:00
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 04/11/24 14:56
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (firecrest, AndrewAMD), 387 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
11honza11, ccorrea, sakolin, rajesh7827, juergen_wue
19045 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
DLL import works - then "Error 111: Crash in script" #465172
04/08/17 13:51
04/08/17 13:51
Joined: Feb 2017
Posts: 1,725
Chicago
AndrewAMD Online OP
Serious User
AndrewAMD  Online OP
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago
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?


Attached Files before.PNGafter.PNG
hellozorro4.c (67 downloads)
zorro script
hellozorro.zip (54 downloads)
dll file
Last edited by AndrewAMD; 04/10/17 00:23.
Re: DLL import works - then "Error 111: Crash in script" [Re: AndrewAMD] #465181
04/10/17 11:11
04/10/17 11:11
Joined: Feb 2017
Posts: 1,725
Chicago
AndrewAMD Online OP
Serious User
AndrewAMD  Online OP
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago
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.

Last edited by AndrewAMD; 04/10/17 11:35.
Re: DLL import works - then "Error 111: Crash in script" [Re: AndrewAMD] #465182
04/10/17 11:38
04/10/17 11:38
Joined: Feb 2017
Posts: 1,725
Chicago
AndrewAMD Online OP
Serious User
AndrewAMD  Online OP
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago
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.

Last edited by AndrewAMD; 04/10/17 11:38.
Re: DLL import works - then "Error 111: Crash in script" [Re: AndrewAMD] #465184
04/10/17 14:12
04/10/17 14:12
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
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

Re: DLL import works - then "Error 111: Crash in script" [Re: jcl] #465187
04/10/17 14:48
04/10/17 14:48
Joined: Feb 2017
Posts: 1,725
Chicago
AndrewAMD Online OP
Serious User
AndrewAMD  Online OP
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago
Originally Posted By: jcl
The manual has a search function.
So it does.

Also, thanks for the clarification.


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1