views:

55

answers:

2

Hi guys

I've imported an API function like

[DllImport("gdi32.dll")]
private unsafe static extern bool SetDeviceGammaRamp(Int32 hdc, void* ramp);

while compiling its showing an error like

Unsafe code may only appear if compiling with /unsafe

how to compile with /unsafe . i'm using Microsoft Visual Studio 2008

can any one help me with a better solution.

Thanks in advance.

+8  A: 

right click on project. properties. build. check allow unsafe code

Catalin Florea
its Left click?? right???
Pramodh
no. right click on project name to open it's menu, so you can go to properties.
Catalin Florea
oh. i thought the menu in the toolbar. Either way we can use. right?
Pramodh
in the solution explorer. sorry, forgot to mention.
Catalin Florea
+3  A: 

Just delete the unsafe keyword from the declaration. Windows API functions like this are not unsafe. You can get rid if the awkward void* (IntPtr in managed code) like this:

    private struct RAMP {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
        public UInt16[] Red;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
        public UInt16[] Green;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
        public UInt16[] Blue;
    }

    [DllImport("gdi32.dll")]
    private static extern bool SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);

Also note that the first argument is a handle, an IntPtr, not an Int32. Required to make this code work on 64-bit operating systems.

Hans Passant