Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8267295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:28:11+00:00 2026-06-08T05:28:11+00:00

In C# I have this: [DllImport(user32.dll, EntryPoint = GetDesktopWindow)] public static extern IntPtr GetDesktopWindow();

  • 0

In C# I have this:

[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
    public static extern IntPtr GetDesktopWindow();

I tried to convert to VB.NET thus:

<DllImport("user32.dll", EntryPoint:="GetDesktopWindow")>
Function GetDesktopWindow() As IntPtr
End Function

But I am getting an error…
“Imports System.Runtime.InteropServices.DllImportAttribute cannot be applied to instance method.”

Can some explain what I need to do to fix this, and even better, tell me WHY?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:28:12+00:00Added an answer on June 8, 2026 at 5:28 am

    You forgot to convert the static keyword from the C# declaration to VB.NET. That’s what the error message is telling you. Unless you have a static method, you’re declaring an instance method, and the DllImportAttribute cannot be applied to an instance method.

    The VB.NET equivalent of static is Shared. So your declaration should look like this:

    <DllImport("user32.dll", EntryPoint:="GetDesktopWindow")>
    Shared Function GetDesktopWindow() As IntPtr
    End Function
    

    I feel compelled to point out a couple of other things:

    • It’s unnecessary to specify the EntryPoint when your function declaration has the same name. Not that it hurts anything to do so anyway, but I feel that it keeps down duplication and reduces the chances of error if you omit it.
    • P/Invoke declarations like this should generally go into a static class with a name like NativeMethods (StyleCop enforces this guideline). In VB.NET, static classes are called modules. So it would look like this:

      Module NativeMethods
          <DllImport("user32.dll")>
          Shared Function GetDesktopWindow() As IntPtr
          End Function
      End Module
      
    • In older versions of VB (pre-VB 10, shipped with VS 2010), you needed line continuation characters in order to break up function declarations onto multiple lines. Those ugly warts make it look like this:

      Module NativeMethods
          <DllImport("user32.dll")> _
          Shared Function GetDesktopWindow() As IntPtr
          End Function
      End Module
      

    And finally, be very careful about how you use the desktop window returned by the GetDesktopWindow function! Lots of people abuse it, and most of the time when I see people trying to retrieve a handle to it, that’s a sign that they’re already doing it wrong. (Not saying you are, since I can’t see the rest of your code, just something to be aware of!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was using GetWindowLong like this: [DllImport(user32.dll)] private static extern IntPtr GetWindowLong(IntPtr hWnd, int
I have the following function: [DllImport(user32.dll, CharSet=CharSet.Auto)] static extern int SendMessage(IntPtr hWnd, int wMsg,
I've got this code: [DllImport(user32.dll)] static extern void mouse_event(uint dwFlags, uint dx, uint dy,
I have a class which uses a method in user32.dll: [System.Runtime.InteropServices.DllImport(user32.dll)] private static extern
i have a method registered like this: [DllImport(MyApi.dll, CharSet = CharSet.Ansi)] private static extern
I have a program that simulates mouse click. Code is something like this: [DllImport(user32.dll,
I'm using the following functions in my program: [DllImport(user32.dll, CharSet = CharSet.Auto)] static extern
Here is the code i have: public string selectedProgram; [DllImport(user32.dll, CharSet = CharSet.Auto, ExactSpelling
For example, this is from .NET Framework source file UnsafeNativeMethods.cs : [DllImport(ExternDll.User32, ExactSpelling=true, CharSet=CharSet.Auto)]
I have several DllImport s with respect to user32.dll in my code and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.