例子: using System; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices;namespace sometest { public partial class frmMain : Form { [DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern int DrawText(IntPtr hdc, string lpStr, int nCount, ref Rect lpRect, int wFormat); private const int DT_CENTER = 0x1; private const int DT_VCENTER = 0x4; private const int DT_SINGLELINE = 0x20; private struct Rect { public int Left, Top, Right, Bottom; public Rect(Rectangle r) { this.Left = r.Left; this.Top = r.Top; this.Bottom = r.Bottom; this.Right = r.Right; } } private Rect rect; public frmMain() { InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { rect = new Rect(this.Bounds); } private void frmMain_Paint(object sender, PaintEventArgs e) {
http://www.pinvoke.net/default.aspx/netapi32/NetShareEnum.html
劳烦各位再讲的细一点,比如说dll中现有这样的函数:
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{ ... }那么我在C#中怎样做能调用此函数呢?
谢谢!!!
引用句柄 都用 IntPtr[DllImport("Netapi32.dll")]
int WinMain (int hInstance,
int hPrevInstance,
string lpCmdLine,
int nCmdShow);
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{ ... }
--------------------
在.net程序里调用Win32的入口函数?
----------------------
WinMain和其他的API函数不一样的,它相当于C中的main,是要程序员自己去编写的,不是拿来调用的。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace sometest
{
public partial class frmMain : Form
{
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern int DrawText(IntPtr hdc, string lpStr, int nCount, ref Rect lpRect, int wFormat); private const int DT_CENTER = 0x1;
private const int DT_VCENTER = 0x4;
private const int DT_SINGLELINE = 0x20; private struct Rect
{
public int Left, Top, Right, Bottom;
public Rect(Rectangle r)
{
this.Left = r.Left;
this.Top = r.Top;
this.Bottom = r.Bottom;
this.Right = r.Right;
} } private Rect rect; public frmMain()
{
InitializeComponent();
} private void frmMain_Load(object sender, EventArgs e)
{
rect = new Rect(this.Bounds);
} private void frmMain_Paint(object sender, PaintEventArgs e)
{
IntPtr hdc = e.Graphics.GetHdc();
DrawText(hdc, "Hello.", 6, ref rect, 0);
e.Graphics.ReleaseHdc(hdc);
}
}
}
DrawText(hdc, "Hello.", 6, ref rect, 0);
谢谢楼上的大哥!!~~