Sat Nov 08, 2008 9:55 am
public static final int PAGE_READONLY = 0x02;
public static final int PAGE_READWRITE = 0x04;
public static final int PAGE_WRITECOPY = 0x08;
public static final int FILE_MAP_COPY = 0x0001;
public static final int FILE_MAP_WRITE = 0x0002;
public static final int FILE_MAP_READ = 0x0004;
public static native int createFileMapping(int lProtect,
int dwMaximumSizeHigh, int dwMaximumSizeLow, String name);
public static native int openFileMapping(int dwDesiredAccess,
boolean bInheritHandle, String name);
public static native int mapViewOfFile(int hFileMappingObj,
int dwDesiredAccess, int dwFileOffsetHigh,
int dwFileOffsetLow, int dwNumberOfBytesToMap);
public static native boolean unmapViewOfFile(int lpBaseAddress);
public static native void writeToMem(int lpBaseAddress, String content);
public static native String readFromMem(int lpBaseAddress);
public static native boolean closeHandle(int hObject);
public static native void broadcast();
JNIEXPORT jint JNICALL Java_com_stanley_memmap_MemMapFile_createFileMapping
(JNIEnv * pEnv, jclass, jint lProtect, jint dwMaximumSizeHigh,
jint dwMaximumSizeLow, jstring name) {
HANDLE hFile = INVALID_HANDLE_VALUE;
HANDLE hMapFile = NULL;
LPCSTR lpName = pEnv->GetStringUTFChars(name, NULL);
__try {
hMapFile = CreateFileMapping(hFile, NULL, lProtect,
dwMaximumSizeHigh, dwMaximumSizeLow, lpName);
if(hMapFile == NULL) {
ErrorHandler(_T("Can not create file mapping object"));
__leave;
}
if(GetLastError() == ERROR_ALREADY_EXISTS)
{ ErrorHandler(_T("File mapping object already
exists"));
CloseHandle(hMapFile);
__leave;
}
} __finally{
}
pEnv->ReleaseStringUTFChars(name, lpName);
// if hMapFile is NULL, just return NULL, or return the handle
return reinterpret_cast<<code>jint>(hMapFile);
}
JNIEXPORT jint JNICALL Java_com_stanley_memmap_MemMapFile_mapViewOfFile
(JNIEnv *, jclass, jint hMapFile, jint dwDesiredAccess,
jint dwFileOffsetHigh,
jint dwFileOffsetLow, jint dwNumberOfBytesToMap) {
PVOID pView = NULL;
pView = MapViewOfFile(reinterpret_cast<<code>HANDLE>(hMapFile),
dwDesiredAccess,
dwFileOffsetHigh, dwFileOffsetLow, dwNumberOfBytesToMap);
if(pView == NULL) ErrorHandler(_T("Can not map view of file"));
return reinterpret_cast<jint>(pView);
}
JNIEXPORT void JNICALL Java_com_stanley_memmap_MemMapFile_broadcast
(JNIEnv *, jclass) {
SendMessage(HWND_BROADCAST, UWM_DATA_READY, 0, 0);
}
#define UWM_DATA_READY_MSG _TAll right, with MemMapFile, now we can share memory among processes, provided these processes know the File-Mapping kernel object name.
("UWM_DATA_READY_MSG-{7FDB2CB4-5510-4d30-99A9-CD7752E0D680}")
UINT UWM_DATA_READY;
BOOL APIENTRY DllMain(HINSTANCE hinstDll,
DWORD dwReasion, LPVOID lpReserved) {
if(dwReasion == DLL_PROCESS_ATTACH)
UWM_DATA_READY = RegisterWindowMessage(UWM_DATA_READY_MSG);
return TRUE;
}
private int mapFilePtr;
private int viewPtr;
mapFilePtr = MemMapFile.createFileMapping(MemMapFile.PAGE_READWRITE, 0,
dwMemFileSize, fileMappingObjName);
if(mapFilePtr != 0) {
viewPtr = MemMapFile.mapViewOfFile(mapFilePtr, MemMapFile.FILE_MAP_READ |
MemMapFile.FILE_MAP_WRITE, 0, 0, 0);
}
public void actionPerformed(ActionEvent e) {
if(viewPtr != 0) {
MemMapFile.writeToMem(viewPtr, textArea.getText());
MemMapFile.broadcast();
}
}
public interface MemMapFileObserver {
public void onDataReady();
}
public class MemMapProxy {
static {
System.loadLibrary("MemMapProxyLib");
}
private MemMapFileObserver observer;
public MemMapProxy(MemMapFileObserver observer) {
this.observer = observer;
init();
}
public void fireDataReadyEvent() {
observer.onDataReady();
}
private native boolean init();
public native void destroy();
}
JNIEXPORT jboolean JNICALL Java_com_stanley_memmap_MemMapProxy_init
(JNIEnv * pEnv, jobject jobj) {
HANDLE hThread;
hThread = (HANDLE)_beginthreadex(NULL, 0,
&CreateWndThread, NULL, 0, &uThreadId);
if(!hThread) {
MessageBox(NULL, _T("Fail creating thread"), NULL, MB_OK);
return false;
}
g_jobj = pEnv->NewGlobalRef(jobj);
return true;
}
unsigned WINAPI CreateWndThread(LPVOID pThreadParam) {
HANDLE hWnd = CreateWindow(_T("dummy window"),
NULL, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if(hWnd == NULL) {
MessageBox(NULL, _T("Failed create dummy window"),
NULL, MB_OK|MB_ICONERROR);
return 0;
}
jint nSize = 1;
jint nVms;
jint nStatus = JNI_GetCreatedJavaVMs(&g_pJvm, nSize, &nVms);
if(nStatus == 0) {
nStatus = g_pJvm->AttachCurrentThread
(reinterpret_cast<void**>(&g_pEnv), NULL);
if(nStatus != 0) ErrorHandler(_T("Can not attach thread"));
}
else {
ErrorHandler(_T("Can not get the jvm"));
}
MSG Msg;
while(GetMessage(&Msg, 0, 0, 0)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
// cache the JNIEnv* pointer
JNIEnv* g_pEnv = NULL;
// cache the JavaVM* pointer
JavaVM* g_pJvm = NULL;
// cache the jobject(MemMapProxy) pointer
jobject g_jobj = NULL;
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM lParam) {
if(Msg == UWM_DATA_READY) {
Callback();
}
else return DefWindowProc(hWnd, Msg, wParam, lParam);
return 0;
}
void Callback() {
if(g_pEnv == NULL || g_jobj == NULL) return;
jclass cls = g_pEnv->GetObjectClass(g_jobj);
jmethodID mid = g_pEnv->GetMethodID(cls, "fireDataReadyEvent", "()V");
g_pEnv->CallVoidMethod(g_jobj, mid, NULL);
}
public void onDataReady() {
int mapFilePtr = MemMapFile.openFileMapping(MemMapFile.FILE_MAP_READ,
false, fileMappingObjName);
if(mapFilePtr != 0) {
int viewPtr = MemMapFile.mapViewOfFile(mapFilePtr,
MemMapFile.FILE_MAP_READ, 0, 0, 0);
if(viewPtr != 0) {
String content = MemMapFile.readFromMem(viewPtr);
textArea.setText(content);
MemMapFile.unmapViewOfFile(viewPtr);
}
MemMapFile.closeHandle(mapFilePtr);
}
}
LRESULT CMemMapCppClientDlg::OnDataReady(WPARAM, LPARAM) {
HANDLE hMapFile = NULL;
PVOID pView = NULL;
hMapFile = OpenFileMapping(FILE_MAP_READ, FALSE, m_pszMemMapFileName);
if(hMapFile == NULL) {
MessageBox("Can not open file mapping");
return 0;
}
pView = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
if(pView == NULL) {
MessageBox("Can map view of file");
CloseHandle(hMapFile);
return 0;
}
LPSTR szContent = reinterpret_cast<<code>LPSTR>(pView);
int nLen = strlen(szContent);
CString strContent;
while(nLen > 0) {
strContent += *szContent++;
--nLen;
}
strContent += '\0';
strContent.Replace("\n", "\r\n");
m_edit.SetWindowText(strContent);
if(pView) UnmapViewOfFile(pView);
if(hMapFile) CloseHandle(hMapFile);
return 0;
}
// dll implementation file MemMapLib.cpp
//
////////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>
#include "MemMapFile.h"
#define UWM_DATA_READY_MSG _T("UWM_DATA_READY_MSG-{7FDB2CB4-5510-4d30-99A9-CD7752E0D680}")
UINT UWM_DATA_READY;
BOOL APIENTRY DllMain(HINSTANCE hinstDll, DWORD dwReasion, LPVOID lpReserved) {
if(dwReasion == DLL_PROCESS_ATTACH)
UWM_DATA_READY = RegisterWindowMessage(UWM_DATA_READY_MSG);
return TRUE;
}
void ErrorHandler(LPCTSTR pszErrorMessage) {
MessageBox(NULL, pszErrorMessage, _T("Error"), MB_OK | MB_ICONERROR);
}
JNIEXPORT jint JNICALL Java_com_stanley_memmap_MemMapFile_createFileMapping
(JNIEnv * pEnv, jclass, jint lProtect, jint dwMaximumSizeHigh,
jint dwMaximumSizeLow, jstring name) {
HANDLE hFile = INVALID_HANDLE_VALUE;
HANDLE hMapFile = NULL;
LPCSTR lpName = pEnv->GetStringUTFChars(name, NULL);
__try {
hMapFile = CreateFileMapping(hFile, NULL, lProtect, dwMaximumSizeHigh,
dwMaximumSizeLow, lpName);
if(hMapFile == NULL) {
ErrorHandler(_T("Can not create file mapping object"));
__leave;
}
if(GetLastError() == ERROR_ALREADY_EXISTS) {
ErrorHandler(_T("File mapping object already exists"));
CloseHandle(hMapFile);
__leave;
}
}
__finally {
}
pEnv->ReleaseStringUTFChars(name, lpName);
// if hMapFile is NULL, just return NULL, or return the handle
return reinterpret_cast<jint>(hMapFile);
}
JNIEXPORT jint JNICALL Java_com_stanley_memmap_MemMapFile_openFileMapping
(JNIEnv * pEnv, jclass, jint dwDesiredAccess,
jboolean bInheritHandle, jstring name) {
HANDLE hMapFile = NULL;
LPCSTR lpName = pEnv->GetStringUTFChars(name, NULL);
hMapFile = OpenFileMapping(dwDesiredAccess, bInheritHandle, lpName);
if(hMapFile == NULL) ErrorHandler(_T("Can not open file mapping object"));
pEnv->ReleaseStringUTFChars(name, lpName);
return reinterpret_cast<jint>(hMapFile);
}
JNIEXPORT jint JNICALL Java_com_stanley_memmap_MemMapFile_mapViewOfFile
(JNIEnv *, jclass, jint hMapFile, jint dwDesiredAccess,
jint dwFileOffsetHigh, jint dwFileOffsetLow, jint dwNumberOfBytesToMap) {
PVOID pView = NULL;
pView = MapViewOfFile(reinterpret_cast<HANDLE>(hMapFile), dwDesiredAccess,
dwFileOffsetHigh, dwFileOffsetLow, dwNumberOfBytesToMap);
if(pView == NULL) ErrorHandler(_T("Can not map view of file"));
return reinterpret_cast<jint>(pView);
}
JNIEXPORT jboolean JNICALL Java_com_stanley_memmap_MemMapFile_unmapViewOfFile
(JNIEnv *, jclass, jint lpBaseAddress) {
BOOL bRet = UnmapViewOfFile(reinterpret_cast<PVOID>(lpBaseAddress));
if(!bRet) ErrorHandler(_T("Can not unmap view of file"));
return bRet;
}
JNIEXPORT jboolean JNICALL Java_com_stanley_memmap_MemMapFile_closeHandle
(JNIEnv *, jclass, jint hObject) {
return CloseHandle(reinterpret_cast<HANDLE>(hObject));
}
JNIEXPORT void JNICALL Java_com_stanley_memmap_MemMapFile_writeToMem
(JNIEnv * pEnv, jclass, jint lpBaseAddress, jstring content) {
LPCSTR pszContent = pEnv->GetStringUTFChars(content, NULL);
PVOID pView = reinterpret_cast<PVOID>(lpBaseAddress);
LPSTR pszCopy = reinterpret_cast<LPSTR>(pView);
lstrcpy(pszCopy, pszContent);
pEnv->ReleaseStringUTFChars(content, pszContent);
}
JNIEXPORT jstring JNICALL Java_com_stanley_memmap_MemMapFile_readFromMem
(JNIEnv * pEnv, jclass, jint lpBaseAddress) {
PVOID pView = reinterpret_cast<PVOID>(lpBaseAddress);
LPSTR pszContent = reinterpret_cast<LPSTR>(pView);
return pEnv->NewStringUTF(pszContent);
}
JNIEXPORT void JNICALL Java_com_stanley_memmap_MemMapFile_broadcast
(JNIEnv *, jclass) {
SendMessage(HWND_BROADCAST, UWM_DATA_READY, 0, 0);
}
package com.stanley.memmap;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author Stanley Wang
* @version 1.0
*/
public class MemMapFile {
public static final int PAGE_READONLY = 0x02;
public static final int PAGE_READWRITE = 0x04;
public static final int PAGE_WRITECOPY = 0x08;
public static final int FILE_MAP_COPY = 0x0001;
public static final int FILE_MAP_WRITE = 0x0002;
public static final int FILE_MAP_READ = 0x0004;
static {
System.loadLibrary("MemMapLib");
}
private MemMapFile() {
}
public static native int createFileMapping(int lProtect, int dwMaximumSizeHigh, int dwMaximumSizeLow, String name);
public static native int openFileMapping(int dwDesiredAccess, boolean bInheritHandle, String name);
public static native int mapViewOfFile(int hFileMappingObj, int dwDesiredAccess, int dwFileOffsetHigh, int dwFileOffsetLow, int dwNumberOfBytesToMap);
public static native boolean unmapViewOfFile(int lpBaseAddress);
public static native void writeToMem(int lpBaseAddress, String content);
public static native String readFromMem(int lpBaseAddress);
public static native boolean closeHandle(int hObject);
public static native void broadcast();
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.