How Can I fix lParam pointer Error in Pinvoke/Win32 ?

MERUN KUMAR MAITY 576 Reputation points
2024-09-18T11:32:18.04+00:00

Hi there, currently I need to dig up some Win32 code which is a part of my project, Actually, it's a part of my WPF application where access Win32 APIs using the traditional Pinvoke method.

But Currently I facing some error at lParam pointer structure.

Here is the piece of code where I face the problem :

 MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
 if (monitor != IntPtr.Zero)
 {
     MONITORINFO monitorInfo = new MONITORINFO();
     GetMonitorInfo(monitor, monitorInfo);


I get red squiggly Line on MINMAXINFO and on MONITORINFO on my Visual Studio 2022.

I hope someone who frequently work with Win32 APIs will able to solve my problem.

Another important point is I already do the necessary DLL imports like

 [DllImport("user32" )]


Hope someone able to figure out my problem. I also attached some images for better visualization and better understanding of the problem.

MONITORINFO

Here is my Full source Code : https://pastebin.com/Y7puGMsZ

I gave the Pastebin link here because posting full source Code may filter by the moderator and bots.

I hope this Microsoft Q & A site content moderation will improve in future, because now a days people facing a lot of problems when they posting any question here, their post will automatically get deleted and comments are filtered and deleted.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,592 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,857 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,693 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. RLWA32 45,326 Reputation points
    2024-09-18T12:54:26.1666667+00:00

    For example, from internal class Win32 for P/Invoke -

        internal const int WM_GETMINMAXINFO = 0X0024;
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct POINT
        {
            public int x;
            public int y;
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct MINMAXINFO
        {
            public POINT ptReserved;
            public POINT ptMaxSize;
            public POINT ptMaxPosition;
            public POINT ptMinTrackSize;
            public POINT ptMaxTrackSize;
        }
    //
        Win32.MINMAXINFO mminfo = (Win32.MINMAXINFO )Marshal.PtrToStructure(m.LParam, typeof(Win32.MINMAXINFO));
    
    

  2. Castorix31 84,546 Reputation points
    2024-09-18T20:07:12.43+00:00

    As there is at beginning :

    using PInvoke;
    

    Then you use the package PInvoke.User32

    then if you add at beginning

    using static PInvoke.User32;
    

    it will add needed definitions as VS suggests

    (I tested by just copy/pasting your code)


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.