Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

drslush

Using windows API functions from C#

This topic is 5544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Whats the best way to use functions in the SDK from C#? I thought it had something to do with importing a dll, but I can''t find the code I saw to do it. Is it easier just to create an object in managed C++ code?

Share this post


Link to post
Share on other sites
Advertisement
You need to use P/Invoke to call functions in the win32 API. Basically you use the DllImportAttribute attribute and then define the function prototype. Say you wanted the MessageBox function, you would do something like this:

[DllImportAttribute("User32.dll", CharSet=CharSet.Auto)]
int MessageBox(IntPtr hWnd, String lpText, String lpCaption, unsigned int type);

If you look up the function you want to use in the platform sdk, it usually has a line at the bottom that tells you what dll to import.

Share this post


Link to post
Share on other sites
Ok, I found the info I wanted on cshrp.net, but it seems like there should be an easier way than typing out the function name every time. Isn''t there some sort of utility akin to VB''s API viewer to generate these statements?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The reason that they are so hard is because you are *suppose* to use C# with winforms not actual windows api(you are only suppose to use the actual windows api sparingly

Share this post


Link to post
Share on other sites
quote:
Original post by drslush
Ok, I found the info I wanted on cshrp.net, but it seems like there should be an easier way than typing out the function name every time. Isn''t there some sort of utility akin to VB''s API viewer to generate these statements?


Don''t you just have to type it once, and then use it like a regular function?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!