在Python 中调用Dll


1.首先写DLL文件,环境是在VC 6.0中

如下所示:

  1. // funDll.cpp : Defines the entry point for the DLL application.   
  2. //   
  3.   
  4. #include "stdafx.h"   
  5. #include <iostream>   
  6. using namespace std;  
  7.   
  8. #ifdef _MANAGED   
  9. #pragma managed(push, off)   
  10. #endif   
  11.   
  12. #ifdef __cplusplus    
  13. #define EXPORT extern "C"__declspec(dllexport)   
  14. #else   
  15. #define EXPORT __declspec(dllexport)   
  16. #endif   
  17. EXPORT int HelloWorld()  
  18. {  
  19.  cout <<"hello world" <<endl;  
  20.  return 0;  
  21. }  
  22.   
  23.   
  24. BOOL APIENTRY DllMain( HMODULE hModule,  
  25.                        DWORD  ul_reason_for_call,  
  26.                        LPVOID lpReserved  
  27.       )  
  28. {  
  29.     return TRUE;  
  30. }  
  31.   
  32. #ifdef _MANAGED   
  33. #pragma managed(pop)   
  34. #endif  

2.然后书写python调用DLL代码。

  1. #coding=utf-8   
  2. ''''' 
  3. Created on 2011-12-5 
  4.  
  5. @author: LONMID 
  6. '''  
  7. from ctypes import *  
  8. fileName = "funDll.dll"  
  9. func = cdll.LoadLibrary(fileName)  
  10. #print func.HelloWorld()ffd天下第一   
  11.   
  12.   
  13.   
  14. func.HelloWorld()  

如果出现"Hello world",说明运行成功。

相关内容