Snapshot, replace tabs with spaces
This commit is contained in:
@@ -3,87 +3,87 @@
|
||||
#include <tlhelp32.h>
|
||||
|
||||
bool getProcessEntry32(const char *program, PROCESSENTRY32 *pe32) {
|
||||
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
||||
if (hSnapshot == INVALID_HANDLE_VALUE) {
|
||||
return false;
|
||||
}
|
||||
bool bFound = false;
|
||||
while (Process32Next(hSnapshot, pe32) != false) {
|
||||
if (strcmp(program, pe32->szExeFile) == 0) {
|
||||
bFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
CloseHandle(hSnapshot);
|
||||
return bFound;
|
||||
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
||||
if (hSnapshot == INVALID_HANDLE_VALUE) {
|
||||
return false;
|
||||
}
|
||||
bool bFound = false;
|
||||
while (Process32Next(hSnapshot, pe32) != false) {
|
||||
if (strcmp(program, pe32->szExeFile) == 0) {
|
||||
bFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
CloseHandle(hSnapshot);
|
||||
return bFound;
|
||||
}
|
||||
|
||||
bool getProcess(const char *program, HANDLE *hProcess) {
|
||||
PROCESSENTRY32 *pe32 = new PROCESSENTRY32;
|
||||
bool bResult = false;
|
||||
if (getProcessEntry32(program, pe32)) {
|
||||
*hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pe32->th32ProcessID);
|
||||
bResult = true;
|
||||
}
|
||||
delete pe32;
|
||||
return bResult;
|
||||
PROCESSENTRY32 *pe32 = new PROCESSENTRY32;
|
||||
bool bResult = false;
|
||||
if (getProcessEntry32(program, pe32)) {
|
||||
*hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pe32->th32ProcessID);
|
||||
bResult = true;
|
||||
}
|
||||
delete pe32;
|
||||
return bResult;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_mimis_util_Native_getHandle(JNIEnv *env, jclass cls, jstring jwindow) {
|
||||
const char *window = env->GetStringUTFChars(jwindow, 0);
|
||||
return (int) FindWindow(window, NULL);
|
||||
const char *window = env->GetStringUTFChars(jwindow, 0);
|
||||
return (int) FindWindow(window, NULL);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_mimis_util_Native_sendMessage(JNIEnv *env, jclass cls, jint handle, jint message, jint wParam, jint lParam) {
|
||||
return SendMessage((HWND) handle, message, wParam, lParam);
|
||||
return SendMessage((HWND) handle, message, wParam, lParam);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_mimis_util_Native_postMessage(JNIEnv *env, jclass cls, jint handle, jint message, jint wParam, jint lParam) {
|
||||
return PostMessage((HWND) handle, message, wParam, lParam);
|
||||
return PostMessage((HWND) handle, message, wParam, lParam);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_mimis_util_Native_mapVirtualKey(JNIEnv *env, jclass cls, jint map, jint type) {
|
||||
return MapVirtualKey(map, type);
|
||||
return MapVirtualKey(map, type);
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_mimis_util_Native_isRunning(JNIEnv *env, jclass cls, jstring jprogram) {
|
||||
const char *program = env->GetStringUTFChars(jprogram, 0);
|
||||
PROCESSENTRY32 *pe32 = new PROCESSENTRY32;
|
||||
bool bRunning = getProcessEntry32(program, pe32);
|
||||
delete pe32;
|
||||
return bRunning;
|
||||
const char *program = env->GetStringUTFChars(jprogram, 0);
|
||||
PROCESSENTRY32 *pe32 = new PROCESSENTRY32;
|
||||
bool bRunning = getProcessEntry32(program, pe32);
|
||||
delete pe32;
|
||||
return bRunning;
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_mimis_util_Native_terminate(JNIEnv *env, jclass cls, jstring jprogram) {
|
||||
const char *program = env->GetStringUTFChars(jprogram, 0);
|
||||
HANDLE *hProcess = new HANDLE;
|
||||
bool bResult = false;
|
||||
if (getProcess(program, hProcess)) {
|
||||
bResult = TerminateProcess(*hProcess, 0);
|
||||
}
|
||||
delete hProcess;
|
||||
return bResult;
|
||||
const char *program = env->GetStringUTFChars(jprogram, 0);
|
||||
HANDLE *hProcess = new HANDLE;
|
||||
bool bResult = false;
|
||||
if (getProcess(program, hProcess)) {
|
||||
bResult = TerminateProcess(*hProcess, 0);
|
||||
}
|
||||
delete hProcess;
|
||||
return bResult;
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL Java_mimis_util_Native_getValue(JNIEnv *env, jclass cls, jint registry, jstring jkey, jstring jname) {
|
||||
const char *key = env->GetStringUTFChars(jkey, 0);
|
||||
const char *name = env->GetStringUTFChars(jname, 0);
|
||||
HKEY hKey;
|
||||
char *value = NULL;
|
||||
if (RegOpenKey((HKEY) registry, key, &hKey) == ERROR_SUCCESS) {
|
||||
char nameBuffer[255];
|
||||
byte valueBuffer[255];
|
||||
DWORD dwNameSize = sizeof(nameBuffer);
|
||||
DWORD dwValueSize = sizeof(valueBuffer);
|
||||
int i = 0;
|
||||
while (RegEnumValue(hKey, i++, nameBuffer, &dwNameSize, NULL, NULL, valueBuffer, &dwValueSize) == ERROR_SUCCESS) {
|
||||
if (strcmp(name, nameBuffer) == 0) {
|
||||
value = (char*) valueBuffer;
|
||||
break;
|
||||
}
|
||||
dwNameSize = sizeof(nameBuffer);
|
||||
}
|
||||
}
|
||||
RegCloseKey(hKey);
|
||||
return env->NewStringUTF(value);
|
||||
const char *key = env->GetStringUTFChars(jkey, 0);
|
||||
const char *name = env->GetStringUTFChars(jname, 0);
|
||||
HKEY hKey;
|
||||
char *value = NULL;
|
||||
if (RegOpenKey((HKEY) registry, key, &hKey) == ERROR_SUCCESS) {
|
||||
char nameBuffer[255];
|
||||
byte valueBuffer[255];
|
||||
DWORD dwNameSize = sizeof(nameBuffer);
|
||||
DWORD dwValueSize = sizeof(valueBuffer);
|
||||
int i = 0;
|
||||
while (RegEnumValue(hKey, i++, nameBuffer, &dwNameSize, NULL, NULL, valueBuffer, &dwValueSize) == ERROR_SUCCESS) {
|
||||
if (strcmp(name, nameBuffer) == 0) {
|
||||
value = (char*) valueBuffer;
|
||||
break;
|
||||
}
|
||||
dwNameSize = sizeof(nameBuffer);
|
||||
}
|
||||
}
|
||||
RegCloseKey(hKey);
|
||||
return env->NewStringUTF(value);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import mimis.value.Windows;
|
||||
|
||||
public class Native {
|
||||
static {
|
||||
com.github.boukefalos.jlibloader.Native.load("com.github.boukefalos", "jlibmimis");
|
||||
com.github.boukefalos.jlibloader.Native.load("com.github.boukefalos", "jlibmimis");
|
||||
}
|
||||
|
||||
public native static int getHandle(String window);
|
||||
|
||||
@@ -3,190 +3,190 @@
|
||||
#include <jni.h>
|
||||
#include "pipe.h"
|
||||
|
||||
#define DEBUG 0
|
||||
#define DEBUG 0
|
||||
|
||||
JNIEXPORT jint JNICALL Java_pipe_Pipe_CreateNamedPipe(JNIEnv *env,
|
||||
jclass className, jstring sPipeName, jint dwOpenMode, jint dwPipeMode,
|
||||
jint nMaxInstances, jint nOutBufferSize, jint nInBufferSize,
|
||||
jint nDefaultTimeOut, jint lpSecurityAttributes) {
|
||||
HANDLE pipeHandler;
|
||||
LPCSTR pipeName;
|
||||
pipeName = (*env)->GetStringUTFChars(env, sPipeName, NULL );
|
||||
if (pipeName == NULL )
|
||||
return -1;
|
||||
jclass className, jstring sPipeName, jint dwOpenMode, jint dwPipeMode,
|
||||
jint nMaxInstances, jint nOutBufferSize, jint nInBufferSize,
|
||||
jint nDefaultTimeOut, jint lpSecurityAttributes) {
|
||||
HANDLE pipeHandler;
|
||||
LPCSTR pipeName;
|
||||
pipeName = (*env)->GetStringUTFChars(env, sPipeName, NULL );
|
||||
if (pipeName == NULL )
|
||||
return -1;
|
||||
|
||||
if (DEBUG) {
|
||||
printf("Native: Pipe Name %s\n", pipeName);
|
||||
printf("Native: dwOpenMode %d\n", dwOpenMode);
|
||||
printf("Native: dwPipeMode %d\n", dwPipeMode);
|
||||
printf("Native: nMaxInstances %d\n", nMaxInstances);
|
||||
printf("Native: nOutBufferSize %d\n", nOutBufferSize);
|
||||
printf("Native: nInBufferSize %d\n", nInBufferSize);
|
||||
printf("Native: nDefaultTimeOut %d\n", nDefaultTimeOut);
|
||||
}
|
||||
if (DEBUG) {
|
||||
printf("Native: Pipe Name %s\n", pipeName);
|
||||
printf("Native: dwOpenMode %d\n", dwOpenMode);
|
||||
printf("Native: dwPipeMode %d\n", dwPipeMode);
|
||||
printf("Native: nMaxInstances %d\n", nMaxInstances);
|
||||
printf("Native: nOutBufferSize %d\n", nOutBufferSize);
|
||||
printf("Native: nInBufferSize %d\n", nInBufferSize);
|
||||
printf("Native: nDefaultTimeOut %d\n", nDefaultTimeOut);
|
||||
}
|
||||
|
||||
pipeHandler = CreateNamedPipe((LPCSTR) pipeName, dwOpenMode, dwPipeMode,
|
||||
nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut,
|
||||
(LPSECURITY_ATTRIBUTES) lpSecurityAttributes);
|
||||
pipeHandler = CreateNamedPipe((LPCSTR) pipeName, dwOpenMode, dwPipeMode,
|
||||
nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut,
|
||||
(LPSECURITY_ATTRIBUTES) lpSecurityAttributes);
|
||||
|
||||
(*env)->ReleaseStringUTFChars(env, sPipeName, pipeName);
|
||||
return (jint) pipeHandler;
|
||||
(*env)->ReleaseStringUTFChars(env, sPipeName, pipeName);
|
||||
return (jint) pipeHandler;
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_pipe_Pipe_ConnectNamedPipe(JNIEnv *env,
|
||||
jclass className, jint hNamedPipe, jint lpOverlapped) {
|
||||
BOOL fConnected;
|
||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||
fConnected = ConnectNamedPipe(pipeHandler, (LPOVERLAPPED) lpOverlapped);
|
||||
return fConnected;
|
||||
jclass className, jint hNamedPipe, jint lpOverlapped) {
|
||||
BOOL fConnected;
|
||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||
fConnected = ConnectNamedPipe(pipeHandler, (LPOVERLAPPED) lpOverlapped);
|
||||
return fConnected;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_pipe_Pipe_GetLastError(JNIEnv *env,
|
||||
jclass className) {
|
||||
DWORD errorNumber = GetLastError();
|
||||
return (jint) errorNumber;
|
||||
jclass className) {
|
||||
DWORD errorNumber = GetLastError();
|
||||
return (jint) errorNumber;
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_pipe_Pipe_CloseHandle(JNIEnv *env,
|
||||
jclass className, jint hNamedPipe) {
|
||||
BOOL result;
|
||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||
result = CloseHandle(pipeHandler);
|
||||
return result;
|
||||
jclass className, jint hNamedPipe) {
|
||||
BOOL result;
|
||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||
result = CloseHandle(pipeHandler);
|
||||
return result;
|
||||
}
|
||||
|
||||
JNIEXPORT jbyteArray JNICALL Java_pipe_Pipe_ReadFile(JNIEnv *env,
|
||||
jclass className, jint hNamedPipe, jint nNumberOfBytesToRead) {
|
||||
int bytesRead = 0;
|
||||
BOOL result;
|
||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||
LPVOID buffer;
|
||||
jbyteArray lpBuffer;
|
||||
jclass className, jint hNamedPipe, jint nNumberOfBytesToRead) {
|
||||
int bytesRead = 0;
|
||||
BOOL result;
|
||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||
LPVOID buffer;
|
||||
jbyteArray lpBuffer;
|
||||
|
||||
buffer = (LPVOID) LocalAlloc(LMEM_ZEROINIT, nNumberOfBytesToRead);
|
||||
buffer = (LPVOID) LocalAlloc(LMEM_ZEROINIT, nNumberOfBytesToRead);
|
||||
|
||||
if (DEBUG) {
|
||||
printf(
|
||||
"Native: Before ReadFile pipeHandler %d nNumberOfBytesToRead %d\n",
|
||||
pipeHandler, nNumberOfBytesToRead);
|
||||
}
|
||||
result = ReadFile(pipeHandler, (LPVOID) buffer,
|
||||
(DWORD) nNumberOfBytesToRead, &bytesRead, (LPOVERLAPPED) 0);
|
||||
if (result) {
|
||||
lpBuffer = (*env)->NewByteArray(env, (jsize) bytesRead);
|
||||
(*env)->SetByteArrayRegion(env, lpBuffer, 0, (jsize) bytesRead,
|
||||
(jbyte *) buffer);
|
||||
} else
|
||||
bytesRead = 0;
|
||||
if (DEBUG) {
|
||||
printf(
|
||||
"Native: Before ReadFile pipeHandler %d nNumberOfBytesToRead %d\n",
|
||||
pipeHandler, nNumberOfBytesToRead);
|
||||
}
|
||||
result = ReadFile(pipeHandler, (LPVOID) buffer,
|
||||
(DWORD) nNumberOfBytesToRead, &bytesRead, (LPOVERLAPPED) 0);
|
||||
if (result) {
|
||||
lpBuffer = (*env)->NewByteArray(env, (jsize) bytesRead);
|
||||
(*env)->SetByteArrayRegion(env, lpBuffer, 0, (jsize) bytesRead,
|
||||
(jbyte *) buffer);
|
||||
} else
|
||||
bytesRead = 0;
|
||||
|
||||
LocalFree(buffer);
|
||||
LocalFree(buffer);
|
||||
|
||||
if (DEBUG) {
|
||||
printf("Native: After ReadFile BytesRead %d\n", bytesRead);
|
||||
}
|
||||
return lpBuffer;
|
||||
if (DEBUG) {
|
||||
printf("Native: After ReadFile BytesRead %d\n", bytesRead);
|
||||
}
|
||||
return lpBuffer;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_pipe_Pipe_WriteFile(JNIEnv *env, jclass className,
|
||||
jint hNamedPipe, jbyteArray lpBuffer, jint nNumberOfBytesToWrite) {
|
||||
int bytesWritten = 0;
|
||||
BOOL result;
|
||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||
LPVOID buffer;
|
||||
jint hNamedPipe, jbyteArray lpBuffer, jint nNumberOfBytesToWrite) {
|
||||
int bytesWritten = 0;
|
||||
BOOL result;
|
||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||
LPVOID buffer;
|
||||
|
||||
buffer = (LPVOID) LocalAlloc(LMEM_ZEROINIT, nNumberOfBytesToWrite);
|
||||
buffer = (LPVOID) LocalAlloc(LMEM_ZEROINIT, nNumberOfBytesToWrite);
|
||||
|
||||
(*env)->GetByteArrayRegion(env, lpBuffer, 0, nNumberOfBytesToWrite, buffer);
|
||||
result = WriteFile(pipeHandler, buffer, (DWORD) nNumberOfBytesToWrite,
|
||||
(LPDWORD) &bytesWritten, (LPOVERLAPPED) 0);
|
||||
LocalFree(buffer);
|
||||
(*env)->GetByteArrayRegion(env, lpBuffer, 0, nNumberOfBytesToWrite, buffer);
|
||||
result = WriteFile(pipeHandler, buffer, (DWORD) nNumberOfBytesToWrite,
|
||||
(LPDWORD) &bytesWritten, (LPOVERLAPPED) 0);
|
||||
LocalFree(buffer);
|
||||
|
||||
if (DEBUG) {
|
||||
printf("Native: After WriteFile BytesReadWritten %d\n", bytesWritten);
|
||||
}
|
||||
if (DEBUG) {
|
||||
printf("Native: After WriteFile BytesReadWritten %d\n", bytesWritten);
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
if (GetLastError() != ERROR_IO_PENDING)
|
||||
result = 0;
|
||||
else
|
||||
result = 1;
|
||||
}
|
||||
if (!result) {
|
||||
bytesWritten = -1;
|
||||
}
|
||||
return bytesWritten;
|
||||
if (!result) {
|
||||
if (GetLastError() != ERROR_IO_PENDING)
|
||||
result = 0;
|
||||
else
|
||||
result = 1;
|
||||
}
|
||||
if (!result) {
|
||||
bytesWritten = -1;
|
||||
}
|
||||
return bytesWritten;
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_pipe_Pipe_FlushFileBuffers(JNIEnv *env,
|
||||
jclass className, jint hNamedPipe) {
|
||||
BOOL result;
|
||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||
result = FlushFileBuffers(pipeHandler);
|
||||
return result;
|
||||
jclass className, jint hNamedPipe) {
|
||||
BOOL result;
|
||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||
result = FlushFileBuffers(pipeHandler);
|
||||
return result;
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_pipe_Pipe_DisconnectNamedPipe(JNIEnv *env,
|
||||
jclass className, jint hNamedPipe) {
|
||||
BOOL result;
|
||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||
result = DisconnectNamedPipe(pipeHandler);
|
||||
return result;
|
||||
jclass className, jint hNamedPipe) {
|
||||
BOOL result;
|
||||
HANDLE pipeHandler = (HANDLE) hNamedPipe;
|
||||
result = DisconnectNamedPipe(pipeHandler);
|
||||
return result;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_pipe_Pipe_CreateFile(JNIEnv *env, jclass className,
|
||||
jstring lpFileName, jint dwDesiredAccess, jint dwShareMode,
|
||||
jint lpSecurityAttributes, jint dwCreationDisposition,
|
||||
jint dwFlagsAndAttributes, jint hTemplateFile) {
|
||||
HANDLE pipeHandler;
|
||||
const jbyte *fileName;
|
||||
fileName = (*env)->GetStringUTFChars(env, lpFileName, NULL );
|
||||
if (fileName == NULL )
|
||||
return -1;
|
||||
pipeHandler = CreateFile((LPCSTR) fileName, (DWORD) dwDesiredAccess,
|
||||
(DWORD) dwShareMode, (LPSECURITY_ATTRIBUTES) lpSecurityAttributes,
|
||||
(DWORD) dwCreationDisposition, (DWORD) dwFlagsAndAttributes,
|
||||
(HANDLE) hTemplateFile);
|
||||
return (jint) pipeHandler;
|
||||
jstring lpFileName, jint dwDesiredAccess, jint dwShareMode,
|
||||
jint lpSecurityAttributes, jint dwCreationDisposition,
|
||||
jint dwFlagsAndAttributes, jint hTemplateFile) {
|
||||
HANDLE pipeHandler;
|
||||
const jbyte *fileName;
|
||||
fileName = (*env)->GetStringUTFChars(env, lpFileName, NULL );
|
||||
if (fileName == NULL )
|
||||
return -1;
|
||||
pipeHandler = CreateFile((LPCSTR) fileName, (DWORD) dwDesiredAccess,
|
||||
(DWORD) dwShareMode, (LPSECURITY_ATTRIBUTES) lpSecurityAttributes,
|
||||
(DWORD) dwCreationDisposition, (DWORD) dwFlagsAndAttributes,
|
||||
(HANDLE) hTemplateFile);
|
||||
return (jint) pipeHandler;
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_pipe_Pipe_WaitNamedPipe(JNIEnv *env,
|
||||
jclass className, jstring lpNamedPipeName, jint nTimeOut) {
|
||||
BOOL result;
|
||||
const jbyte *pipeName;
|
||||
pipeName = (*env)->GetStringUTFChars(env, lpNamedPipeName, NULL );
|
||||
if (pipeName == NULL )
|
||||
return 0;
|
||||
result = WaitNamedPipe((LPCSTR) pipeName, (DWORD) nTimeOut);
|
||||
return result;
|
||||
jclass className, jstring lpNamedPipeName, jint nTimeOut) {
|
||||
BOOL result;
|
||||
const jbyte *pipeName;
|
||||
pipeName = (*env)->GetStringUTFChars(env, lpNamedPipeName, NULL );
|
||||
if (pipeName == NULL )
|
||||
return 0;
|
||||
result = WaitNamedPipe((LPCSTR) pipeName, (DWORD) nTimeOut);
|
||||
return result;
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL Java_pipe_Pipe_FormatMessage(JNIEnv *env,
|
||||
jclass className, jint errorCode) {
|
||||
LPVOID lpMsgBuf;
|
||||
LPVOID lpDisplayBuf;
|
||||
DWORD dw = (DWORD) errorCode;
|
||||
jclass className, jint errorCode) {
|
||||
LPVOID lpMsgBuf;
|
||||
LPVOID lpDisplayBuf;
|
||||
DWORD dw = (DWORD) errorCode;
|
||||
|
||||
FormatMessage(
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
|
||||
| FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0,
|
||||
NULL );
|
||||
FormatMessage(
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
|
||||
| FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0,
|
||||
NULL );
|
||||
|
||||
lpDisplayBuf = (LPVOID) LocalAlloc(LMEM_ZEROINIT,
|
||||
(lstrlen((LPCTSTR) lpMsgBuf) + 40) * sizeof(TCHAR));
|
||||
StringCchPrintf((LPTSTR) lpDisplayBuf,
|
||||
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
|
||||
TEXT("Failed with error %d: %s"), dw, lpMsgBuf);
|
||||
return (jstring) (*env)->NewStringUTF(env, lpDisplayBuf);
|
||||
lpDisplayBuf = (LPVOID) LocalAlloc(LMEM_ZEROINIT,
|
||||
(lstrlen((LPCTSTR) lpMsgBuf) + 40) * sizeof(TCHAR));
|
||||
StringCchPrintf((LPTSTR) lpDisplayBuf,
|
||||
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
|
||||
TEXT("Failed with error %d: %s"), dw, lpMsgBuf);
|
||||
return (jstring) (*env)->NewStringUTF(env, lpDisplayBuf);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_pipe_Pipe_Print(JNIEnv *env, jclass className,
|
||||
jstring lpMsgBuf) {
|
||||
const jbyte *str;
|
||||
str = (*env)->GetStringUTFChars(env, lpMsgBuf, NULL );
|
||||
if (str == NULL )
|
||||
return;
|
||||
printf("Native: %s\n", str);
|
||||
(*env)->ReleaseStringUTFChars(env, lpMsgBuf, str);
|
||||
return;
|
||||
jstring lpMsgBuf) {
|
||||
const jbyte *str;
|
||||
str = (*env)->GetStringUTFChars(env, lpMsgBuf, NULL );
|
||||
if (str == NULL )
|
||||
return;
|
||||
printf("Native: %s\n", str);
|
||||
(*env)->ReleaseStringUTFChars(env, lpMsgBuf, str);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -6,51 +6,51 @@ import com.github.boukefalos.jlibloader.Native;
|
||||
* @author Vikram S Khatri vikram.khatri@us.ibm.com
|
||||
*/
|
||||
public class Pipe {
|
||||
static final int ERROR_PIPE_CONNECTED = 535;
|
||||
static final int ERROR_BROKEN_PIPE = 109;
|
||||
static final int PIPE_ACCESS_DUPLEX = 0x00000003;
|
||||
static final int PIPE_WAIT = 0x00000000;
|
||||
|
||||
static {
|
||||
Native.load("com.github.boukefalos", "jlibpipe");
|
||||
}
|
||||
static final int ERROR_PIPE_CONNECTED = 535;
|
||||
static final int ERROR_BROKEN_PIPE = 109;
|
||||
static final int PIPE_ACCESS_DUPLEX = 0x00000003;
|
||||
static final int PIPE_WAIT = 0x00000000;
|
||||
|
||||
static {
|
||||
Native.load("com.github.boukefalos", "jlibpipe");
|
||||
}
|
||||
|
||||
public static final native int CreateNamedPipe(
|
||||
String pipeName,
|
||||
int ppenMode,
|
||||
int pipeMode,
|
||||
int maxInstances,
|
||||
int outBufferSize,
|
||||
int inBufferSize,
|
||||
int defaultTimeOut,
|
||||
int securityAttributes);
|
||||
public static final native int CreateNamedPipe(
|
||||
String pipeName,
|
||||
int ppenMode,
|
||||
int pipeMode,
|
||||
int maxInstances,
|
||||
int outBufferSize,
|
||||
int inBufferSize,
|
||||
int defaultTimeOut,
|
||||
int securityAttributes);
|
||||
|
||||
public static final native boolean ConnectNamedPipe(int namedPipeHandle, int overlapped);
|
||||
public static final native boolean ConnectNamedPipe(int namedPipeHandle, int overlapped);
|
||||
|
||||
public static final native int GetLastError();
|
||||
public static final native int GetLastError();
|
||||
|
||||
public static final native boolean CloseHandle(int bbject);
|
||||
public static final native boolean CloseHandle(int bbject);
|
||||
|
||||
public static final native byte[] ReadFile(int file, int numberOfBytesToRead);
|
||||
public static final native byte[] ReadFile(int file, int numberOfBytesToRead);
|
||||
|
||||
public static final native int WriteFile(int file, byte[] buffer, int numberOfBytesToWrite);
|
||||
public static final native int WriteFile(int file, byte[] buffer, int numberOfBytesToWrite);
|
||||
|
||||
public static final native boolean FlushFileBuffers(int file);
|
||||
public static final native boolean FlushFileBuffers(int file);
|
||||
|
||||
public static final native boolean DisconnectNamedPipe(int namedPipeHandle);
|
||||
public static final native boolean DisconnectNamedPipe(int namedPipeHandle);
|
||||
|
||||
public static final native int CreateFile(
|
||||
String fileName,
|
||||
int desiredAccess,
|
||||
int shareMode,
|
||||
int securityAttributes,
|
||||
int creationDisposition,
|
||||
int flagsAndAttributes,
|
||||
int templateFile);
|
||||
public static final native int CreateFile(
|
||||
String fileName,
|
||||
int desiredAccess,
|
||||
int shareMode,
|
||||
int securityAttributes,
|
||||
int creationDisposition,
|
||||
int flagsAndAttributes,
|
||||
int templateFile);
|
||||
|
||||
public static final native boolean WaitNamedPipe(String namedPipeName, int timeOut);
|
||||
public static final native boolean WaitNamedPipe(String namedPipeName, int timeOut);
|
||||
|
||||
public static final native String FormatMessage(int errorCode);
|
||||
public static final native String FormatMessage(int errorCode);
|
||||
|
||||
public static final native void Print(String message);
|
||||
public static final native void Print(String message);
|
||||
}
|
||||
|
||||
@@ -3,22 +3,22 @@ package pipe;
|
||||
import java.io.RandomAccessFile;
|
||||
|
||||
public class Client {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// Connect to the pipe
|
||||
RandomAccessFile pipe = new RandomAccessFile("\\\\.\\pipe\\detest", "rw");
|
||||
String echoText = "Hello word\n";
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// Connect to the pipe
|
||||
RandomAccessFile pipe = new RandomAccessFile("\\\\.\\pipe\\detest", "rw");
|
||||
String echoText = "Hello word\n";
|
||||
|
||||
// write to pipe
|
||||
pipe.write(echoText.getBytes());
|
||||
// write to pipe
|
||||
pipe.write(echoText.getBytes());
|
||||
|
||||
// read response
|
||||
String echoResponse = pipe.readLine();
|
||||
System.out.println(echoResponse);
|
||||
pipe.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// read response
|
||||
String echoResponse = pipe.readLine();
|
||||
System.out.println(echoResponse);
|
||||
pipe.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,81 +6,81 @@ import java.io.InputStream;
|
||||
|
||||
public class TestPipe {
|
||||
|
||||
private int namedPipeHandle;
|
||||
private String pipeName, srcFile;
|
||||
private int pipeBuffer = 131072, fileBuffer = 8192;
|
||||
private int namedPipeHandle;
|
||||
private String pipeName, srcFile;
|
||||
private int pipeBuffer = 131072, fileBuffer = 8192;
|
||||
|
||||
public TestPipe(String pipeName, String srcFile) {
|
||||
this.pipeName = pipeName;
|
||||
this.srcFile = srcFile;
|
||||
}
|
||||
public TestPipe(String pipeName, String srcFile) {
|
||||
this.pipeName = pipeName;
|
||||
this.srcFile = srcFile;
|
||||
}
|
||||
|
||||
private void log(String message) {
|
||||
System.out.println(message);
|
||||
}
|
||||
private void log(String message) {
|
||||
System.out.println(message);
|
||||
}
|
||||
|
||||
private boolean createPipe() {
|
||||
namedPipeHandle = Pipe.CreateNamedPipe(
|
||||
pipeName,
|
||||
Pipe.PIPE_ACCESS_DUPLEX,
|
||||
Pipe.PIPE_WAIT,
|
||||
5,
|
||||
pipeBuffer,
|
||||
pipeBuffer,
|
||||
0xffffffff,
|
||||
0);
|
||||
if (namedPipeHandle == -1) {
|
||||
log("CreateNamedPipe failed for " + pipeName + " for error Message " + Pipe.FormatMessage(Pipe.GetLastError()));
|
||||
} else {
|
||||
log("Named Pipe " + pipeName + " created successfully Handle=" + namedPipeHandle);
|
||||
}
|
||||
return namedPipeHandle != -1;
|
||||
}
|
||||
private boolean createPipe() {
|
||||
namedPipeHandle = Pipe.CreateNamedPipe(
|
||||
pipeName,
|
||||
Pipe.PIPE_ACCESS_DUPLEX,
|
||||
Pipe.PIPE_WAIT,
|
||||
5,
|
||||
pipeBuffer,
|
||||
pipeBuffer,
|
||||
0xffffffff,
|
||||
0);
|
||||
if (namedPipeHandle == -1) {
|
||||
log("CreateNamedPipe failed for " + pipeName + " for error Message " + Pipe.FormatMessage(Pipe.GetLastError()));
|
||||
} else {
|
||||
log("Named Pipe " + pipeName + " created successfully Handle=" + namedPipeHandle);
|
||||
}
|
||||
return namedPipeHandle != -1;
|
||||
}
|
||||
|
||||
private boolean connectToPipe() {
|
||||
log("Waiting for a client to connect to pipe " + pipeName);
|
||||
boolean connected = Pipe.ConnectNamedPipe(namedPipeHandle, 0);
|
||||
if (!connected) {
|
||||
int lastError = Pipe.GetLastError();
|
||||
if (lastError == Pipe.ERROR_PIPE_CONNECTED)
|
||||
connected = true;
|
||||
}
|
||||
log((connected ? "Connected to the pipe " : "Falied to connect to the pipe ") + pipeName);
|
||||
return connected;
|
||||
}
|
||||
private boolean connectToPipe() {
|
||||
log("Waiting for a client to connect to pipe " + pipeName);
|
||||
boolean connected = Pipe.ConnectNamedPipe(namedPipeHandle, 0);
|
||||
if (!connected) {
|
||||
int lastError = Pipe.GetLastError();
|
||||
if (lastError == Pipe.ERROR_PIPE_CONNECTED)
|
||||
connected = true;
|
||||
}
|
||||
log((connected ? "Connected to the pipe " : "Falied to connect to the pipe ") + pipeName);
|
||||
return connected;
|
||||
}
|
||||
|
||||
public void runPipe() {
|
||||
if (createPipe() && connectToPipe()) {
|
||||
log("Client connected.");
|
||||
try {
|
||||
File f1 = new File(this.srcFile);
|
||||
InputStream in = new FileInputStream(f1);
|
||||
log("Sending data to the pipe");
|
||||
byte[] buf = new byte[fileBuffer];
|
||||
int len, bytesWritten;
|
||||
while ((len = in.read(buf)) > 0) {
|
||||
bytesWritten = Pipe.WriteFile(namedPipeHandle, buf, len);
|
||||
log("Sent " + len + "/" + bytesWritten + " bytes to the pipe");
|
||||
if (bytesWritten == -1) {
|
||||
int errorNumber = Pipe.GetLastError();
|
||||
log("Error Writing to pipe " + Pipe.FormatMessage(errorNumber));
|
||||
}
|
||||
}
|
||||
in.close();
|
||||
Pipe.FlushFileBuffers(namedPipeHandle);
|
||||
Pipe.CloseHandle(namedPipeHandle);
|
||||
Pipe.DisconnectNamedPipe(namedPipeHandle);
|
||||
log("Writing to the pipe completed.");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
public void runPipe() {
|
||||
if (createPipe() && connectToPipe()) {
|
||||
log("Client connected.");
|
||||
try {
|
||||
File f1 = new File(this.srcFile);
|
||||
InputStream in = new FileInputStream(f1);
|
||||
log("Sending data to the pipe");
|
||||
byte[] buf = new byte[fileBuffer];
|
||||
int len, bytesWritten;
|
||||
while ((len = in.read(buf)) > 0) {
|
||||
bytesWritten = Pipe.WriteFile(namedPipeHandle, buf, len);
|
||||
log("Sent " + len + "/" + bytesWritten + " bytes to the pipe");
|
||||
if (bytesWritten == -1) {
|
||||
int errorNumber = Pipe.GetLastError();
|
||||
log("Error Writing to pipe " + Pipe.FormatMessage(errorNumber));
|
||||
}
|
||||
}
|
||||
in.close();
|
||||
Pipe.FlushFileBuffers(namedPipeHandle);
|
||||
Pipe.CloseHandle(namedPipeHandle);
|
||||
Pipe.DisconnectNamedPipe(namedPipeHandle);
|
||||
log("Writing to the pipe completed.");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String pipeName = "\\\\.\\pipe\\detest";
|
||||
String fileName = "C:\\Users\\Rik\\Music\\Artists\\+44\\When Your Heart Stops Beating\\+44 - 155.mp3";
|
||||
TestPipe testPipe = new TestPipe(pipeName, fileName);
|
||||
testPipe.runPipe();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
String pipeName = "\\\\.\\pipe\\detest";
|
||||
String fileName = "C:\\Users\\Rik\\Music\\Artists\\+44\\When Your Heart Stops Beating\\+44 - 155.mp3";
|
||||
TestPipe testPipe = new TestPipe(pipeName, fileName);
|
||||
testPipe.runPipe();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user