Results 1 to 11 of 11

Thread: C Plugins

  1. #1
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default C Plugins

    C Plugins

    For this tutorial you will need a C compiler.
    I will also include some information on a simple Makefile for C projects.

    You will want a reliable way to backup your work!! Ex: GitHub, googlecode

    There is a zip at the end of this post with all the files needed to start your C plugins (Doesn't include a C compiler or make)

    Here is a template for main.c (Can be renamed to anything with a c extension)
    PHP Code:
    #include <main.h>
    #include <string.h>

    static __stdcall int a(void)
    {
      return 
    42;
    }

    static 
    __stdcall int GetFuncCount(void)
    {
      return 
    1;
    }

    static 
    __stdcall int GetFuncInfo(int xvoid **addrchar **def)
    {
      switch (
    x) {
        case 
    0:
          *
    addr = (void *)&a;
          
    strcpy(*def"function a(): LongInt;");
          break;
        default:
          
    = -1;
      }
      return 
    x;

    and main.h
    Note there is a little magic here to get rid of stdcall name mangling.
    PHP Code:
    #ifndef __MAIN_H_
    #define __MAIN_H_

    static __stdcall int a(void);
    static 
    __stdcall int GetFuncCount(void);
    static 
    __stdcall int GetFuncInfo(int xvoid **addrchar **def);

    int GetFunctionCount(void__attribute__((alias("GetFuncCount@0")));
    int GetFunctionInfo(int xvoid **addrchar **def__attribute__((alias("GetFuncInfo@12")));

    #endif 
    Now you can ether decide on a Makefile or a simple sh/bat file.

    Your simple bat/sh file will look something like this.
    Code:
    gcc -I. -c main.c -o main.o
    gcc -shared -o libTest.dll main.o
    You can also go the Makefile route.
    For this method I ask you put all *.c files in src/
    and all *.h files in includes/

    Code:
    .PHONY: all build clean dist todolist
    
    PROJECT=cplugin4simb
    CC=gcc
    
    EXT=.so
    ifeq ($(OS),Windows_NT)
    EXT=.dll
    endif
    
    AUXFILES := Makefile README COPYING $(PROJECT)$(EXT)
    PROJDIRS := src includes
    SRCFILES := $(shell find $(PROJDIRS) -type f -name "*.c")
    HDRFILES := $(shell find $(PROJDIRS) -type f -name "*.h")
    OBJFILES := $(patsubst src/%.c,build/%.o,$(SRCFILES))
    DEPFILES := $(patsubst src/%.c,build/%.d,$(SRCFILES))
    SRCFILES := $(SRCFILES)
    ALLFILES := $(SRCFILES) $(HDRFILES) $(AUXFILES)
    
    WARNINGS := -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align \
                -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations \
                -Wredundant-decls -Wnested-externs -Winline -Wno-long-long \
                -Wuninitialized -Wconversion -Wstrict-prototypes
    CFLAGS := -g -std=c99 -O -I./includes -c $(WARNINGS)
    
    all: clean build
    
    build: $(PROJECT)$(EXT)
    	
    $(PROJECT)$(EXT): $(OBJFILES)
    	@$(CC) -shared -o $(PROJECT)$(EXT) $(OBJFILES)
    	
    %/.ph: 
    	@mkdir -p $(@D)
    	@touch $@
    	
    build/%.o: src/%.c build/.ph
    	@$(CC) $(CFLAGS) $< -o $@
    
    clean:
    	-@$(RM) $(wildcard $(PROJECT).tar.gz  $(PROJECT)$(EXT))
    	-@$(RM) -r build
    	
    dist:
    	@tar czf $(PROJECT).tar.gz $(ALLFILES)
    	
    todolist:
    	-@for file in $(SRCFILES); do fgrep -H -e TODO -e FIXME $$file; done; true
    Note this makefile will build your dll with ALL c files in src/ no need to change anything.


    If you notice anything weird or wrong, please don't hesitate to ask questions.
    Last edited by Dgby714; 12-29-2011 at 01:08 PM.

  2. #2
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    Good

  3. #3
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Holy crap luffs, you're fast.... Only 1min after I made the thread...

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  4. #4
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    Mind reader. Not super informative, but whatever.

  5. #5
    Join Date
    Sep 2006
    Posts
    6,089
    Mentioned
    77 Post(s)
    Quoted
    43 Post(s)

    Default

    Cool How do you work with strings? Pass them as PChar?
    Hup Holland Hup!

  6. #6
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by nielsie95 View Post
    Cool How do you work with strings? Pass them as PChar?
    Yes, tho PascalScript's string does work as a PChar as well you just have to remember to add a #0 too the end.
    Last edited by Dgby714; 08-11-2011 at 10:21 PM.

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  7. #7
    Join Date
    Jan 2011
    Location
    Denver, CO
    Posts
    1,351
    Mentioned
    2 Post(s)
    Quoted
    72 Post(s)

    Default

    Really nice tutorial, I do have one question. What are the @0 and @12 for at the end of the strings in the header file? ("GetFuncCount@0" and "GetFuncInfo@12")

  8. #8
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by Echo_ View Post
    Really nice tutorial, I do have one question. What are the @0 and @12 for at the end of the strings in the header file? ("GetFuncCount@0" and "GetFuncInfo@12")
    It is stdcall name mangling. It is how much of space the parameters take up, some compilers also add a underscore to the begging of the function.

    say

    int __stdcall a(int b);

    externally will look like

    a@4 or _a@4

    where as

    int __stdcall a(void); (This has no parameters)

    will look like

    a@0 or _a@0

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  9. #9
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    Quote Originally Posted by Dgby714 View Post
    It is stdcall name mangling. It is how much of space the parameters take up, some compilers also add a underscore to the begging of the function.

    say

    int __stdcall a(int b);

    externally will look like

    a@4 or _a@4

    where as

    int __stdcall a(void); (This has no parameters)

    will look like

    a@0 or _a@0
    So it doesn't count space? "int b" becomes basically "intb"?
    There used to be something meaningful here.

  10. #10
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Quote Originally Posted by Frement View Post
    So it doesn't count space? "int b" becomes basically "intb"?
    You are quite lost lol...

    I ment it show the size of all the parameters.

    An int is 4 bytes long.

    so a function with only 1 parameter that is an int gets @4
    Where as a function with 2 parameters both int gets @8

    pointers are also 4 bytes long.

    so take a look at

    static __stdcall int GetFuncInfo(int x, void **addr, char **def);

    it has 3 parameters

    a int 4 bytes
    a pointer 4 bytes
    another pointer 4 bytes

    that means it will be

    GetFuncInfo@12

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

  11. #11
    Join Date
    Jan 2011
    Location
    Denver, CO
    Posts
    1,351
    Mentioned
    2 Post(s)
    Quoted
    72 Post(s)

    Default

    Quote Originally Posted by Dgby714 View Post
    It is stdcall name mangling. It is how much of space the parameters take up, some compilers also add a underscore to the begging of the function.

    say

    int __stdcall a(int b);

    externally will look like

    a@4 or _a@4

    where as

    int __stdcall a(void); (This has no parameters)

    will look like

    a@0 or _a@0
    Thanks for clearing that up

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •