IsWow64() and C#

Announcements and discussion for PInvoke

Moderators: Luke Jefferson, Robert

IsWow64() and C#

Postby chemelli » Tue Apr 15, 2008 2:35 pm

Hi all,

I just noticed a small error in the sample of IsWow64() and I have correct it.

The main issue in anycase is that I cannot compile my sample program because of those messages from the compiler (VS2005):

Error 1:
'Microsoft.Win32.SafeHandles.SafeProcessHandle' is inaccessible due to its protection level

Error 2:
Inconsistent accessibility: parameter type 'Microsoft.Win32.SafeHandles.SafeProcessHandle' is less accessible than method 'WindowsApplication1.Form1.IsWow64Process(Microsoft.Win32.SafeHandles.SafeProcessHandle, out bool)'


Any help will be much appreciated.

Simone
chemelli
 
Posts: 2
Joined: Tue Apr 15, 2008 2:29 pm

Postby Robert » Tue Apr 15, 2008 3:36 pm

Hi,

I've just had a quick look at this (purely a look - I haven't tested any of this in code), and it looks like the problem is that SafeProcessHandle is an internal type, so you can't access it in your code. The second error you're seeing is because you're declaring a method as being public, but with a parameter that is internal (less accessible) than the method.

I suspect that you can probably just change it to taking an IntPtr, which is what process.Handle returns:

Code: Select all
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWow64Process(
     [In] IntPtr hProcess,       
     [Out] out bool lpSystemInfo
     );


Then something like:

Code: Select all
Process p = Process.GetCurrentProcess();
IntPtr handle = p.Handle;
bool isWow64;
bool success = IsWow64Process (handle, out isWow64);
if (!success)
    throw new Win32Exception();
else
    return isWow64;


Hope that helps, or at least gives you a pointer in the right direction.

Regards,
Robert
Robert Chipperfield
Red Gate
Robert
 
Posts: 410
Joined: Mon Oct 30, 2006 11:15 am
Location: Cambridge, UK

Postby Robert » Tue Apr 15, 2008 3:41 pm

Sorry, another thought I've just had. If you're trying to just check whether the current process is running Wow64, you can do so by checking the value of some environment variables instead: http://msdn2.microsoft.com/en-us/library/aa384274.aspx.

Hope that helps,
Robert
Robert Chipperfield
Red Gate
Robert
 
Posts: 410
Joined: Mon Oct 30, 2006 11:15 am
Location: Cambridge, UK

Postby chemelli » Tue Apr 15, 2008 3:58 pm

Thx Robert,

you code fixed my issue ;)

Simone
chemelli
 
Posts: 2
Joined: Tue Apr 15, 2008 2:29 pm

Postby Robert » Tue Apr 15, 2008 3:58 pm

Good stuff, glad to help! :-)
Robert Chipperfield
Red Gate
Robert
 
Posts: 410
Joined: Mon Oct 30, 2006 11:15 am
Location: Cambridge, UK


Return to PInvoke.Net

Who is online

Users browsing this forum: No registered users and 1 guest