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文件的文件路径
返回值:
读取到的字符数,不包括末尾的'