The testEdge
function loads a dynamic library (005.dll
) using node-api-dotnet
and creates an instance of the Rosetta.Euler
class from it, then calls its Main
method. The function is exported as a module, making it available for use in other parts of the application.
npm run import -- "test a csharp dylib"
const path = require('path')
const dynamicLib = path.join(__dirname, '../Resources/Projects/rosetta-euler/csharp/005/Debug/net9.0/osx-arm64/005.dll')
//const dynamicLib = path.join(__dirname, '../Resources/Projects/rosetta-euler/csharp/005/005.dll')
/*
function moveMouse(x, y) {
open({
library: 'mouse', // key
path: path.join(__dirname, dynamicLib) // path
})
let result = load({
library: "mouse", // path to the dynamic library file
funcName: 'main', // the name of the function to call
retType: DataType.BigInt, // the return value type
paramsType: [DataType.I32, arrayConstructor({
type: DataType.StringArray,
length: 1
})], // the parameter types
paramsValue: [0, ['']] // the actual parameter values
// freeResultMemory: true, // whether or not need to free the result of return value memory automatically, default is false
})
console.log(result)
close('mouse')
return result
}
*/
//const edge = require('edge-js');
const dotnet = require('node-api-dotnet');
async function testEdge() {
dotnet.load(dynamicLib);
/*
const Main = edge.func({
assemblyFile: dynamicLib,
typeName: 'Rosetta.Euler',
methodName: 'Calculate' // Must be a static method
});
console.log('it works: ', await Main([]));
*/
console.log(dotnet)
const Euler = dotnet.Rosetta.Euler;
const myObject = new Euler()
//console.log(await myObject.Calculate([]))
Euler.Main([])
}
module.exports = testEdge
const dynamicLib = require.resolve(path.join('../Resources/Projects/rosetta-euler/csharp/005/Debug/net9.0/osx-arm64/005.dll'));
const dotnet = require('node-api-dotnet');
/**
* Load the dynamic library and call the Main function.
* @returns {Promise} The result of the Main function.
*/
async function testEdge() {
try {
// Load the dynamic library
await dotnet.load(dynamicLib);
// Get the Rosetta.Euler class
const Euler = dotnet.Rosetta.Euler;
// Create an instance of the class
const myObject = new Euler();
// Call the Main function
const result = await myObject.Main([]);
console.log('Result:', result);
} catch (error) {
// Handle any errors that occur
console.error('Error:', error);
}
}
// Export the testEdge function
module.exports = testEdge;
Code Breakdown
path
: a built-in Node.js module for working with file paths.dynamicLib
: a file path to a dynamic library (005.dll
) located in a specific directory.testEdge
Function005.dll
dynamic library using node-api-dotnet
.Rosetta.Euler
class from the loaded library.Main
method on the Euler
class instance, passing no arguments.edge-js
library to load the dynamic library and call its functions.moveMouse
function is not relevant to the current code.dotnet
variable is an instance of the node-api-dotnet
library, which is used to load and interact with the dynamic library.Euler
class instance is created using the new
keyword, and its Main
method is called as a static method (although it is not marked as static in the code).testEdge
function is exported as a module, making it available for use in other parts of the application.