1.首先寫DLL文件,環境是在VC 6.0中
如下所示:
- // funDll.cpp : Defines the entry point for the DLL application.
- //
-
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
-
- #ifdef _MANAGED
- #pragma managed(push, off)
- #endif
-
- #ifdef __cplusplus
- #define EXPORT extern "C"__declspec(dllexport)
- #else
- #define EXPORT __declspec(dllexport)
- #endif
- EXPORT int HelloWorld()
- {
- cout <<"hello world" <<endl;
- return 0;
- }
-
-
- BOOL APIENTRY DllMain( HMODULE hModule,
- DWORD ul_reason_for_call,
- LPVOID lpReserved
- )
- {
- return TRUE;
- }
-
- #ifdef _MANAGED
- #pragma managed(pop)
- #endif
2.然後書寫python調用DLL代碼。
- #coding=utf-8
- '''''
- Created on 2011-12-5
-
- @author: LONMID
- '''
- from ctypes import *
- fileName = "funDll.dll"
- func = cdll.LoadLibrary(fileName)
- #print func.HelloWorld()ffd天下第一
-
-
-
- func.HelloWorld()
如果出現"Hello world",說明運行成功。