2

I try to access user32.dll functions in R Session. I used the code:

dyn.load("c://windows//system32//user32.dll")
.External("MessageBeep", 0L)

But R session crashes. According to documentation .External is intended to use during R package creation. However there was no prohibition to use it as shown above.

I am using Windows 8 and RStudio (1.1.453) / R (3.5.0). Could you advise a proper way to call external Windows functions from R session?

Artem
  • 3,304
  • 3
  • 18
  • 41
  • 2
    Where do you specify the calling convention as stdcall? My guess is you can't and would need to create a wrapper dll. – David Heffernan Aug 26 '18 at 12:07
  • As I understand calling conventions should be specified during compilation. And I should look either 1) to compile some package or 2) to use some external process to access to desired dll (somethin like VBA)? – Artem Aug 26 '18 at 14:10
  • 1
    The calling convention of that function is stdcall. I'm not sure if R can call stdcall functions. I think it always uses cdecl. VBA isn't what you need. Which functions are you actually trying to call? – David Heffernan Aug 26 '18 at 15:24
  • Thanks for the hint, I found SO post on [cdecl & stdcall](https://stackoverflow.com/questions/3404372/stdcall-and-cdecl) as well as more thoroughly try to read the R package creation documentation. I am trying to call any function from any dll function. For academic purposes. – Artem Aug 26 '18 at 18:44

1 Answers1

3

You are accessing the Win32 C API, thus you can load the user32.dll, and then use the Foreign {base} .C() call too access the Window32 MessageBeep function.

##Example Code

dyn.load("c://windows//system32//user32.dll")
.C("MessageBeep")

##Runtime Output

> dyn.load("c://windows//system32//user32.dll")
> .C("MessageBeep")
list()
> 

You should hear the Windows Message "Beep" - Alas, I have yet to figure out how to include sounds in solutions (chuckle).

Note: I'd also recommend you take a look at the Rcpp package, I am a big fan.

halfer
  • 19,824
  • 17
  • 99
  • 186
Technophobe01
  • 8,212
  • 3
  • 32
  • 59