Click here to Skip to main content
15,883,910 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
My goal is to import any .dll file to use any Windows API.

There is Windows API Index where i choose any .dll to wrap to use.

http://msdn.microsoft.com/en-us/library/ff818516(v=vs.85).aspx[^]

I chose the next .dll, how can i wrap it to use? I can't because there is no information for such a goal, in other words Windows API reference is not made for such a goal.

http://msdn.microsoft.com/en-us/library/ms535564(v=vs.85).aspx[^]

There is .NET Framework Class Library, it shows everything i need to know to use them in my C# program. I need the same thing about WinAPI.
http://msdn.microsoft.com/en-us/library/gg145045(v=vs.100).aspx[^]

The next one is what i need for, but it an exception for certain classes.
http://msdn.microsoft.com/en-us/library/ms534039%28VS.85%29.aspx[^]

I have the working code for Outline, but i got it from the last link where are just 600 functions, but i wanna do the same thing with any .dll i choose from Windows API Index.

I think Windows API Index reference is specialized for C++, not for C#.
Posted
Updated 17-Dec-14 2:49am
v2
Comments
MasterCodeon 17-Dec-14 15:59pm    
i have added a solution check it out

ok so the first thing you need to do in order to use a windows api function(this is for the DLL "User32.dll") is to add a reference to the System.Runtime.InteropServices
by doing this:
C#
using System.Runtime.InteropServices;


then you import the DLL like this(note you do this in the space right before the constructor for a winform app):
C#
[DllImport("user32.dll")]


here is an example of how to import the LockWorkStation method from the newly imported "User32.dll" (Note: you should add the proceeding line of code right under the DLL import statement):
C#
public static extern void LockWorkStation();

now this method doesn't take any arguments so you call it like this:
C#
LockWorkStation();

this will lock your computer so i would put this in a button event method or something

this is a very simple use of the windows api and a very easy to do operation.
C# can access the windows api its just a little harder than C++.
i hope this gets you started and helps,
MasterCodeon
 
Share this answer
 
This is exactly the same question that you have already posted twice, and the answer is still the same. You use P/Invoke to access Windows API functions from .NET applications; there is no other documentation. Choose the function you wish to use and look it up on the P/Invoke website. If there is no reference to it then you will need to work out the calling sequence for yourself.
 
Share this answer
 
Solved!

Question: How to use .dll files?
1. Go to WinAPI Index.
http://msdn.microsoft.com/en-us/library/ff818516(v=vs.85).aspx[^]
2. Choose any .dll file.
http://msdn.microsoft.com/en-us/library/ms646294(v=vs.85).aspx[^]
3. Use Platform Invoke (C#, Windows Forms, .Net Framework 4.0, Visual Studio 2010).
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Reflection;

namespace _123
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Method();
        }

        [DllImportAttribute(@"User32.dll")]
        static extern void GetFocus();

        private void Method()
        {
            GetFocus();
        }
    }
}


Question: How to use GDI+ Flat API in the same way?
Answer: It is impossible to use any of GDI+ Flat APIs in the same way.
1. For example, choose a simple GDI+ API:
http://msdn.microsoft.com/en-us/library/ms535564(v=vs.85).aspx[^]
2. You can not use it!
Question: Why?
Answer: Because of:
Quote:
Microsoft Product Support Services will not provide support for code that calls the flat API directly

http://msdn.microsoft.com/en-us/library/ms534039%28VS.85%29.aspx[^]

Question: Is it possible to use any API of Windows API from Windows API Index directly?
Answer: It is impossible to use all of them directly, you only can use .dll files directly. As a result, you should use C++ to make .dll files for those APIs to use them in C# by using Platform Invoke.
 
Share this answer
 
v5
Comments
MasterCodeon 18-Dec-14 9:36am    
by the way i think you don't need the @ symbol in the @"User32.dll" line, i think you just need "User32.dll"(this should work just fine)
Ziya1995 18-Dec-14 9:40am    
Thank you, i have been talking with another user:
http://www.codeproject.com/Questions/853787/Using-native-dlls-in-Csharp
That was hard talk, but now there is no problem, i hope you got it and happy with my solution, thanks.
Richard MacCutchan 18-Dec-14 10:24am    
That's what I told you to do two days ago.
Ziya1995 18-Dec-14 11:43am    
You said how to use .dll files, but there are some .dll files that you can not use if to rely on the information on the page of WinAPI Index, and i asked how to use such a .dll file that you can not use in the same way as you use others.
Read: "Question: How to use GDI+ Flat API in the same way?" in my Solution.
I really appreciate you, you made pretty good diligence and benefited me, but you should be happy, because i understood everything.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900