Thava's Software Blog

This blog is most useful for Software Engineers, Database Administrators, QA and etc.

Jun 23, 2008

Calling Win32 Api from C# - csharp

Though .Net classes are vast and versatile at time you will have to resort to Win32 API calls for accomplishing certain tasks.

The following code shows a simple example of invoking a Win32 api.

The best way to use Win32 apis is to encapsulate it in a class module.
The namespace for COM interop is System.Runtime.InteropServices.
The method name should have DllImport attribute specifying the name of the dll. Here the dll is user32. Other dlls are Gdi and Kernel.

using System;
using System.Collection.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

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

private void cmdtam_Klik(object sender, EventArgs e)
{
string cstr;
cstr = "data source=jery;initial catalog=latihan;uid=sa;pwd=sri";
SqlConnection con1 = new SqlConnection(cstr);
con1.Open();
SqlCommand com1 = new SqlCommand();
com1.Connection = con1;
com1.CommandType = CommandType.Text;
com1.CommandText = "select * from customer"
DataSet ds1 = new DataSet();
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
adp1.Fill(ds1,"customer");
grd1.DataMember = "customer";
con1.Close();
}
}
}
Posted by Thavachelvan Thiyaneshwaran at Monday, June 23, 2008
Newer Post Older Post Home

About Me

My photo
Thavachelvan Thiyaneshwaran
I have 12+ years of experience in the IT industry.
View my complete profile

Userfull Links

  • paymentgateway
  • java & oracle site
  • Free Templetes
  • torrent

Labels

  • C# (4)
  • Crystalreport (4)
  • Crystalreport 8.5 (1)
  • database mail (1)
  • database mirroing (1)
  • Database space (1)
  • datetime function (7)
  • Download (3)
  • image from resource (1)
  • mysql (1)
  • Oracle (4)
  • Oracle 11 (1)
  • Privacy Policy (1)
  • RDCMan (1)
  • Remote desktop connection manager (1)
  • reporting service 2008 (1)
  • Softwares (2)
  • sql (20)
  • sql debug (1)
  • sql server (9)
  • sql server 2005 (6)
  • sql server 2008 (1)
  • sql server transaction (1)
  • sql2005 (2)
  • sql2008 (1)
  • vb6 (1)
  • websites (1)

Blog Archive

  • ►  2013 (2)
    • ►  March 2013 (1)
    • ►  February 2013 (1)
  • ►  2012 (3)
    • ►  December 2012 (3)
  • ►  2011 (1)
    • ►  January 2011 (1)
  • ►  2010 (8)
    • ►  December 2010 (2)
    • ►  November 2010 (1)
    • ►  September 2010 (1)
    • ►  June 2010 (3)
    • ►  February 2010 (1)
  • ►  2009 (53)
    • ►  December 2009 (5)
    • ►  November 2009 (2)
    • ►  October 2009 (2)
    • ►  September 2009 (2)
    • ►  August 2009 (5)
    • ►  July 2009 (7)
    • ►  June 2009 (3)
    • ►  May 2009 (5)
    • ►  April 2009 (12)
    • ►  March 2009 (6)
    • ►  February 2009 (2)
    • ►  January 2009 (2)
  • ▼  2008 (154)
    • ►  December 2008 (2)
    • ►  November 2008 (4)
    • ►  October 2008 (7)
    • ►  September 2008 (19)
    • ►  August 2008 (32)
    • ►  July 2008 (55)
    • ▼  June 2008 (35)
      • Sources for Database Information in SQL Server 200...
      • myITforum.com : SQL Server 2005. Hidden system dat...
      • SQL-SCRIPT eSnips Folder
      • Decompile An Exe To Source Code - How to recognize it
      • Free C# Video tutorials - not the MSDN ones - Dev ...
      • Free Online Computer Science and Programming Books...
      • Calling Win32 Api from C# - csharp
      • Dislay Data on datagrid - csharp
      • Writing to EventLog - csharp
      • Numbers-Only Textbox - csharp
      • SQL Server 2005 Express Edition Feature Tour
      • Microsoft SQL Server: What's New in SQL Server 2005
      • Microsoft SQL Server: Top 30 Features of SQL Serve...
      • Introduction to Service Broker
      • WebGyver - Tools and Resources for Successful Web ...
      • Ajaxload - Ajax loading gif generator
      • SQL, database, informix, oracle, SQL Server,Free s...
      • .Net Programming and Tutorials - CoderSource.net
      • Scrolling Text on a Form using label control
      • C# .Net Delegates and events
      • Using Error Provider Control in Windows Forms and C#
      • Executing external applications using Process class
      • Sending emails in C#
      • Creating custom windows forms in C# with images
      • Creating a Splash Screen in C#
      • Google Answers: Microsoft Visual C# Method: External
      • Win32 Window Handler in C#
      • Finding and Listing Processes in C#
      • ASP 101 - Samples
      • Freelance Programming
      • How to AutoSize the DropDown portion of a ComboBox
      • Reading and Writing XML Documents
      • Auto create database connection string using INI b...
      • Passing Multiple Parameters to a Crystal Report Pr...
      • Using Microsoft Data Access Application Block
(c) Thava. Simple theme. Powered by Blogger.