I want to use R functions in my visual studio application. So, I created a package using Rcpp and got the dll of it. I wrote a simple console to test the methods in the dll. I could load the dll but not able to call the method.
I have a simple function which returns sum of the two numbers.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int addNumbers(int x, int y) {
return x+y;
}
I have created a package called "HelloRcpp" with this function. I got the dll of the package after building it. I tested the dll in R by using dyn.load()
. It worked fine.
The RcppExports.cpp
looks like below.
// Generated by using Rcpp::compileAttributes() -> do not edit by hand
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
#include <Rcpp.h>
using namespace Rcpp;
// addNumbers
int addNumbers(int x, int y);
RcppExport SEXP _HelloRcpp_addNumbers(SEXP xSEXP, SEXP ySEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< int >::type x(xSEXP);
Rcpp::traits::input_parameter< int >::type y(ySEXP);
rcpp_result_gen = Rcpp::wrap(addNumbers(x, y));
return rcpp_result_gen;
END_RCPP
}
static const R_CallMethodDef CallEntries[] = {
{"_HelloRcpp_addNumbers", (DL_FUNC) &_HelloRcpp_addNumbers, 2},
{NULL, NULL, 0}
};
RcppExport void R_init_HelloRcpp(DllInfo *dll) {
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
}
I wrote below console in visual studio to test the dll.
#include <Windows.h>
#include <iostream>
#include <string>
using namespace std;
typedef int(*FNPTR)(int, int);
int main()
{
HINSTANCE hInst = LoadLibrary(L"C:\\Users\\blrsri02\\Documents\\R\\win-library\\3.3\\HelloRcpp\\libs\\x64\\HelloRcpp.dll");
if(hInst == NULL)
{
cout<<"\n couldn't load the library!!";
exit(0);
}
FNPTR fn = (FNPTR)GetProcAddress(hInst, "_HelloRcpp_addNumbers");
int Sum = fn(1, 2);
return 0;
}
I could load the dll successfully and get the procadress of the method. However, when I call the method, I am getting the unhandled exception error. I think I need do something with the Rcpp interface attributes. any help is appreciated.