SRL Archive - OSR Advanced Scripting Tutorials

C++ Plugins!


BenLand100

2007-07-06 21:54:04

Anyway, I was trying to make a c++ plugin for scar, and it didn't work. So I went through the trouble of making a plugin that would manualy inject a c++ dll into scar, only to find out that I had messed up an address for a pointer… Needless to say its easier than memory injection :) Ask questions, etc.

#include <windows.h>
#define Export _declspec(dllexport)
#define DelphiExport extern "C" Export

HINSTANCE inst;

Export BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID checks) {
switch(reason) {
case DLL_PROCESS_ATTACH:
//Do you're init here, but don't change anything else (besides cleanup)
inst = instance;
case DLL_THREAD_ATTACH:
return true;
case DLL_PROCESS_DETACH:
//Do you're cleanup here, but don't change anything else (besides init)
FreeLibrary(inst);
case DLL_THREAD_DETACH:
return true;
}
return false;
}

//Add in your functions

DelphiExport void __stdcall Test(void) {
//Shows a message box.
MessageBox(NULL, "Testing 1 2 3", "DLL", MB_OK);
}

//Export name changed in def file
DelphiExport int __stdcall GetFunctionCount(void) {
return 1; //Change depending on number to export
}

//Export name changed in def file
DelphiExport int __stdcall GetFunctionInfo(int x, void*& address, char*& def) {
switch (x) {
case 0:
address = GetProcAddress(inst,"_Test@0"); //Remember C naming scheme?
strcpy(def,"procedure DllTest;"); //Have to strcpy, how you want it to be called
return x;
}
return -1;
}
Plus the REQUIRED def file:
LIBRARY    ScarPlugin
EXPORTS
GetFunctionInfo = _GetFunctionInfo@12
GetFunctionCount = _GetFunctionCount@0

YoHoJo

2007-07-10 16:54:54

Muahah No One Cares Ben!

Santa_Clause

2007-07-11 04:30:11

Maybe cause we don't understand :p

Markus

2007-07-11 05:11:51

Remember this post by Lorax:
From: Lorax

you know BenLand is this little math kid that no one understands.
So because we don't understand we just do like: "mmhm, yeah, woaw, jubii, hello, mmhm, yea, neat, hey, juhu" or stuff like that, so it goes in one of the eyes and out the other ;)

Ben is just too smart for us normal people to understand :p

SKy Scripter

2007-07-11 05:20:09

yes. ben is smart but doesnt mean he always uses his brain. XD

Santa_Clause

2007-07-11 07:05:24

Haha, apparently Ben is a quick learner.

About what SKy said:

From: BenLand100
erm… i didn't even know there was a general public board , they've been minimized at the bottom of my srl page

SKy Scripter

2007-07-12 03:51:40

lol, {lol sorry ben had to save that ;)}

