MozillaZine

ctypes.open() couldn't open dll library

Talk about add-ons and extension development.
metju312
 
Posts: 10
Joined: September 6th, 2017, 1:25 am

Post Posted September 11th, 2017, 9:26 am

Hi,

How to open dll library using writing addon not packed to xpi.

I already try this:
Code: Select all
const files = require('resource://km-services/file');
let env_path = files.getEnv('PATH');
files.setEnv('PATH', `${env_path};${myDll_dir}`);
let dll_content = ctypes.open(io.join(myDll_dir, dll_name));
files.setEnv('PATH', env_path);


And:
https://stackoverflow.com/questions/19382201/how-to-load-dll-from-sdk-addon-data-folder

And:
https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/Using_js-ctypes/ctypes.open#Custom_Native_File

Nothing works

Process Monitor show SUCCESS get myDll but still TB consol shows "couldn't open library"

Thanks for help

morat
 
Posts: 2450
Joined: February 3rd, 2009, 6:29 pm

Post Posted September 11th, 2017, 1:11 pm

metju312 wrote:Nothing works

I got ctypes working in Thunderbird.

Code: Select all
Components.utils.import("resource://gre/modules/ctypes.jsm");
var lib = ctypes.open("kernel32.dll");
var pid = lib.declare("GetCurrentProcessId", /* name        */
  ctypes.winapi_abi,                         /* abi         */
  ctypes.uint32_t);                          /* return type */
var ret = pid();
lib.close();
alert("PID " + ret);

GetCurrentProcessId function
http://msdn.microsoft.com/en-us/library/ms683180(v=vs.85).aspx

Thunderbird 52.3.0
Windows 7 SP1 32-bit

metju312
 
Posts: 10
Joined: September 6th, 2017, 1:25 am

Post Posted September 11th, 2017, 11:03 pm

Thanks morat - that work. I guess there are problems with my dll file.

morat
 
Posts: 2450
Joined: February 3rd, 2009, 6:29 pm

Post Posted September 12th, 2017, 1:49 am

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/ms682489(VS.85).aspx

Process32First function
http://msdn.microsoft.com/en-us/library/ms684834(VS.85).aspx

Process32Next function
http://msdn.microsoft.com/en-us/library/ms684836(VS.85).aspx

PROCESSENTRY32 structure
http://msdn.microsoft.com/en-us/library/ms684839(VS.85).aspx

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 1 guest