110,535
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
extern "C"
{
__declspec(dllexport) void DisplayHelloFromDLL()
{
printf ("Hello from DLL !\n");
}
}
using System;
using System.Runtime.InteropServices; // DLL support
class HelloWorld
{
[DllImport("TestLib.dll")]
public static extern void DisplayHelloFromDLL ();
static void Main ()
{
Console.WriteLine ("This is C# program");
DisplayHelloFromDLL ();
}
}
引用: using System.Runtime.InteropServices;
[DllImport("Kernel32")] //读取动态库文件
public static extern int GetProcAddress(int handle, String funcname);
[DllImport("Kernel32")]
public static extern int LoadLibrary(String funcname);
[DllImport("Kernel32")]
public static extern int FreeLibrary(int handle);
[DllImport("HmPark.dll")] //声明C++中的接口函数, 其中 HmPark 为你的动态库文件名
public static extern double GetStdCharge(UInt32 time_In, UInt32 time_Out);
private int huser32 = 0;
private void CountCharge()
{
double dCharge = 0;
try
{
huser32 = LoadLibrary("HmPark.dll"); //载入动态库
dCharge = GetStdCharge(time_In, time_Out); //调用 C++ 中的接口函数
}
catch (Exception ex)
{
MessageBox.Show("调用出错: " + ex.Message, "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
try
{
Inspect.FreeLibrary(huser32); //释放动态库文件, 否则会弹出异常
}
catch (Exception ee)
{
MessageBox.Show("释放出错: " + ee.Message, "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}