Another neat thing to do is take some 10-8 guage wire, wrap it in 5-7 loops around a coke can (0.5 inches spacing) and put that coil (without the can!!!) into the center of a satalite dish (comming through the back. Now hook that coil up to a large energy source (em pulser or cap bank) and *quickly* dump the entire load into it. The wire will vaporize into an almost superconductive plasma and…
Ben Land says:
generate a massive em field which will be focused at a target by the dish effectivly destroying any electronics in its range

Wanted

2007-07-12 23:57:05

Hey, if you don't mind me asking,

What are you planing to do with C++ plugins?

jhildy

2007-07-13 23:35:27

From: SKy Scripter
Another neat thing to do is take some 10-8 guage wire, wrap it in 5-7 loops around a coke can (0.5 inches spacing) and put that coil (without the can!!!) into the center of a satalite dish (comming through the back. Now hook that coil up to a large energy source (em pulser or cap bank) and *quickly* dump the entire load into it. The wire will vaporize into an almost superconductive plasma and…
Ben Land says:
generate a massive em field which will be focused at a target by the dish effectivly destroying any electronics in its range


wow…

lamatime

2007-07-24 20:30:25

so, this lets me use C++ on scar?

GREAT!

next week i am taking a C++ camp/course

I cant wait to apply my new c++ skills in scar!

thanks!

Freddy1990

2007-07-25 07:42:39

Heh, my tutorial was the best I could do without actually learning c++

EDIT: Hmm, ben, with what compiler do you have to compile this? Borland C++ Builder 6 compiles it, but it doesn't import into SCAR

lardmaster

2007-07-25 21:14:47

its great if you want to run c code in scar, the most universal language!

BenLand100

2007-07-25 21:31:33

From: Freddy1990
EDIT: Hmm, ben, with what compiler do you have to compile this? Borland C++ Builder 6 compiles it, but it doesn't import into SCAR

Should compile on anything. I used VC++ 6. Make sure you have the .def file to change the export names ;)

ronny.m.p

2007-07-26 07:29:23

Hey ben. How exactly do these files work (what do I have to do) and what do I save those two text files as (ext)?

R0b0t1

2007-11-24 17:59:18

Eh. How do I use the DEF file?


*sorry for mega-bump*

Wizzup?

2008-01-02 16:49:41

Big bump again. Thanks, Ben. ;)

mastaraymond

2008-01-02 17:03:16

From: Wizzup?
Big bump again. Thanks, Ben. ;)

That's an infraction.. No grave digging here.. I'm sorry.

pplsuqbawlz

2008-02-24 23:59:17

Suppose that works, not the way I would do it benland, but that's because I angst windows programming.

By the by, why are you declaring them as standard calls; Isn't that default?

And the functions don't do much, so technically __fastcall is your friend!

RAM

2008-02-25 00:29:54

From: pplsuqbawlz
Suppose that works, not the way I would do it benland, but that's because I angst(against) windows programming.

By the way, why are you declaring them as standard calls; Isn't that default?

And the functions don't do much, so technically __fastcall is your friend!


Wow, an Old topic keeps getting bumped, seems there is still an interest in this….and considering who is bumping, I think we'll let it slide and await Ben's reply here :)

BTW, Welcome pplsuqbawlz, your reputation precedes you :)

~RAM

Wizzup?

2008-02-26 15:21:34

From: RAM
Wow, an Old topic keeps getting bumped, seems there is still an interest in this….


Could be… since Embedded Smart.dll is made in C++. :)
I'll try to link BenLand to this topic next time he's online.

Bobarkinator

2008-02-26 16:23:58

From: Wizzup?
Could be… since Embedded Smart.dll is made in C++. :)
I'll try to link BenLand to this topic next time he's online.


Evil undecompiling c++

BenLand100

2008-02-26 22:28:55

From: Farseercccp
This is all useless for scar scripting. C++ is only useful if you are working on a project similar to autorune.

Or making something like SMART… Pwnt?

From: pplsuqbawlz
Suppose that works, not the way I would do it benland, but that's because I angst windows programming.

By the by, why are you declaring them as standard calls; Isn't that default?

And the functions don't do much, so technically __fastcall is your friend!

Ya, I'm kind of tied to windows programming anyway since its mostly responsible for making the screen an image, and I need to control parts of that ;) As for the standard calls, I'd rather be explicit than vague, plus the scar/delphi end requires them to be stdcall. I'm not sure if it would work another way (never tried, the delphi versions always explicitly used stdcall), though I'm pretty sure fastcall would use registers, and since scar/delphi does not know to do that… Basically i'm tied to whatever scar already knows how to do, if that makes any sense. :rolleyes:

logik

2008-02-29 13:52:52

Well coding scar in C++ is like someone in the english (what we use in U.S.) measuring system trying to convert to SI (system international what foreign use) for comparing data, SMART is the only thing that should be in C++ for scar o.O its too much for our normal minds…

Wizzup?

2008-08-27 06:32:48

