1. python 有沒有和 C/C++ 進程共享內存的方式
通過share memory 取態悔對象的例子, c write object into memory map, python read it by call dll api.
So there still questions you should consider how to guarantee the process share security. Good luck..
----python part----
from ctypes import *
#windll.kernel32.SetLastError(-100)
print windll.kernel32.GetLastError()
getMessage=windll.kernel32.OpenFileMappingA
getMessage.restype = c_int
handle=getMessage(1,False,"Global\\MyFileMappingObject")
if handle ==0:
print 'open file mapping handle is Null'
else:
mapView=windll.kernel32.MapViewOfFile
mapView.restype = c_char_p
print mapView(handle,1,0,0,256)
----c part----
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#define BUF_SIZE 256
TCHAR szName[] = TEXT("Global\\MyFileMappingObject");
TCHAR szMsg[] = TEXT("Message from first process.");
int _tmain()
{
HANDLE hMapFile;
LPCTSTR pBuf;
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
szName); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not create file mapping object (%d).\敬閉悉n"),
GetLastError());
return 1;
}
pBuf = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, /亮乎/ read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
_getch();
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}
2. 怎樣用python調用vc++編出來的win32 DLL
python調用vc中的API
准備工作: 安裝pywin32-210.5.win32-py2.5.exe
例1:最簡單的MessageBox函數
import win32api, win32gui
import win32con, winerror,win32event,pywintypes
import sys, os,time
win32api.MessageBox(0,'hello', 'WYM',win32con.MB_OK)
例2:進程監控
#Mutex.py
mutex=None
mutex=win32event.CreateMutex(None,pywintypes.FALSE,"MutexWym")
if(win32api.GetLastError() == winerror.ERROR_ALREADY_EXISTS):
print"Opened existing mutex object", mutex
else:
print "Created new mutex"
time.sleep(10)
win32api.CloseHandle(mutex)
print "close Mutex"
time.sleep(10)
#MonitorProcess.py
import win32api, win32gui
import win32con, winerror,win32event ,pywintypes
import sys, os,time
STANDARD_RIGHTS_REQUIRED = 0x000F0000L
SYNCHRONIZE = 0x00100000L
MUTANT_QUERY_STATE = 0x0001
MUTEX_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | MUTANT_QUERY_STATE
try:
while 1:
time.sleep(2)
hMutex=win32event.OpenMutex(MUTEX_ALL_ACCESS,pywintypes.FALSE,"MutexWym")
print win32api.GetLastError()
if hMutex is not None:
print "Mutex open"
win32api.CloseHandle(hMutex)
else:
print "Mutex not open"
break
except pywintypes.error, (errno, object, strerror):
print "Error in", object, ":", strerror
如何使用kernel32的API呢
例3:
import pywintypes
import winerror, win32con
import win32api, win32event, win32file, win32pipe, win32process, win32security
import win32service, win32serviceutil, servicemanager,ntsecuritycon
from ctypes import * #本例包括這個頭文件
hMutex = windll.kernel32.CreateMutexA(None, 0, "szMutex")
#其它API同理,比如像windll.kernel32.InitializeCriticalSection() 等等
a、python中涉及的windows下的類型?
通過pywintypes 可以獲得這個數據類型
b、獲得窗口句柄
通過FindWindow這API來查找相應的窗口的句柄,然後發消息給這個窗口,就可以實現這個功能
ct = win32api.GetConsoleTitle()
hd = win32gui.FindWindow(0,ct)
win32gui.ShowWindow(hd,0)
3. INI的文件讀寫
讀取 (_In_LPCTSTRlpAppName,_In_LPCTSTRlpKeyName,_In_LPCTSTRlpDefault,_Out_LPTSTRlpReturnedString,_In_DWORDnSize,_In_LPCTSTRlpFileName);參數說明:
lpAppName (輸入參數)節名
lpKeyName (輸入參數)鍵名
lpDefault (輸入參數)默認值。INI讀取失敗時,得到這個值
lpReturnedString (輸出參數)讀取到的鍵值
nSize (輸入參數)lpReturnedString的緩沖區大小
lpFileName (輸入參數)INI文件的文件路徑
返回值:
讀取到的字元數,不包括末尾的'