Created
March 27, 2014 12:34
-
-
Save masahitojp/9806567 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
// C++では、内部の関数名が異なるので、C言語として認識させる。 | |
// 内部の関数名が異なる理由は | |
// C++のポリモフィズムにより、関数名@識別子を別途付けるため | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
// DLLとして公開する関数に付けるPrefix | |
#define DLL_EXPORT __declspec (dllexport) | |
DLL_EXPORT int __add(const int x, const int y); | |
DLL_EXPORT int __sum(const int x, const int y); | |
DLL_EXPORT int __mul(const int x, const int y); | |
DLL_EXPORT int __div(const int x, const int y); | |
// C言語として認識させる空間はここまで | |
#ifdef __cplusplus | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ctypes import windll | |
lib = windll.LoadLibrary("dll.dll"); | |
if (lib.__add(1,2) != 3): | |
print("error!") | |
else: | |
print("ok") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "dll.h" | |
#include <Windows.h> | |
// DLLのエントリポイント | |
// DLLにアタッチ/デタッチが発生する都度呼ばれ、FALSEを返すと失敗となります。 | |
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; | |
} | |
// 単純な加算。 | |
DLL_EXPORT int __add(const int x, const int y) { | |
return x + y; | |
} | |
// 単純な減算。 | |
DLL_EXPORT int __sum(const int x, const int y) { | |
return x - y; | |
} | |
// 単純な乗算。 | |
DLL_EXPORT int __mul(const int x, const int y) { | |
return x * y; | |
} | |
// 単純な除算。 | |
DLL_EXPORT int __div(const int x, const int y) { | |
return (y) | |
? x / y | |
: 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment