How to call specific EXE function on Linux/Mac - c++

I'm sure if it's possible, but I'd like to call the function which is defined in the exe file on Linux/Mac:
0x421ff0 ##my_func_doing_stuff#Initialize 4
Basically I'd like to add command line support which is not implemented and the UI is kind of drag&drop which is useless.
Note: I don't have access to the source of the file.
I've was playing with wine, objdump, uncc (trying to covert it into C again) and Python using pefile, SWIG and ctypes:
#!/usr/bin/python
from ctypes import *
import pefile, sys
pe = pefile.PE('my_file.exe')
print pe.dump_info()
my_exe = cdll.LoadLibrary('./my_file.exe')
but without success.
The error:
OSError: ./my_file.exe: invalid ELF header
reminded me that I can't call any of Windows functions under Linux without emulation?
So I'm looking for some other solutions. Probably it can be done somehow by emulating or debugging it under wine. But I'm not sure if there is any API for calling the specific functions.
Are there any existing solutions?

You can use winelib to load and link with the binary (since you need wine to provide a usable ABI), but keep in mind that this effectively turns your application into a Wine application.

Related

Use a C++ compiled code within a R Shiny app in shinyapp.io

I have developed a ShinyApp that is built around a C++ program. In short, what the app does is :
provides a nice interface to setup the parameters (in a text file) for the C++ app
runs the C++ compiled code using the system(...) command
displays the output of the C++ code using ggplot2
The C++ compiled code is stored into the www folder. Locally it works fine, but when I load the app to the shinyapp website (I have a free subscription), I got the following error:
sh: 1: ./a.out: Permission denied
with a.out being my compile c++ code. Any idea if
I am doing something wrong?
It is possible call a compiled c++ code within shinyapp.io?
This is a super old question, but since I stumbled on it looking for an answer for my identical problem, I would share what worked for me.
I didn't try the .bat suggestion mentioned in the comments, because that seemed to be tied to Windows OS and Shiny uses Linux.
Instead, I used R's Sys.chmod() function. In your case, if you are calling system("a.out"), before that line, put Sys.chmod("a.out", mode="777"). Note that you may want to look more into what chmod does with regards to permissions. But the code would look like:
// ...
Sys.chmod("a.out", mode="777")
system("a.out")
// ... remaining code

Call dll function works in IronPython, doesn't work in CPython3.4 gives "No method matches given arguments" error

For a project I need to include a DLL in Python. I'm using CPython3.4 and for including the dll I use pythonnet clr module (pythonnet-2.0.0.dev1-cp34-none-win_amd64.whl). In the dll I need a function that gives me a continuous update of a measurement. The dll is written in VB.net, the function that I need is shown below:
Public Sub AdviseStart(ByVal item As Integer, ByVal a As Action(Of Object)) Implements IConversation.AdviseStart
_parameterPoller.RegisterCallback(item, a)
End Sub
This is the code that I have written in python to call this function:
import clr
clr.AddReference('dll name')
from dll import SetupMonitor
monitor = SetupMonitor(None, None, None)
# call to the dll function
# Everytime the measurement is changed the "test" function should be executed
monitor.AdviseStart(8, test)
def test(data):
print("Value: " + str(data))
For the sake of my further project I want to use Python3.4 and not Ironpython.
I tested this code in python3.4 and 2.7 and I got this error:
No method matches given arguments
I am 100% sure that the error is from the AdviseStart function. Because when I use the same code with IronPython it works. In Ironpython this codes gives the output I expect:
Value: -74
The goal of this function is that everytime a new measurement is made the function "test" will be called. Everything for the measurement and calling is in the dll. Is there anyway I can make this function work in any CPython version?
There were some problems with integer arguments in the 64 bit builds. As far as I know that particular problem has been solved, but it would have been after that build you are using was released.
Try getting the source from github (https://github.com/renshawbay/pythonnet) and build that instead. To build and install it you just run "python setup.py install" (or any of the usual setup.py commands).
If it still doesn't work you can build a debug version by setting CONFIG="Debug" in setup.py and rebuilding. You will then be able to attach the visual studio debugger to your python process and step through the python.net code to see what signatures it's finding and why it's not matching with the arguments you're calling it with.

Installing glLoadGen 2.0.2

I need an opengl loader, extension or core. I know I am suppose to use gl code inside after gl context has been called. My problem is actually setting up this opengl development environment.
I tried glew, and failed horribly.
Next one on my list is gl load generator. It generates specific opengl core, so all i have to do is include them to use those core functions.
However, I can not get this to install and feel like giving up and moving onto gl3w soon...
This is the error I am recieving when I try to make a simple file:
C:\MinGW64\glLoadGen_2_0_2>lua LoadGen.lua -style=pointer_c -spec=gl -version=3.
3 -profile=core core_3_3
lua: ./modules/Styles.lua:37: attempt to index local 'lfs' (a boolean value)
stack traceback:
./modules/Styles.lua:37: in main chunk
[C]: in function 'require'
./modules/GetOptions.lua:28: in main chunk
[C]: in function 'require'
LoadGen.lua:17: in main chunk
[C]: ?
C:\MinGW64\glLoadGen_2_0_2>cd C:\MinGW64\glLoadGen_2_0_2
Environment:
Windows 7 64 bit, running cmd as adminstrator, luafilesystem 1.5.0-1
There's a little syntax mistake. However, if you did some researches on this you would have found this Ticket
You have to replace
local lfs = pcall(require, "lfs")
if(lfs and lfs.attributes("modules/UserStyles.lua", "mode") == "file") then
with
local status, lfs = pcall(require, "lfs")
if(status and lfs.attributes("modules/UserStyles.lua", "mode") == "file") then
This fixes the errors.
Cheers
I get the exact same error message on Ubuntu 14.04 with default lua with glLoadGen_2_0_2. The only way I have been able to use glLoadGen is to download the glLoadGen_1_9 distribution and run the exact same lua command. It generates the gl .h and .c files fine. I'm assuming something is badly broken in the glLoadGen_2_0_2 distribution to get identical errors in such very different environments.
Seems it is trying to use mingw compiler suite to do the build. You either need to install mingw, or you need to tell luarocks (or whatever command you're using for the installation) how to find it, or you need to tell that command to use MSVC (or whatever build tool chain you want to use).

Creating R package containing C++ on Windows

My goal is to create a package in R with C++ code: So my questions is how?
I am following the tutorial http://www.stat.columbia.edu/~gelman/stuff_for_blog/AlanRPackageTutorial.pdf on creating an R package containing C++ code. The specific code Im trying to compile and package is exactly as described in the tutorial.
R CMD SHLIB seems to be working creating .dll file.
I can load in R using dyn.load() and test it on simulated data (as described in tutorial)
R CMD INSTALL is where the problem begins. I have done two things encountering two different errors supposedly related:
1) The tutorial says the NAMESPACE file is supposed to contain the code:
useDynLib(XDemo)
export(XDemoAutoC)
When it does R CMD INSTALL fail resulting in error:
Error in inDL(x,as.logical(local), as.logical(now),...): unable to
load shared object 'C:/.../libs/i386/XDemo.dll': Loadlibrary failure:
1% is not a valid Win32-program
2) Removing the above mentioned lines in NAMESPACE file will result in installation of package. I can succesfully load it in R but when I try to use the R function that makes a .C() call to the C++ written function I another error:
library(newpackage)
ls(package:newpackage)
[[1]] "XDemoAutoC"
Warning message:
In ls(package:newpackage) :
‘package:newpackage’ converted to character string
XDemoAutoC(c(1,2,3,4))
Error in .C("DemoAutoCor", OutVec = as.double(vector("numeric", OutLength)), :
C symbol name "DemoAutoCor" not in load table
Im running version R2.15.2 on windows 64-bit and using R64 bit.
I read the following post with a similar problem:
http://r.789695.n4.nabble.com/Include-C-DLL-error-in-C-symbol-name-not-in-load-table-td3464021.html
Except they mention nothing about the NAMESPACE-matter.
Also I read this post:
Problem with loading compiled c code in R x64 using dyn.load
So I am thinking: that based on the fact that I am able to use dyn.load() in Rx64 means that I have succesfully created x64 .dll. Assuming that the NAMESPACE file is supposed to be left as in the tutorial - hopefully fixing the >>not in load table<< error - this would mean I should focus on fixing problem one. This problem seems to be caused by something related to 32-bit. I have used Dependency Walker on the .dll file but I am not sure how to interpret the results
I really don't have any ideas on how to fix this problem so any suggestion on what to do would be welcome?
I think you are doing it wrong. Two quick suggestions:
Read the Writing R Extensions manual written to explain just this: writing R extensions including those with compiled code
Have a look at Rcpp which makes R and C++ extensions, including package building so much easier. Or so we think. Writing a package is as easy as calling Rcpp.package.skeleton(). The documentation in 1) still help.
That said, if R CMD INSTALL fails you may have some mixup in your $PATH. Never ever mix MinGW and Cygwin. Make sure no Cygwin DLLs are found when you build or call R. Path order matters greatly. See the manual for details.

Importing ctype; embedding python in C++ application

I'm trying to embed python within a C++ based programming language (CCL: The compuatational control language, not that any of you have heard of it). Thus, I don't really have a "main" function to make calls from.
I have made a test .cc program with a main, and when I compile it and run it, I am able to import my own python modules and system modules for use.
When I embed my code in my CCL-based program and compile it (with g++), it seems I have most functionality, but I get a RUNTIME error:
ImportError: /usr/lib/python2.6/lib-dynload/_ctypes.so: undefined symbol: PyType_GenericNew
This is the code that is executed at Runtime error.
void FFSim::initCKBot (){
Py_Initialize();
PyRun_SimpleString("execfile('logical.py')");
}
logical.py imports modules, one of which attempts to execute 'from cytpes import *', which throws the runtime error.
Can someone explain this to me and how to go about solving it? It seems like I've linked the objects correctly when compiling the c++ aspect of the code.
Thanks.
The Python runtime is effectively a collection of libraries that your program uses. Those libraries take strings, convert them to Python bytecode and then interpret the bytecode. The error you're getting is that as part of interpreting the program, the Python runtime needs to call a function (PyType_GenericNew), but that function does not exist in the compiled Python runtime on your system. Going off the name of the function, this isn't something you can ignore or workaround. It's a fundamental part of the runtime.
Assuming your PATH is correct, your best solution is to reinstall or rebuild Python. Your installation is missing something important.