C#调用C/C++ dll

来源:互联网 发布:第一创业交易软件 编辑:程序博客网 时间:2024/05/16 10:03

本文通过实例代码说明如何在托管的C#代码中调用非托管的Win32 API或者自己用C/C++写的Dll中的函数,以及如何传递输入、输出字符串参数,结构类型参数等问题。

 

Win32 C/C++ DLL代码如下(最终编译成dll.dll):

#include "stdafx.h"

#include <stdio.h>

 

#ifdef _MANAGED

#pragma managed(push, off)

#endif

//简单的函数调用测试

extern "C" __declspec(dllexport) int Test(void)

{

return 34;

}

//输入输出字符串传递测试

extern "C" __declspec(dllexport) int TCharPara(char *in, int len, char *out)

{

     memcpy(out, in, len);

 

return 0;

}

//结构参数传递测试

struct _XY

{

     int x;

     int y;

};

extern "C" __declspec(dllexport) int TStructPara(struct _XY *xy)

{

     xy->x = 5;

     xy->y = 5;

 

return 0;

}

//回调函数测试

typedef void (CALLBACK *CBFunc)(char *);

extern "C" __declspec(dllexport) int TCallback(CBFunc pf, char *out)

{

     (*pf)(out);

 

return 0;

}

#ifdef _MANAGED

#pragma managed(pop)

#endif

 

调用Win32 C/C++ dll中函数的托管C#代码如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

 

namespace TCallCDll

{

     //用于测试结构参数传递,对应Win32 dll中的结构_XY

    [StructLayout(LayoutKind.Sequential)]

    public struct _XY

    {

        public int x;

        public int y;

    };

     //Win32 C/C++ dll中的函数TCallback回调此函数

public delegate void OurDllCallBack(String iin);

//user32.dll中的EnumWindows函数回调此函数

    public delegate bool SysCallBack(int hwnd, int lParam);

 

    public class TCallCDll{

        [DllImport("dll.dll")]

        //////////////////////////////////////////////////////////////////////////

        //iin: 输入参数

        //oout: 输出参数

        //////////////////////////////////////////////////////////////////////////

        public static extern int TCharPara(String iin, int len, StringBuilder oout);

        //测试结构参数传递的函数

        [DllImport("dll.dll")]

        public static extern int TStructPara(ref _XY xy);

         //自己的dll函数回调测试

        [DllImport("dll.dll", CharSet = CharSet.Ansi)]

        public static extern void TCallback(OurDllCallBack f, String s);

         //user32.dll中的函数回调测试

        [DllImport("user32.dll")]

        public static extern int EnumWindows(SysCallBack x, int y);

 

    }

    public partial class callCDllMain : Form

    {

        public callCDllMain()

        {

            InitializeComponent();

        }

        //提供给dll.dll中的TCallback回调的函数

        public static void ff(String s)

        {

            Console.WriteLine(s);

        }

        //提供给user32.dll中的EnumWindows回调的函数

        public static bool Report(int hwnd, int lParam)

        {

            Console.Write("Window handle is ");

            Console.WriteLine(hwnd);

            return true;

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            StringBuilder a = new StringBuilder(3);

            TCallCDll.TCharPara("ABC", 3, a);

            this.Text = a.ToString();

 

            _XY xy = new _XY();

            TCallCDll.TStructPara(ref xy);

            this.Text += "x:" + xy.x + ", y:" + xy.y;

 

            OurDllCallBack fff = new OurDllCallBack(ff);

            TCallCDll.TCallback(fff, "This string is output in C&C++ dll call back function set in C# code/n");

 

            SysCallBack cb = new SysCallBack(Report);

            TCallCDll.EnumWindows(cb, 0);

        }

    }

}



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ZOLoveGD/archive/2009/04/03/4046845.aspx

原创粉丝点击