From L.T.Kell at cefas.co.uk  Mon Oct 27 21:00:57 2003
From: L.T.Kell at cefas.co.uk (L.T.Kell@cefas.co.uk)
Date: Mon Oct 27 21:00:02 2003
Subject: [Rd] (PR#4806)
Message-ID: <200310272000.h9RK0v0P014034@pubhealth.ku.dk>

Duncan Temple Lang correctly pointed out that the function should have been
declared as

 test<-function(x,y) .Call("out",as.numeric(x),as.numeric(y))

However, the problem is that calling any of the functions in my DLL
(compiled using MSVC C++) causes R 1.8.0 to crash; out() ws just a simple
example. The functions had previous worked under R 1.7.x. I have now
recompiled R 1.8.0 under an earlier version of MinGW and my DLL functions
now works. I therefore suspect that the problem is related to MinGW and
Windows.

Regards Laurie

>  -----Original Message-----
> From: 	Laurence Kell FM CEFAS  
> Sent:	27 October 2003 15:34
> To:	'r-bugs@r-project.org'
> Subject:	
> 
> # Your mailer is set to "none" (default on Windows),
> # hence we cannot send the bug report directly from R.
> # Please copy the bug report (after finishing it) to
> # your favorite email program and send it to
> #
> #       r-bugs@r-project.org
> #
> ######################################################
> 
> 
> 
> I am trying to call functions written in Visual C++ from R version 1.8.0
> under Windows. 
> Although these functions ran correctly in version 1.7 in Version 1.8.0
> they cause R to crash after the function has returned to R
> 
> I can repeat the problem with a DLL based on a simple example given by
> Douglas Bates. 
> 
> dyn.load("TestDll.dll")
> test<-function(x,y) .Call("out",x,y)
> 
> x<-1:10
> y<-2:10
> test(x,y)
> 
> The C++ code is below
> 
> #include <windows.h>
> #include <R.h>
> #include <Rinternals.h>
> 
> 
> HWND      hWndMain;   
> HINSTANCE hInst;    
> HGLOBAL   hXloperArray;
> 
> //32 bit Entry point
> 
> #define CLASS_NAME_BUFFER  50
> 
> BOOL WINAPI DllMain(HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved)
>    {					
>    extern HWND      hWndMain;
>    extern HINSTANCE hInst;    
>    
>    hInst = hDLL; 
>     
>    return TRUE;
>    }
> 
> extern "C" int FAR PASCAL WEP (int nArgument)
>    {
>    return 1;
>    } 
>      
> extern "C" __declspec(dllexport) SEXP __stdcall out(SEXP x, SEXP y)
>      {
>        int i, j, nx, ny;
>        double tmp;
>        SEXP ans;
>      
>        nx = length(x); ny = length(y);
>        PROTECT(ans = allocMatrix(REALSXP, nx, ny));
>        for(i = 0; i < nx; i++) {
>          tmp = REAL(x)[i];
>          for(j = 0; j < ny; j++)
>            REAL(ans)[i + nx*j] = tmp * REAL(y)[j];
>        }
>        UNPROTECT(1);
>        return(ans);
>      }
>      
> 
> 
> I created the lib file from the R.dll using pexports and lib
> i.e.
> 
> pexports R.dll > RDLL.def
> lib /machine:i386 /def:RDLL.def /out:Rdll.lib
> 
> Used the following def file
> 
> LIBRARY   TestDLL	
> EXPORTS	
>    out		       =_out@8
> 
> Regards Laurie
> 
> --please do not edit the information below--
> 
> Version:
>  platform = i386-pc-mingw32
>  arch = i386
>  os = mingw32
>  system = i386, mingw32
>  status = 
>  major = 1
>  minor = 8.0
>  year = 2003
>  month = 10
>  day = 08
>  language = R
> 
> Windows 2000 Professional (build 2195) Service Pack 3.0
> 
> Search Path:
>  .GlobalEnv, package:methods, package:ctest, package:mva, package:modreg,
> package:nls, package:ts, Autoloads, package:base
> 
> 
>  << File: Laurence Kell (E-mail).vcf >>


[Rd] (PR#4806)
L.T.Kell@cefas.co.uk