Here is an example I got working in Firefox a few years ago.
I compiled the cpp file with Code::Blocks. It uses a MinGW port of GCC as its compiler.
Code: Select all
#include <windows.h>
#include <tlhelp32.h>
BOOL WINAPI DllMain( HINSTANCE instance, DWORD reason, LPVOID reserved )
{
return TRUE;
}
extern "C" BOOL IsProcessRunning( LPCWSTR filename )
{
HANDLE snapshot;
PROCESSENTRY32W processEntry;
snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( snapshot == INVALID_HANDLE_VALUE )
{
return( FALSE );
}
processEntry.dwSize = sizeof( PROCESSENTRY32W );
if( !Process32FirstW( snapshot, &processEntry ) )
{
CloseHandle( snapshot );
return( FALSE );
}
do
{
if( wcsicmp( filename, processEntry.szExeFile ) == 0 )
{
CloseHandle( snapshot );
return( TRUE );
}
} while( Process32NextW( snapshot, &processEntry ) );
CloseHandle( snapshot );
return( FALSE );
}
Code: Select all
Components.utils.import("resource://gre/modules/ctypes.jsm");
var lib = ctypes.open("C:\\IsProcessRunning.dll");
var isProcessRunning = lib.declare("IsProcessRunning", /* name */
ctypes.default_abi, /* abi */
ctypes.bool, /* return type */
ctypes.jschar.ptr); /* argument type */
var ret = isProcessRunning("calc.exe");
lib.close();
alert(ret);
C:\Windows\System32\notepad.exe IsProcessRunning.cpp
C:\Cpp\MinGW\bin\g++.exe -c IsProcessRunning.cpp
C:\Cpp\MinGW\bin\g++.exe -shared -o IsProcessRunning.dll IsProcessRunning.o -Wl,-e_DllMain@12
C:\Windows\System32\calc.exe
Code::Blocks
http://www.codeblocks.org/
IsProcessRunning
http://dxr.mozilla.org/mozilla-release/ ... essRunning
CreateToolhelp32Snapshot function
http://msdn.microsoft.com/en-us/library ... S.85).aspx
Process32First function
http://msdn.microsoft.com/en-us/library ... S.85).aspx
Process32Next function
http://msdn.microsoft.com/en-us/library ... S.85).aspx
PROCESSENTRY32 structure
http://msdn.microsoft.com/en-us/library ... S.85).aspx