歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 在Python 中調用Dll

在Python 中調用Dll

日期:2017/3/1 10:50:29   编辑:Linux編程

1.首先寫DLL文件,環境是在VC 6.0中

如下所示:

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

2.然後書寫python調用DLL代碼。

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

如果出現"Hello world",說明運行成功。

Copyright © Linux教程網 All Rights Reserved