0

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.cpplooks 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.

Srinivas D
  • 51
  • 3
  • `_HelloRcpp_RcppExport_validate` function name doesn't look like the `addNumbers` function name that you are trying to call. What makes you think that `_HelloRcpp_RcppExport_validate` has signature of `addNumbers` function and you can call it like this? – user7860670 Sep 27 '17 at 09:32
  • Actually it is '_HelloRcpp_addNumbers'. I pasted the wrong function in the above snippet and tried to edit it but don't know how to edit. I am using the right function in my console and getting the unhandled exception. – Srinivas D Sep 27 '17 at 09:49
  • RCpp does not export your original `addNumbers` function, it generates a wrapper function (suitable for R) and exports it instead. Check out `RcppExports.cpp` file generated by RCpp. – user7860670 Sep 27 '17 at 09:56
  • I have added the `RcppExports.cpp` to my question. As you can see, the wrapper function for `addNumbers` is `_HelloRcpp_addNumbers`. Further, when I ask for `procaddress` of this method I am not getting a null. So, I feel there is no problem with the name of the method in the console. – Srinivas D Sep 27 '17 at 10:18

0 Answers0