GithubHelp home page GithubHelp logo

Comments (3)

Tatsh avatar Tatsh commented on June 7, 2024

I don't think there is a (nice) way. Can you use a Swift script instead?

This is not working but it's close, and can't be run in sandboxed Swift app. Fix SomeTypeHere to be the struct, initialise pidInfo to the correct type, etc.

#!/usr/bin/env swift
import Darwin

let handle = dlopen("/usr/lib/libc.dylib", RTLD_NOW)
let proc_pidinfoSym = dlsym(handle, "proc_pidinfo")

typealias proc_pidinfoFunc = @convention(c) (_ x: CInt, _ y: CInt, _ z: CInt, _ q: UnsafeMutablePointer<SomeTypeHere>, _ r: CInt) -> CInt
let proc_pidinfo = unsafeBitCast(proc_pidinfoSym, to: proc_pidinfoFunc.self)

let pids = ...
var pidInfo: CInt = 0
let infoSize = proc_pidinfo(Int32(pids[0]), PROC_PIDTBSDINFO, 0, &pidInfo, 32)
print(infoSize)

from jxa-cookbook.

Tatsh avatar Tatsh commented on June 7, 2024

This can be done with $.malloc.

#!/usr/bin/env osascript -l JavaScript
ObjC.import('Cocoa')
ObjC.import('stdlib')

let pids = [145]
ObjC.bindFunction('malloc', ['void*', ['int']])
let pidInfo = $.malloc(2000)
ObjC.bindFunction('proc_pidinfo', ['int',['int', 'int', 'Int64', 'void *', 'int']]);
$.proc_pidinfo(pids[0], 3, 0, pidInfo, 2000 );

let arr = []
for (let i = 0; i < 2000; i++) {
    arr.push(String.fromCharCode(pidInfo[i]))
}
console.log(arr)

The issue is that the fields are not going to be defined at all.

Example output with pids = [145] where 145 is the PID of loginwindow.

0,`,@,,,,,,,,,,�,,,,,,,,õ,,,,,,,,,,,,,,,,,,,,,,,,,,,,l,o,g,i,n,w,i,n,d,o,w,,,,,,l,o,g,i,n,w,i,n,d,o,w,,,,,,,,,,,,,,
,,,,,,,,d,,,,�,,,,,,,,ÿ,ÿ,ÿ,ÿ,,,,,,,,,�,n,
�,a,,,,,L,�,,,,,,,à,¼,~,,,`,,,,½,~,,,`,,, ,½,~,,,`,,,@,½,~,,,`,,,`,½,~,,
,`,,,�,½,~,,,`,,, ,½,~,,,`,,,À,½,~,,,`,,,à,½,~,,,`,,
,,¾,~,,,`,,, ,¾,~,,,`,,,@,¾,~,,,`,,,`,¾,~,,,`,,,�,¾,~,,,`,,,À,¾,~,,,`,,,à,¾,~,,,`,,,,¿
,~,,,`,,, ,¿,~,,,`,,,@,¿,~,,,`,,,�,¿,~,,,`,,, ,¿,~,,,`,,,À,¿,~,,,`,,,à,¿,~,,,`,,, ,º,},,,`,,,,
À,~,,,`,,, ,À,~,,,`,,,@,À,~,,,`,,,`,À,~,,,`,,, ,À,~,,,`,,,à
,À,~,,,`,,,,Á,~,,,`,,, ,Á,~,,,`,,,@,Á,~,,,`,,,`,Á,~,,,`,,,�,Á,~,,,`,,, ,Á,~
,,,`,,,À,Á,~,,,`,,,à,Á,~,,,`,,,,Â,~,,,`,,, ,Â,~,,,`,,,`,Â,~,,,`,,, ,Â,~,,,`,,,À,Â,~,,,`,,,à,Â,~,,,`,,,,Ã,~,,,`,,, ,Ã,~,

from jxa-cookbook.

stephancasas avatar stephancasas commented on June 7, 2024

I realize this thread is a little old, but you may have better luck deciphering the output if you clear the allocated memory prior to passing it through the target function.

Here's an example implementing the deprecated GetProcessForPID function:

function run(argv) {
  ObjC.bindFunction('malloc', ['void*', ['int']]); // bind malloc
  ObjC.bindFunction('memset', ['void *', ['void *', 'int', 'int']]); // bind memset to clear allocated memory

  const PID = 516; // query for PID 516 (Brave Browser on my system at the time of this demo)
  const bytes = 8; // allocate 8 bytes -- for ProcessSerialNumber struct with two UInt32 fields

  let psn = $.malloc(bytes); // allocate the memory
  $.memset(psn, 0, bytes); // clear the memory

  ObjC.bindFunction('GetProcessForPID', ['int', ['int', 'void *']]); // override bind GetProcessForPID to ignore JXA type safety
  $.GetProcessForPID(PID, psn); // call the function

  // assemble the output for review
  let arr = [];
  for (let i = 0; i < bytes; i++) {
    arr.push(psn[i]);
  }

  return JSON.stringify(arr);
}

In my own test, I got back:

[0,0,0,0,15,240,0,0]

This can be read as two UInt32 values — highLongOfPSN and lowLongOfPSN — belonging to the struct ProcessSerialNumber. Having already run the function from XCode using the native ObjC compiler, I know the decimal values for these fields to be 0 and 61455. If I convert the latter to a its UInt32 (little endian) equivalent, we get the bytes 15, 240, 0, 0.

from jxa-cookbook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.