[R] Passing a pointer to .C() in Win32

James Wettenhall wettenhall at wehi.edu.au
Fri Apr 16 07:18:21 CEST 2004


I sent this to R-help recently, but I'm not sure if it was
received correctly.  It it is in:
https://www.stat.math.ethz.ch/pipermail/r-help/2004-April.txt.gz
but not yet in:
http://tolstoy.newcastle.edu.au/R/help/04/04/index.html
Apologies if I've sent it twice.

On Thu, 15 Apr 2004, James Wettenhall wrote:
Hi,

Is there any way to pass an integer from R to C and have
it cast as a pointer?

# Win32 Example:

library(tcltk)
tt <- tktoplevel()
hWndString <- tclvalue(tkwm.frame(tt))
# I'll avoid posting code to this function:
source("http://bioinf.wehi.edu.au/folders/james/R/hexStringToDecimalInteger.R")
hWnd <- hexStringToDecimalInteger(hWndString)
system32 <- file.path(Sys.getenv("windir"),"system32")
user32 <- file.path(system32,"user32.dll")
dyn.load(user32)

# WARNING: THIS NEXT LINE WILL PASS AN INVALID WINDOW HANDLE TO
# USER32.DLL AND PROBABLY CRASH YOUR R SESSION!!!!
.C("SetForegroundWindow",hWnd)

# This above won't work, because .C() will pass a pointer to the
# integer hWnd to SetForegroundWindow (in user32.dll) whereas
# I want the integer hWnd to be cast as a pointer.

So for each DLL function I want to call with a pointer argument,
do I have to define my own C function e.g. my_SetForegroundWindow
(using the Windows API) which takes an integer argument instead
and then casts it as a pointer in order to call the real
SetForegroundWindow function?

Of course environments behave like pointers but they are
read-only.  You can't do this (below), right?
mode(hWnd) <- "environment"

WHY WOULD I WANT TO DO ANYTHING LIKE THIS ABOVE?

I know that I can use tkfocus for Tk windows, but I have other
applications in mind.  In Win32, when I run RGui with MDI, the
bringToTop in tcltk's .onLoad brings the console to the top, but
doesn't focus it, i.e. after library(tcltk), I can't type into
the console until I click on it.  Now if I had RConsole->handle
from the GraphApp code for RGui, maybe I could try something
like SetForegroundWindow... For more info, go to
http://msdn.microsoft.com and search for "BringWindowToTop",
search for "SetForegroundWindow", and compare...

Maybe for this particular application, something like
show(RConsole) might be better, (see R-devel thread below)
http://www.mail-archive.com/r-devel@stat.math.ethz.ch/msg01829.html

but I'm still wondering if casting to pointers can be done...

Another application is specifying a Tk window to be "Always On
Top" using user32.dll's SetWindowPos function (again, search
for it on http://msdn.microsoft.com).

Regards,
James




More information about the R-help mailing list