blabberer

  1. Simple Dll Compiled From Commandline Unlike what google returns vc++ proj

    Sometimes Google in its infinite wisdom will never get you what you remember you saw earlier in the same Google come what may or use whatever search term you may imagine

    so i was searching for this simple dll tutorial which i vividly remember and i couldn't find it

    and hence this blog

    so you want to create a dll and you want to do it in command line
    not using start->program->vs->new->project->name->win32->console->crap>dll>bs->whatever->magic->finish->stdafx.h->pch->build f7->search the whole comp for dll

    ok here is how you do it

    make a new directory somewhere the dir i created is named NOFIXED

    add these files to the directory

    Code:
    NOFIXED:\>cd NOFIXED
    
    NOFIXED:\>dir /b
    AddNumbers.bat
    AddNumbers.c
    AddNumbers.def
    AddNumbers.h
    CallAddNum.c
    NOFIXED:\>
    AddNumbers.c is the source code for dll and it contains

    Code:
    NOFIXED:\>type AddNumbers.c
    #include <windows.h>
    #include "AddNumbers.h"
    
    
    BOOL WINAPI DllMain(
        HINSTANCE hinstDLL,
        DWORD fdwReason,
        LPVOID lpReserved ){
                    switch( fdwReason ){
                            case DLL_PROCESS_ATTACH:
                                    break;
                            case DLL_THREAD_ATTACH:
                                    break;
                            case DLL_THREAD_DETACH:
                                    break;
                            case DLL_PROCESS_DETACH:
                                    break;
                    }
                    return TRUE;
    }
    
    
    _declspec (dllexport) ULONG AddNumbers(ULONG a, ULONG b){
        return((ULONG)(a+b));
    }
    
    NOFIXED:\>
    AddNumbers.h is the Header File You Would Need To Link To The Dll when You Create An EXE and it contains

    Code:
    NOFIXED:\>type AddNumbers.h
    #include <windows.h>
    BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,LPVOID lpReserved );
    _declspec (dllexport) ULONG  AddNumbers(ULONG a, ULONG b);
    
    NOFIXED:\>
    AddNumbers.def is the module definition File that is required to Build a Dll and it contains

    Code:
    NOFIXED:\>type AddNumbers.def
    EXPORTS
    AddNumbers
    NOFIXED:\>
    CallAddNum.c is the source code for the exe that links to the AddNumbers.dll that you are going to Build and Call the function in the dll and it contains

    Code:
    NOFIXED:\> type CallAddNum.c
    #include <stdio.h>
    #include "AddNumbers.h"
    
    int main (void){
    
            printf("3 + 5 = %x\n" , AddNumbers(3,5));
            return 0;
    }
    NOFIXED:\>
    and finally AddNumbers.Bat contains the command line to build the exe and dll

    and it contains the following commands
    cl is the compiler
    /nologo suppresses copyright message
    /c compiles only no linking
    /LD tells the compiler to create a dll and not an exe
    link is the linker
    /dll tells the linker to link the compiled obj code into a dll
    def:"AddNumbers.def" is the module def file which creates AddNumbers.Lib And AddNumbers.Exp which you use when you compile any exe to link to the dll

    Code:
    
    NOFIXED:\>type AddNumbers.bat
    cl  /nologo /c /LD AddNumbers.c
    link /NOLOGO /dll /def:"AddNumbers.def" AddNumbers.obj
    cl /nologo CallAddNum.c AddNumbers.lib
    NOFIXED:\>
    now open vs2008commandprompt

    start -> program -> microsoft visual c++ 2008 Express Edition -> visual studio tools -> visual studio 2008 command prompt

    navigate to the present NOFIXED Directory
    and run the AddNumbers.bat you should get you dll and exe compiled

    Code:
    NOFIXED:\>dir /b & AddNumbers.bat & dir /b
    AddNumbers.bat
    AddNumbers.c
    AddNumbers.def
    AddNumbers.h
    CallAddNum.c
    
    NOFIXED:\>cl  /nologo /c /LD AddNumbers.c
    AddNumbers.c
    
    NOFIXED:\>link /NOLOGO /dll /def:"AddNumbers.def" AddNumbers.obj
       Creating library AddNumbers.lib and object AddNumbers.exp
    
    NOFIXED:\>cl /nologo CallAddNum.c AddNumbers.lib
    
    CallAddNum.c
    AddNumbers.bat
    AddNumbers.c
    AddNumbers.def
    AddNumbers.dll
    AddNumbers.exp
    AddNumbers.h
    AddNumbers.lib
    AddNumbers.obj
    CallAddNum.c
    CallAddNum.exe
    CallAddNum.obj
    
    NOFIXED:\>
    run the exe to check if you have succeeded

    Code:
    NOFIXED:\>CallAddNum.exe
    3 + 5 = 8
    
    NOFIXED:\>
    that is all for now