110,532
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace dllImport
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
static extern int LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
static extern IntPtr GetProcAddress(int hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);
[DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
static extern bool FreeLibrary(int hModule);
delegate int GetBusinessData1(string inputData, ref StringBuilder outputdata);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{ string _dllpath = @"G:\program\dllImport\Project2.dll";
string inputData = "1234";
StringBuilder AOutputData = new StringBuilder(213273);
int hModule = LoadLibrary(_dllpath);
if (hModule == 0)
return;
//得到指向Add函数的指针
IntPtr intPtr = GetProcAddress(hModule, "GetBusinessData");
GetBusinessData1 myfun = (GetBusinessData1)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(GetBusinessData1));
int iReturn = myfun(inputData, ref AOutputData);
MessageBox.Show(AOutputData.ToString());
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}