gcc
デフォルトで関数をエクスポートするようですが、PE Explorer (View > Export) などの任意の PE ビューアーを使用して、エクスポートされた関数を表示できます。

ただし、このコードを VC++ でコンパイルしようとすると、この関数はエクスポートされません。エクスポートされた関数がないことがわかります。

この関数をエクスポートするように依頼する必要があります。
__declspec(dllexport) int addition(int a, int b){
return a+b;
}
呼び出し規約に関しては、ルールは単純です。
関数がほとんどの Win32API と同様に を使用する場合、またはを使用__stdcall
して DLL をインポートする必要があります。例:WinDLL('mylib.dll')
windll.mylib
> type mylib.c
__declspec(dllexport) int __stdcall addition(int a, int b) {
return a+b;
}
***********************************************************************
> cl mylib.c /link /dll /out:mylib.dll
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
mylib.c
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.
/out:mylib.exe
/dll
/out:mylib.dll
mylib.obj
Creating library mylib.lib and object mylib.exp
***********************************************************************
> python
>>> from ctypes import *
>>>
>>> WinDLL('mylib.dll').addition(1, 2)
3
>>> windll.mylib.addition(1, 2)
3
>>>
関数が を使用する場合、 witch がデフォルトの呼び出し規約であるため、またはを使用__cdecl
して DLL をインポートする必要があります。例:CDLL('mylib.dll')
cdll.mylib'
> type mylib.c
// `__cdecl` is not needed, since it's the default calling convention
__declspec(dllexport) int addition(int a, int b){
return a+b;
}
***********************************************************************
> cl mylib.c /link /dll /out:mylib.dll
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
mylib.c
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.
/out:mylib.exe
/dll
/out:mylib.dll
mylib.obj
Creating library mylib.lib and object mylib.exp
***********************************************************************
> python
>>> from ctypes import *
>>>
>>> CDLL('mylib.dll').addition(1, 2)
3
>>> cdll.mylib.addition(1, 2)
3
>>>