ctypes.open() couldn't open dll library

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

ctypes.open() couldn't open dll library

Post by metju312 »

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/193 ... ata-folder

And:
https://developer.mozilla.org/en-US/doc ... ative_File

Nothing works

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

Thanks for help
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: ctypes.open() couldn't open dll library

Post by morat »

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 ... s.85).aspx

Thunderbird 52.3.0
Windows 7 SP1 32-bit
metju312
Posts: 10
Joined: September 6th, 2017, 1:25 am

Re: ctypes.open() couldn't open dll library

Post by metju312 »

Thanks morat - that work. I guess there are problems with my dll file.
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: ctypes.open() couldn't open dll library

Post by morat »

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
Post Reply