[R] C-Code in R (running Windows)

K.A. hanny.hoehne at web.de
Wed Mar 1 15:34:16 CET 2006


I got it!!!
As Andy said, I generated an dll-File (using Dev-C++, it was quite easy) and
imported some R-header. This could be opened in R with dyn.load(dll-file)
und executed with .C()!!


Thanks for your help!!

PS: I've taken the Files of my (Dev C++ -)project behind:


dllmain.c:
#include "dll.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
          #include <R.h>
          #include <Rinternals.h>
          #include <R_ext/Rdynload.h>
          
          void
          R_init_mylib(DllInfo *info)
          {
            /* Register routines, allocate resources. */
          }
          
          void
          R_unload_mylib(DllInfo *info)
          {
            /* Release resources. */
          }

 
/* useC1.c                                    */
/* Calling C with an integer vector using .C  */   

DLLIMPORT void useC(int *i) {
    i[3] = 11;  //This can be used in R!!
}


DLLIMPORT void HelloWorld ()
{
    //This can be used in R!!
    MessageBox (0, "Hello World from DLL!\n", "Hi", MB_ICONINFORMATION);
}




BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                       DWORD reason        /* Reason this function is being
called. */ ,
                       LPVOID reserved     /* Not used. */ )
{
    switch (reason)
    {
      case DLL_PROCESS_ATTACH:
        break;

      case DLL_PROCESS_DETACH:
        break;

      case DLL_THREAD_ATTACH:
        break;

      case DLL_THREAD_DETACH:
        break;
    }

    /* Returns TRUE on success, FALSE on failure */
    return TRUE;
}



dll.h:
#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */


DLLIMPORT void HelloWorld (void);
DLLIMPORT void useC(int *i);

#endif /* _DLL_H_ */

--
View this message in context: http://www.nabble.com/C-Code-in-R-%28running-Windows%29-t1205196.html#a3184042
Sent from the R help forum at Nabble.com.




More information about the R-help mailing list