2

I have the following C# code: (Updating the code/question) The code and the project are marked as unsafe

private struct MEMORY_BASIC_INFORMATION
        {
            internal uint BaseAddress;
            internal uint AllocationBase;
            internal uint AllocationProtect;
            internal uint RegionSize;
            internal uint State;
            internal uint Protect;
            internal uint Type;
        }


public unsafe static bool CheckForSufficientStack(long bytes)
{
    MEMORY_BASIC_INFORMATION stackInfo = new MEMORY_BASIC_INFORMATION();

    // originally was
    // IntPtr currentAddr = new IntPtr((uint)&stackInfo - 4096);

    GCHandle gh = GCHandle.Alloc(stackInfo, GCHandleType.Pinned);
    IntPtr p = gh.AddrOfPinnedObject();

    //now - this line passes compilation        
    IntPtr currentAddr = new IntPtr((uint)p - 4096);

    // build error in the below line
    VirtualQuery(currentAddr, ref stackInfo, sizeof(MEMORY_BASIC_INFORMATION));

   return ((uint)currentAddr.ToInt64() - stackInfo.AllocationBase) >
                   (bytes + STACK_RESERVED_SPACE);
}

and when I compile it I get the error: Cannot take the address of, get the size of, or declare a pointer to a managed type

This is a code I copied and I was actually surprised to know there is '&' in c# but then build failed.

Any idea how to resolve this?

The error is now for the last line when we do a sizeof(MEMORY_BASIC_INFORMATION)

1
  • You cannot take the addess of an object before you have "pinned" it in memory. The reason is that the GC may just move it under your feet otherwise. Commented Oct 10, 2016 at 7:38

3 Answers 3

2

You must "pin" the object in memory first, to make sure that theGC won't move it while you use it:

GCHandle gh = GCHandle.Alloc(stackInfo, GCHandleType.Pinned);
IntPtr p = gh.AddrOfPinnedObject();

Get the size of the struct with:

Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION))
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! this solved the first error (I get two same error for two different lines) MEMORY_BASIC_INFORMATION stackInfo = new MEMORY_BASIC_INFORMATION(); GCHandle gh = GCHandle.Alloc(stackInfo, GCHandleType.Pinned); IntPtr p = gh.AddrOfPinnedObject(); IntPtr currentAddr = new IntPtr((uint)p - 4096); VirtualQuery(currentAddr, ref stackInfo, sizeof(MEMORY_BASIC_INFORMATION)); The second error is for the last line
Could you update the question to make it more clear?
Is it possible to change from class to struct?
Great. Would it be possible to share the declaration with us? Have you tried Marshal.SizeOf(), btw?
Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION)) works for me, and promptly returns "28" (which of course is 4*7).
|
1

In order to make it work you have to indicate you are using unsafe code in C# (since a miscalculation for example could end you up with an invalid address). This is enforced by the unsafe keyword.

unsafe
{
    IntPtr currentAddr = new IntPtr((uint)&stackInfo - 4096);
}

Also make sure your project settings have Unsafe code enabled.

5 Comments

The method itself is marked as unsafe. Isn't that enough?
Nope. The use of & is the reason.
I have circled all the lines with 'unsfae' and still getting the error :( and my project settings allow unsafe code
You can put one unsafe block around an entire section. Did you check your project settings too? Is the error message the exact same as in your question?
Not sure what is wrong then. This fixes the issue at my end and is the only logical explanation of the error you get.
0

The original code from here http://joeduffyblog.com/2006/07/15/checking-for-sufficient-stack-space/ passed compilation when I wrote it inside a new class and not inside an existing class I already have... I have no idea why it passed....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.