nodejs:How to call c++ DLL function through nodejs? - c++

I have a windows c++ DLL. It provides some functions like add(1,2). But I don't have the source code for this DLL, is it possible call functions in this DLL through nodejs, I mean, through web side and http. If it possible, what should I do?

Did you check out the ffi nodejs library? https://github.com/node-ffi/node-ffi
var ffi = require('ffi');
var libm = ffi.Library('libm', {
'ceil': [ 'double', [ 'double' ] ]
});
libm.ceil(1.5); // 2

The
https://github.com/node-ffi/node-ffi
was indeed a good solution but not maintained since 2019.
The new version is:
https://github.com/node-ffi-napi/node-ffi-napi

Related

IDA Pro finding functions that have different addresses in a new version of a binary?

With IDA Pro Decompiler,
i was looking for an way to trace the address of a function when the file changed.
For example, I have a .so (ELF) file and its version 1.0
there a function called
[ Writer_Starting ]
and This Function Address is
[ 0x3DA224 ]
well after a while , and after updating the .so (elf) file by his owner to 1.1
the function address changed to
[ 0x3DA228 ]
well , is there any way to automatically find all the changed address for functions
by comparing the old version of the same file ?
or a way to get specific address like example
i put the old address
[ 0x3DA224 ]
then i found the new one
[ 0x3DA228 ] ?
You can create signatures of the functions in v1.0 with a tool like IDB2PAT, then match those signatures in v1.1 with FLIRT functionality of IDA. But the function body must remain completely unchanged for it to work.

Can I use all the functionally I see exposed by the .TLH file?

Background:
I have an existing code that uses functionality provided by Microsoft, to post XML data over HTTP. Specifically, IServerXMLHTTPRequest (included in MSXML3 and up) from msxml4.dll (COM). I am moving to msxml6.dll as msxml4.dll is not supported anymore (superseded by MSXML6). More information about MSXML versions.
Code:
#import "msxml6.dll"
using namespace MSXML2;
…
IServerXMLHTTPRequestPtr spIXMLHTTPRequest = NULL;
hr = spIXMLHTTPRequest.CreateInstance(__uuidof(ServerXMLHTTP40));
Problem:
When building my app with msxml4.dll as well as msxml6.dll the following is included in the msxml4.tlh and msxml6.tlh respectively:
struct __declspec(uuid("88d969c6-f192-11d4-a65f-0040963251e5"))
ServerXMLHTTP40;
// [ default ] interface IServerXMLHTTPRequest2
As I understand, looking at msxml6.tlh, I can use ServerXMLHTTP40 (and not change the code to ServerXMLHTTP60) with msxml6.dll (same for DOMDocument40, FreeThreadedDOMDocument40, XMLSchemaCache40 etc.).
Now, searching the registry in a fresh Windows 7 Ultimate installation, I cannot find the uuid above. As a result, this code fails on this machine:
hr = spIXMLHTTPRequest.CreateInstance(__uuidof(ServerXMLHTTP40));
Questions:
If msxml6 is exposing ServerXMLHTTP40, why is it that I cannot find it in the registry? Can I use ServerXMLHTTP40 when msxml6 is installed (msxml4 is not installed)?
Need additional information? Just let me know. Thank you!
.TLH file (as a product of import from .TLB, which is in turn a compiled version of .IDL file) is a description of interfaces, structures, methods etc. which ones uses to talk through COM to another object. There is no guarantee or promise that the other party implementing these interfaces is installed or otherwise available, or even exists at all.
Yes you have the signatures defined for you convenience. You might need to install runtime that implements the functionality. MSXML 4 might need a separate install regardless of where you obtained the development details from.

Use "sp" in Android NDK

