This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_000_01C39C9F.B5DABBD0 Content-Type: text/plain; charset="iso-8859-1" # 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 <<Laurence Kell (E-mail).vcf>> ------_=_NextPart_000_01C39C9F.B5DABBD0 Content-Type: application/octet-stream; name="Laurence Kell (E-mail).vcf" Content-Disposition: attachment; filename="Laurence Kell (E-mail).vcf" BEGIN:VCARD VERSION:2.1 N:Kell;Laurence FN:Laurence Kell (E-mail) ORG:CEFAS TEL;WORK;VOICE:+44 (0) 1502 524257 TEL;WORK;FAX:+44 (0) 1502 524511 ADR;WORK:;;Lowestoft Laboratory;Pakefield Road;Lowestoft,;NR33 0HT;UK LABEL;WORK;ENCODING=QUOTED-PRINTABLE:Lowestoft Laboratory=0D=0APakefield Road, Lowestoft, NR33 0HT=0D=0AUK EMAIL;PREF;INTERNET:/o=CEFAS/ou=LOWESTOFT/cn=Recipients/cn=LTK00 REV:20030410T130517Z END:VCARD ------_=_NextPart_000_01C39C9F.B5DABBD0--
As far as I can see, the code you give passes two integer vectors (x and y) to a routine out() that accesses the values in the vector as if they were numeric vectors. This is an error in the code and not in R. If test were written as test = function(x, y) .Call("out", as.numeric(x), as.numeric(y)) the code will work or other problems will be found. D. L.T.Kell@cefas.co.uk wrote:> This message is in MIME format. Since your mail reader does not understand > this format, some or all of this message may not be legible. > > ------_=_NextPart_000_01C39C9F.B5DABBD0 > Content-Type: text/plain; > charset="iso-8859-1" > > # 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 > > > <<Laurence Kell (E-mail).vcf>> > > ------_=_NextPart_000_01C39C9F.B5DABBD0 > Content-Type: application/octet-stream; > name="Laurence Kell (E-mail).vcf" > Content-Disposition: attachment; > filename="Laurence Kell (E-mail).vcf" > > BEGIN:VCARD > VERSION:2.1 > N:Kell;Laurence > FN:Laurence Kell (E-mail) > ORG:CEFAS > TEL;WORK;VOICE:+44 (0) 1502 524257 > TEL;WORK;FAX:+44 (0) 1502 524511 > ADR;WORK:;;Lowestoft Laboratory;Pakefield Road;Lowestoft,;NR33 0HT;UK > LABEL;WORK;ENCODING=QUOTED-PRINTABLE:Lowestoft Laboratory=0D=0APakefield Road, Lowestoft, NR33 0HT=0D=0AUK > EMAIL;PREF;INTERNET:/o=CEFAS/ou=LOWESTOFT/cn=Recipients/cn=LTK00 > REV:20030410T130517Z > END:VCARD > > ------_=_NextPart_000_01C39C9F.B5DABBD0-- > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-devel-- _______________________________________________________________ Duncan Temple Lang duncan@research.bell-labs.com Bell Labs, Lucent Technologies office: (908)582-3217 700 Mountain Avenue, Room 2C-259 fax: (908)582-3340 Murray Hill, NJ 07974-2070 http://cm.bell-labs.com/stat/duncan
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 >>
On Mon, 27 Oct 2003 21:00:57 +0100 (MET), L.T.Kell@cefas.co.uk wrote :>> 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 RYou have a problem with the calling convention.>> extern "C" __declspec(dllexport) SEXP __stdcall out(SEXP x, SEXP y)R wants "cdecl", not "stdcall". See my web page <http://www.stats.uwo.ca/faculty/murdoch/software/compilingDLLs/> for some advice (as well as readme.packages for VC++ advice). Duncan Murdoch