Jacksonville Developers User Group

Learn new things...write better code.
Welcome to Jacksonville Developers User Group Sign in | Join | Help
in
Home Blogs Forums

WIN32 API Classes

Last post 07-31-2007, 10:45 PM by Entrix. 0 replies.
Sort Posts: Previous Next
  •  07-31-2007, 10:45 PM 1831

    • Entrix is not online. Last active: 07-16-2008, 6:36 PM Entrix
    • Top 150 Contributor
    • Joined on 08-01-2007
    • Jacksonville, Florida
    • Posts 1
    • Points 0

    WIN32 API Classes

    WiNDOWS API 32 (What can it do for me and my business?)

     

    Great document and a little fun poked at Microsoft.

     

    • Windows Application Programming Interface

    Win32 API can create simple applications to control other programs/applications, such as in my example Notepad.exe.

     

    We are going to use C# Express it’s a free download, and I will walk you thru some of the API processes in C# Express. 

    First we must create a new windows form, I can’t upload pictures or I would walk you thru this process.  But most of you better know how to create a Windows Form!

     

    Add in the following using statement.

     

    using System.Runtime.InteropServices;

     

    n      What does Microsoft Classify this as?

     

    System.Runtime.InteropServices Namespace

    The System.Runtime.InteropServices namespace provides a wide variety of members that support COM interop and platform invoke services. If you are unfamiliar with these services, see Interoperating with Unmanaged Code.

    Members of this namespace provide several categories of functionality, as shown in the following table. Attributes control marshaling behavior, such as how to arrange structures or how to represent strings. The most important attributes are DllImportAttribute, which you use to define platform invoke methods for accessing unmanaged APIs, and MarshalAsAttribute, which you use to specify how data is marshaled between managed and unmanaged memory.

     

    OK! All this mess means, that you can basically now importdll’s in to your code.  It’s unmanaged but its still very good code to know.

     

    Question of the Day – Why would I ever need to use unmanaged code in my .NET Application?  Doesn’t .NET suffice for this? No, unfortunately not.

     

    .NET has a lot of code that is managed but not all the WIN API’s are handled with the .NET interface. 

    I’m not really sure but I’m sure Bill Gates and his staff members are working on it. 
    They are also working on taking over the world but no one ask about that either. <JK>

     

     

     

     

      Let’s go over some API changes I’ve experienced with C# Development, and the new .NET Interface.

    The old way in Visual Basic version 6:


    Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, Byval lParam As Integer) As Integer

     

    The new C# .NET Framework Development:

     

    [[DllImport("WinCoreDll.dll")]
    public extern int SendMessage( IntPtr hwnd, uint msg, uint wParam, ref TVHITTESTINFO lParam);

     

    Let’s go over some of the things that have changed.

     

    The DllImport method was never used in the Original Code.  We are using WinCoreDll.dll which is not really a valid Windows DLL file.  What should this be?  Go down for your answer.  (Answer Section: I)

     

    We are using different calls such as public extern which is the same method as: Public Declare Function.

     

    Let’s hear what Microsoft has to say about public extern.

     

    The Requested Page Has Moved

    We apologize for the inconvenience, but the web page you have requested (http://msdn.microsoft.com/library/en-us/dnnetcomp/html/publicextern.asp) has moved.

    Well, we love there explanations, but let me give you my explanation in Microsoft Terms.

     

    Public Extern Statement

    The public extern statement provides a wide variety of members that support dll statements that are inputs in multiple platforms. If you are unfamiliar with these functions, see http://msdn.microsoft.com/library/en-us/dnnetcom/html/publicextern.asp (Page still cannot be found.) 

     

     

    The old way in Visual Basic version 6:


    Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, Byval lParam As Integer) As Integer

     

    The new C# .NET Framework Development:

     

    [[DllImport("WinCoreDll.dll")]
    public extern int SendMessage( IntPtr hwnd, uint msg, uint wParam, ref TVHITTESTINFO lParam);

     

     

    We are calling the entire statement an integer statement at the beginning now.  We have also changed some of the hwnd parms to a pointer statement, and using unassigned integers as message, and unassigned integers as wParam, and referenced a custom statement as lParam. 

     

    What can we changed to make it like the original code?

     

    Let’s try to figure it out.

    Answer Section: I

    [[DllImport("User32.dll")]
    public extern int SendMessage( int hwnd, int msg, int wParam, int lParam);

     

    That’s great what can this code be used for? 

     

    Well, we will find out in the next chapter of Programming.

     

     

     

     

     



     

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems