Comments (3)
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.
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.
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)
- Possible to intercept http link clicks from an app?? HOT 3
- better integrating Objective-C Bridge documentation + proposed expansions HOT 6
- JXA chat network HOT 1
- Problems with Mojave? HOT 20
- Can't import a custom framework HOT 4
- Using IconRef HOT 2
- How can I create a http request with JXA? HOT 5
- How to copy image or file to clipboard? HOT 15
- Integrating with JXA-Snippets example scripts HOT 1
- Object specifiers and Apple Music HOT 4
- How do you get a random number in JXA? HOT 2
- How to make FSEventStream Work HOT 4
- NSInvalidArgumentException: NSGetSizeAndAlignment(): unsupported type encoding spec 'G' at 'GPoint}"size"{CGSize}}' in '{CGPoint}"size"{CGSize}}' HOT 7
- Arrow functions now work in shebang files HOT 2
- Please state a license for use/reuse of the text
- Mismatched method names in ObjC example
- writeToFileAtomicallyEncodingError has "getFileSystemRepresentation:maxLength"
- Add Missing Event Function `GURLGURL` (JXA-equivalent of `open location`)
- Library impoting
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jxa-cookbook.