Is there anyway to call a memory address (like (void*) f = 0xFFFF; ) WITHOUT IntPtr or any native Win32 functions. I need to do this for an executable loader (It is for an OS that uses a open source project called COSMOS)
Asked
Active
Viewed 1,758 times
1
-
Why without `IntPtr`? Then how do you want to express your pointer value? You can use pointer type variables in unsafe code. – Mohammad Dehghan Aug 25 '12 at 07:56
-
I'm not an expert on that matter, but it might be possible within an `unsafe` block. (http://msdn.microsoft.com/en-us/library/y31yhkeb%28v=vs.100%29.aspx and http://stackoverflow.com/questions/3069448/how-to-declare-a-void-pointer-in-c-sharp) – Jensen Aug 25 '12 at 07:57
-
I can not use IntPtr because I am using a native C# compiler that does not have implementations of most of the .NET framework including IntPtrs – user1454902 Aug 25 '12 at 08:14
-
@user1454902 C# has a pointer type called `IntPtr`. It is the type used to represent pointers/memory addresses, and it is exactly what you need. End of story. If your broken compiler does not implement support for that, *then ask the correct question*. Don't ask what C# supports, because that's clearly irrelevant. Ask what your broken compiler supports. C# does not have a pointer type which is "pointers for compilers which failed to implement pointers" – jalf Aug 25 '12 at 08:59
1 Answers
1
There is no way to do it in a proper C# high level way without using Marshal.GetDelegateForFunctionPointer
:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate void CallMeDelegate(int i);
CallMeDelegate del = (CallMeDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(0xffff), typeof(CallMeDelegate));
Note that IntPtr isn't native Windows just because of its name: It is the typical pointer value for the current compilation target (32 or 64 bit).
You don't get around using that API I think, because you have to specify a calling convention for being able to use it in normal C# delegate syntax. Be happy with the fact, that Marshal.GetDelegateForFunctionPointer does all the dirty work for you, platform independently!

Sebastian Graf
- 3,602
- 3
- 27
- 38
-
Regarding the last comment the OP gave on the question regarding his compiler: Ignore my answer. But wouldn't it be easier to just implement IntPtr in the compiler? Otherwise you have to resort to ASM/Register manipulation i guess... – Sebastian Graf Aug 25 '12 at 08:24