Skip to content

Instantly share code, notes, and snippets.

@masahitojp
Created March 27, 2014 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masahitojp/9806567 to your computer and use it in GitHub Desktop.
Save masahitojp/9806567 to your computer and use it in GitHub Desktop.
#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
from ctypes import windll
lib = windll.LoadLibrary("dll.dll");
if (lib.__add(1,2) != 3):
print("error!")
else:
print("ok")
#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