Well Ben, it doesn't work on Code::Blocks. I can't get to compile the first part of your code.

F:\CodeBlocks Projects\SCARPlugin.cpp|39|error: invalid conversion from `int (*)()' to `void*'|

Also, maybe you could be more clear on this .def file?

mastaraymond

2008-08-27 06:39:04

From: Wizzup?
Well Ben, it doesn't work on Code::Blocks. I can't get to compile the first part of your code.

F:\CodeBlocks Projects\SCARPlugin.cpp|39|error: invalid conversion from `int (*)()' to `void*'|

Also, maybe you could be more clear on this .def file?

I've got it to work at only 1 compiler :p

Wizzup?

2008-10-06 11:33:09

Well, today I made a C Plugin for SCAR with a friend, during a college. :p
It only works on the SCAR Portable edition, don't ask me why. ;)
It should compile with the GCC Compiler, and the Microsoft Compiler.
I didn't add a DLLMain, since that seems redundant.
It might be the reason it isn't working on the SCAR Normal editions, or it is just generally a bug in the normal SCAR Editions, and accidentally fixed in the Portable Editions. (It doesn't work on 2.03, either)


#define EXPORT_FUNCTIONS
#include "main.h"
#include <windows.h>

DLLCALL unsigned int GetFunctionInfo(unsigned int x,void** ppfunc,char** szlppString)
{
switch(x)
{
case 0:
strcpy(*szlppString,"procedure hello();");
*ppfunc = (void*)hello;
break;
default:
return -1;
}
return x;
}

DLLCALL unsigned int GetFunctionCount(void)
{
return 1;
}

void hello()
{
MessageBox(NULL, "Testing 1 2 3", "DLL", MB_OK);
}


and the header(main.h) :


#ifdef EXPORT_FUNCTIONS
#define DLLCALL __declspec(dllexport)
#else
#define DLLCALL __declspec(dllimport)
#endif

DLLCALL unsigned int GetFunctionInfo(unsigned int x,void** ppfunc,char** szlppString);
DLLCALL unsigned int GetFunctionCount(void);
void hello();

Will update on this later… I guess.

chemicstry

2012-01-29 08:52:38

Bumping this old thread…

So I'm using this code:
#include <windows.h>
#define Export _declspec(dllexport)
#define DelphiExport extern "C" Export

HINSTANCE inst;

Export BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID checks) {
switch(reason) {
case DLL_PROCESS_ATTACH:
//Do you're init here, but don't change anything else (besides cleanup)
inst = instance;
case DLL_THREAD_ATTACH:
return true;
case DLL_PROCESS_DETACH:
//Do you're cleanup here, but don't change anything else (besides init)
FreeLibrary(inst);
case DLL_THREAD_DETACH:
return true;
}
return false;
}

//Add in your functions

void Test(void) {
//Shows a message box.
MessageBox(NULL, "Testing 1 2 3", "DLL", MB_OK);
}

//Export name changed in def file
DelphiExport int __stdcall GetFunctionCount(void) {
return 1; //Change depending on number to export
}

//Export name changed in def file
DelphiExport int __stdcall GetFunctionInfo(int x, void** address, char** def) {
switch (x) {
case 0:
*address = (void*)Test;
strcpy(*def,"procedure DllTest;"); //Have to strcpy, how you want it to be called
return x;
}
return -1;
}

Compiled with CodeBlocks.

Simba script:
program new;

{$loadlib test2}

begin
DllTest;
end.

However I get error that DllTest is undefined.
It also sometimes give "Your DLL test2 has not been found. Exception in Script: Unknown compiler directives at 4:3" errors.

Few things wasn't clear for me:
1. .def file has to be generated by compile or should I create it manually? And if my dll is test2.dll so this file should be test2.def?
2. How do I enable debugging to know what functions were exported?

Thanks.

EDIT: Nvm, fixed… I was unable to make Code::Blocks include .def file into dll so I used MinGW.