请问如果用C#需要调用一个C++的dll中的一个函数,这个函数需要传入windows句柄,在C#里有类似的句柄吗?
应该使用什么参数,怎么调用啊?
小弟拜谢了!!!!

解决方案 »

  1.   

    如果是win api的话看看这个
    http://www.pinvoke.net/default.aspx/netapi32/NetShareEnum.html
      

  2.   

    在.net中,每個Form和Control都有一個Handle屬性,就是句柄,類型為IntPtr
      

  3.   

    谢谢楼上各位,但我不能很好的理解
    劳烦各位再讲的细一点,比如说dll中现有这样的函数:
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    { ... }那么我在C#中怎样做能调用此函数呢?
    谢谢!!!
      

  4.   

    值类型用 int 
     引用句柄 都用 IntPtr[DllImport("Netapi32.dll")]
    int WinMain (int hInstance,
                  int hPrevInstance,
                string lpCmdLine,
                int nCmdShow);
       
      

  5.   

    请问楼上,这就是在C#中对DLL中的函数进行了声明,就可以在后面调用了,这样理解对吗?是不是用到DLL中的那些函数,都要在前面声明?
      

  6.   

    句柄是Windows操作系统的概念, 与语言无关.它是一个32位int值, 在C++中, 你可以用指针来表示它, 在C#中, 你可以用IntPtr或者Int32来表示他
      

  7.   

    谢谢楼上的,请问比如说我在4楼的那个函数,假设它需要一个父窗口作为容器,这个父窗口是C#中的form1,那么怎么把这个form1的句柄传入呢,就是说调用函数该怎么写(实参怎么写)?谢谢各位,我是个菜鸟,谢谢大家的帮助,问题一解决立刻结贴给分
      

  8.   

    int   APIENTRY   WinMain(HINSTANCE   hInstance, 
                                              HINSTANCE   hPrevInstance, 
                                              LPTSTR         lpCmdLine, 
                                              int               nCmdShow) 
    {   ...   } 
    --------------------
    在.net程序里调用Win32的入口函数?
      

  9.   

    谢谢楼上的,请问比如说我在4楼的那个函数,假设它需要一个父窗口作为容器,这个父窗口是C#中的form1,那么怎么把这个form1的句柄传入呢,就是说调用函数该怎么写(实参怎么写)?谢谢各位,我是个菜鸟,谢谢大家的帮助,问题一解决立刻结贴给分
    ----------------------
    WinMain和其他的API函数不一样的,它相当于C中的main,是要程序员自己去编写的,不是拿来调用的。
      

  10.   

    句柄其实就是C语言的void * 类型,在.net一般用IntPtr代替。
      

  11.   

    例子:
    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);
            }
        }
    }
      

  12.   

                            IntPtr   hdc   =   e.Graphics.GetHdc(); 
                            DrawText(hdc,   "Hello.",   6,   ref   rect,   0); 
    谢谢楼上的大哥!!~~