TOP Win32API

EXPORTS

to define exports

Use __declspec(dllexport) or .DEF file
__declspec(dllexport)
.DEF

to link exports

dynamic linkstatic link
compilewith header
linknonewith import library(.lib) of dll
loadapp will load/unload dlldll will be loaded on exe loaded.
callfunc pointerusing proto

dumpbin

$ dumpbin /exports calc.dll
Microsoft (R) COFF/PE Dumper Version 8.00.50727.762
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file calc.dll

File Type: DLL

  Section contains the following exports for calc.dll

    00000000 characteristics
    4829555F time date stamp Tue May 13 17:46:23 2008
        0.00 version
           1 ordinal base
           2 number of functions
           2 number of names

    ordinal hint RVA      name

          1    0 00001000 Add
          2    1 0000100D Subtract

  Summary

        1000 .bss
        1000 .edata
        1000 .idata
        1000 .reloc
        1000 .stab
        1000 .stabstr
        1000 .text
$ dumpbin /imports test.exe

__stdcall

statck will be poped by this function.

C++

.h

// mydll.h
#ifndef __MYDLL_H__
#define __MYDLL_H__
// DLLAPI definition when creating .exe
#ifndef DLLAPI
#define DLLAPI extern "C" __declspec(dllimport)
#endif
#include <windows.h>
DLLAPI BOOL Swap(int *lp1, int *lp2);
#endif

.cpp (for dll)

// DLLAPI definition for creating .dll
#define DLLAPI extern "C" __declspec(dllexport)
#include "mydll.h"
BOOL IsValidAddress(LPVOID lp1, LPVOID lp2) {
    return lp1 == NULL || lp2 == NULL;
}
DLLAPI BOOL Swap(int *lp1, int *lp2){
    int tmp;
    if (IsValidAddress(lp1, lp2)) return FALSE;
    tmp  = *lp1;
    *lp1 = *lp2;
    *lp2 = tmp;
    return TRUE;
}

makefile

$(DLLTARGET).lib: $(DLLTARGET).dll
$(DLLTARGET).dll: $(OBJS4DLL) #$(DLLTARGET).def
    link /DLL /OUT:$@ $(OBJS4DLL) $(LOPT4DLL) #/DEF:$(DLLTARGET).def

DLL func prototype

use with DLL source

#ifdef __cplusplus
extern "C" {
#endif
void __declspec(dllexport) __stdcall dll_func(int param);
#ifdef __cplusplus
}
#endif

use with APP source

replcace dllexport with dllimport

Dynamic link

hInstance hdll;
void (__stdcall * myfunc)(int param);
hdll=LoadLibraryEx("mydll.dll",NULL,0);
if(hdll==NULL) exit(-1);
myfunc=(void (__stdcall *)(int))GetProcAddress(hdll,"myfunc");
myfunc(1);
FreeLibrary(hdll);

DllMain

BOOL WINAPI DllMain(HINSTANCE hdll,DWORD msg,LPVOID lpv){
   switch(msg){
       case DLL_PROCESS_ATTACH:
       case DLL_THREAD_ATTACH:
       case DLL_THREAD_DETACH:
       case DLL_PROCESS_DETACH:
            break;
   }
}

pragma data_seg

some.cpp

#pragma data_seg("brabra")
int counter=0;
#pragma data_seg()
InterlockedIncrement(&counter);

some.def

LIBRARY some
SECTIONS
    brabra READ WRITE SHARED

管理人/副管理人のみ編集できます