Click here to Skip to main content
14,599,444 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi. Does anybody know how to use NativeMethods in c sharp? Does it have special namespace?
Rate this:
Please Sign up or sign in to vote.

Solution 1

You probably talking about WinApi methods in meaning of word NativeMethods.
If it's true, you must look forward to use P/Invoke in .NET enviroment.

For example calling of method GetDC from User32.dll could be declared in next format:

private static extern IntPtr GetDC(IntPtr hWnd);

Take a closer look to P\Invoke
Good luck.
Zoltán Zörgő 1-Jun-12 17:40pm
Good mind-reading :) My vote +5.
By the way, NativeMethods is a concept not a language entity. (
Oleksandr Kulchytskyi 1-Jun-12 17:48pm
Yep ) it's an interesting article. Thanks for share link)
aliprogrammer 1-Jun-12 17:46pm
These codes will set the windows calculator into a windows form application. But the question is how to use the NativeMethods.SetParent in the third line. Does it have special namespace?

System.Diagnostics.Process p = System.Diagnostics.Process.Start("calc.exe");
NativeMethods.SetParent(p.MainWindowHandle, this.Handle);

Please help me to use NativeMethods in the third line.
Oleksandr Kulchytskyi 1-Jun-12 17:51pm
=) Now issue became more clearly)
NativeMethods is a third-party class , which is not related to .NET environment.
aliprogrammer 1-Jun-12 17:52pm
What should I do?
Oleksandr Kulchytskyi 1-Jun-12 17:54pm
=)) You should implement some class whith this method)

public static class NativeMethods
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
Oleksandr Kulchytskyi 1-Jun-12 17:59pm
Is it any unclear moments ?
Rate this:
Please Sign up or sign in to vote.

Solution 2

In addition to Oleksandr's answer, wrapping P\Invoke methods in a class called "NativeMethods" is a convention, so if you're looking for a specific class with that name I suggest looking into any external libraries used with code where you may have seen it referenced (making a guess from how you typed it that this may be the case).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100