views:

54

answers:

2

Looking at Microsoft's page on Wow64DisableWow64FsRedirection, I see some C code. What if you want to call this function and it's revert from VB.net?

So far I have done this:

 <Runtime.InteropServices.DllImport("KERNEL32.DLL",  EntryPoint:="Wow64DisableWow64FsRedirection")> _
Public Shared Function DisableWow64Redirection() As Boolean
End Function

And I do likewise for the Revert brother function.

I call it like so:

DisableWow64Redirection()

This seems to work, as the shell command I call after actually finds its exe in system32, but I am not sure about the Revert, does it need a parameter? This Revert page seems to want me to take the output from disable and plug it into the revert call. Does that sound right? How do I change my DLLimport to take in a boolean and actually use it in the Kernal32.DLL function?

Thanks!

+1  A: 

You could change your function definition to

<DllImport("kernel32.dll", EntryPoint := "Wow64DisableWow64FsRedirection")> _
Public Shared Function DisableWow64Redirection(ByRef output As IntPtr) As Boolean

and define Revert() like so:

<DllImport("kernel32.dll", EntryPoint := "Wow64RevertWow64FsRedirection")> _
Public Shared Function RevertWow64Redirection(ByRef handle As IntPtr) As Boolean

You'd invoke these like so:

Dim handle As IntPtr
DisableWow64Redirection(handle)
RevertWow64Redirection(handle)

I'm not a VB.NET guy, so maybe some syntax is incorrect - the important thing is that you provide a reference parameter of IntPtr type, which maps to the PVOID native type. You'll want to hang on to it, and pass the same value to Revert().

Ben
+1  A: 

The declaration is wrong, it takes a (ByRef oldValue As IntPtr) argument. Which you need to pass to the revert function.

However, you cannot safely use this in a .NET program. It also affects the CLR, it won't be able to find .NET framework assemblies anymore. You cannot predict when they get loaded. There are surely better ways to accomplish what you need, start a new question about that.

Hans Passant
Well, this is part of the installation. Are you saying that even though I ask it to revert, it may not? The rest of the installation might screwed up? I imagine the gap between the installation and running the program would be enough time for it to revert? I really just want to run ServerManagerCmd in a 64bit Windows Server 2008, but it doesn't find it unless I move the exe to sysWow64 from system32.
RichardJohnn
No, I'm saying that you custom install code will bomb. Quite likely in this scenario because you'll execute code after canceling redirection that will get JIT compiled. Which causes assemblies to get loaded. I'm not saying it will, test the bejeesus out of it. There are better ways to handle 64-bit vs 32-bit installs, I recommend you ask a question about it. Don't forget to carefully document what you're trying to do.
Hans Passant