I'm trying to intercept some native library-calls via LD_PRELOAD.
This is working fine for simple libraries written in C, but now I try to go further and override some more complex class-methods from the AOSP written in C++.
Here's my example:
#include <rs/cpp/util/RefBase.h>
namespace android {
sp<MediaCodec> MediaCodec::CreateByType(const sp<ALooper> &looper, const char *mime, bool encoder) {
// TODO this will be implemented by me
return NULL;
}
}
In my Application.mk, I got the following piece of code:
APP_STL := gnustl_static
and inside the Android.mk this one:
LOCAL_STATIC_LIBRARIES += libstlport_static
Sadly, the error I get is the following:
jni/libhook/ld_preload.cpp:88:1: error: 'sp' does not name a type
Anyone an idea how to use sp<..> here? I assume it's not Android-specific but a standard C++-thing - I'm totally new at C++, just started "today" :)
I know this may be bad practice, so I'm welcome for any other idea.
sp<> is Android-specific. sp<> is Strong Pointer, wp<> is Weak Pointer; they came into being as part of the Binder IPC implementation.
The place to start looking for the implementation is the framework RefBase.h, which is a bit twisty for a C++ newcomer. None of what you're fiddling with is part of the public API defined by the NDK, which means it's subject to change between releases, so be aware that what you're trying to do may not work across devices or software updates.

Firefox Extension/Addon Binary Component Backward Compatibility

