[R] Passing a pointer to .C() in Win32
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:
but not yet in:
Apologies if I've sent it twice.
On Thu, 15 Apr 2004, James Wettenhall wrote:
Is there any way to pass an integer from R to C and have
it cast as a pointer?
# Win32 Example:
tt <- tktoplevel()
hWndString <- tclvalue(tkwm.frame(tt))
# I'll avoid posting code to this function:
hWnd <- hexStringToDecimalInteger(hWndString)
system32 <- file.path(Sys.getenv("windir"),"system32")
user32 <- file.path(system32,"user32.dll")
# WARNING: THIS NEXT LINE WILL PASS AN INVALID WINDOW HANDLE TO
# USER32.DLL AND PROBABLY CRASH YOUR R SESSION!!!!
# 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
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)
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).
More information about the R-help