I have been reading up and looking at ways to compile binary components for Firefox extensions. Since Firefox 5 is being released (and 6 & 7 coming up soon) I was wondering if binary components are worth making anymore or just use a standalone executable to run the functionality I want.
I got a sample binary component to compile for Firefox 5 but when I tested it on Firefox 3.6, I get this error:
[Exception... "Could not convert Native argument arg 0 [nsISupports.QueryInterface]" nsresult: "0x8057000a (NS_ERROR_XPC_BAD_CONVERT_NATIVE)"
Running this code
var obj = Components.classes['#example.com/MyComponent;1'].QueryInterface(Components.interfaces.IMyComponent);
And errors at the QueryInterface. Apparently building for Firefox 4 (XULrunner-sdk 2.0 instead of 5.0 will work).
Here is the module code:
#include "mozilla/ModuleUtils.h"
#include "MyComponent.h"
NS_GENERIC_FACTORY_CONSTRUCTOR(MyComponent)
NS_DEFINE_NAMED_CID(MY_COMPONENT_CID);
static const mozilla::Module::CIDEntry kMyComponentCIDs[] = {
{ &kMY_COMPONENT_CID, false, NULL, MyComponentConstructor },
{ NULL }
};
static const mozilla::Module::ContractIDEntry kMyComponentContracts[] = {
{ MY_COMPONENT_CONTRACTID, &kMY_COMPONENT_CID },
{ NULL }
};
static const mozilla::Module kMyComponentModule = {
mozilla::Module::kVersion,
kMyComponentCIDs,
kMyComponentContracts,
NULL
};
NSMODULE_DEFN(NS_MyComponent_Module) = &kMyComponentModule;
NS_IMPL_MOZILLA192_NSGETMODULE(&kMyComponentModule)
I also heard that FF3.6 doesn't need to have the xpt or the dll inside the manifest file.
So basically my question is, for backward compatibility would it be better to make an executable or continue to make binary components? (Since it looks like compiling for FF5, FF3.6 broke.)
Your error message should be due to the XPT file not being recognized correctly (Components.interfaces.IMyComponent is undefined). Maybe that's because it is in the wrong directory - in Firefox 3.6 you don't declare it in the chrome.manifest file, instead it has to be located in the compoments/ directory along with your dll file.
The backwards compatibility story of XPCOM components got a lot worse starting with Firefox 4, see https://developer.mozilla.org/En/Developer_Guide/Interface_Compatibility#Binary_Interfaces. Theoretically, if you want to support multiple Firefox versions you need to put multiple versions of your XPCOM component into your XPI package, that's lots of effort for releases that come out every six weeks. If the point is really calling a few functions from a native library then you should seriously consider switching to js-ctypes. You can also ship a native library (plain, not XPCOM) with your extension and use js-ctypes to call it. Firefox supports js-ctypes starting with version 4 (Gecko 2.0), for Firefox 3.6 you would still need a different solution.

C++\IronPython integration example code?

I'm looking for a simple example code for C++\IronPython integration, i.e. embedding python code inside a C++, or better yet, Visual C++ program.
The example code should include: how to share objects between the languages, how to call functions\methods back and forth etc...
Also, an explicit setup procedure would help too. (How to include the Python runtime dll in Visual Studio etc...)
I've found a nice example for C#\IronPython here, but couldn't find C++\IronPython example code.
UPDATE - I've written a more generic example (plus a link to a zip file containing the entire VS2008 project) as entry on my blog here.
Sorry, I am so late to the game, but here is how I have integrated IronPython into a C++/cli app in Visual Studio 2008 - .net 3.5. (actually mixed mode app with C/C++)
I write add-ons for a map making applicaiton written in Assembly. The API is exposed so that C/C++ add-ons can be written. I mix C/C++ with C++/cli. Some of the elements from this example are from the API (such as XPCALL and CmdEnd() - please just ignore them)
///////////////////////////////////////////////////////////////////////
void XPCALL PythonCmd2(int Result, int Result1, int Result2)
{
if(Result==X_OK)
{
try
{
String^ filename = gcnew String(txtFileName);
String^ path = Assembly::GetExecutingAssembly()->Location;
ScriptEngine^ engine = Python::CreateEngine();
ScriptScope^ scope = engine->CreateScope();
ScriptSource^ source = engine->CreateScriptSourceFromFile(String::Concat(Path::GetDirectoryName(path), "\\scripts\\", filename + ".py"));
scope->SetVariable("DrawingList", DynamicHelpers::GetPythonTypeFromType(AddIn::DrawingList::typeid));
scope->SetVariable("DrawingElement", DynamicHelpers::GetPythonTypeFromType(AddIn::DrawingElement::typeid));
scope->SetVariable("DrawingPath", DynamicHelpers::GetPythonTypeFromType(AddIn::DrawingPath::typeid));
scope->SetVariable("Node", DynamicHelpers::GetPythonTypeFromType(AddIn::Node::typeid));
source->Execute(scope);
}
catch(Exception ^e)
{
Console::WriteLine(e->ToString());
CmdEnd();
}
}
else
{
CmdEnd();
}
}
///////////////////////////////////////////////////////////////////////////////
As you can see, I expose to IronPython some objects (DrawingList, DrawingElement, DrawingPath & Node). These objects are C++/cli objects that I created to expose "things" to IronPython.
When the C++/cli source->Execute(scope) line is called, the only python line
to run is the DrawingList.RequestData.
RequestData takes a delegate and a data type.
When the C++/cli code is done, it calls the delegate pointing to the
function "diamond"
In the function diamond it retrieves the requested data with the call to
DrawingList.RequestedValue() The call to DrawingList.AddElement(dp) adds the
new element to the Applications visual Database.
And lastly the call to DrawingList.EndCommand() tells the FastCAD engine to
clean up and end the running of the plugin.
import clr
def diamond(Result1, Result2, Result3):
if(Result1 == 0):
dp = DrawingPath()
dp.drawingStuff.EntityColor = 2
dp.drawingStuff.SecondEntityColor = 2
n = DrawingList.RequestedValue()
dp.Nodes.Add(Node(n.X-50,n.Y+25))
dp.Nodes.Add(Node(n.X-25,n.Y+50))
dp.Nodes.Add(Node(n.X+25,n.Y+50))
dp.Nodes.Add(Node(n.X+50,n.Y+25))
dp.Nodes.Add(Node(n.X,n.Y-40))
DrawingList.AddElement(dp)
DrawingList.EndCommand()
DrawingList.RequestData(diamond, DrawingList.RequestType.PointType)
I hope this is what you were looking for.
If you don't need .NET functionality, you could rely on embedding Python instead of IronPython. See Python's documentation on Embedding Python in Another Application for more info and an example. If you don't mind being dependent on BOOST, you could try out its Python integration library.