您需要導入非託管的DLL到C#.NET應用程序。我的例子返回一個指向結構的指針,但你可以提供你的應用需要的任何其他返回類型。一定要使用dumpbin.exe/EXPORTS從dll中獲取整個函數/ EntryPoint名稱。這裏是我的全班同學:
using System;
using System.Runtime.InteropServices;
namespace aviationLib
{
public struct MAGtype_GeoMagneticElements
{
public double Decl; /* 1. Angle between the magnetic field vector and true north, positive east*/
public double Incl; /*2. Angle between the magnetic field vector and the horizontal plane, positive down*/
public double F; /*3. Magnetic Field Strength*/
public double H; /*4. Horizontal Magnetic Field Strength*/
public double X; /*5. Northern component of the magnetic field vector*/
public double Y; /*6. Eastern component of the magnetic field vector*/
public double Z; /*7. Downward component of the magnetic field vector*/
public double GV; /*8. The Grid Variation*/
public double Decldot; /*9. Yearly Rate of change in declination*/
public double Incldot; /*10. Yearly Rate of change in inclination*/
public double Fdot; /*11. Yearly rate of change in Magnetic field strength*/
public double Hdot; /*12. Yearly rate of change in horizontal field strength*/
public double Xdot; /*13. Yearly rate of change in the northern component*/
public double Ydot; /*14. Yearly rate of change in the eastern component*/
public double Zdot; /*15. Yearly rate of change in the downward component*/
public double GVdot; /*16. Yearly rate of change in grid variation*/
};
public class Declination
{
[DllImport("wmm.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "[email protected]@[email protected]@[email protected]")]
public static extern IntPtr GeoMagneticElements(float sdate, int igdgc, int units, float alt, float latitude, float longitude);
public MAGtype_GeoMagneticElements e;
public MAGtype_GeoMagneticElements MagDeclination(float decimalLat, float decimalLon)
{
try
{
String d = DateTime.Now.Year.ToString("D4") + '.' + DateTime.Now.Day.ToString("D1");
IntPtr pnt = GeoMagneticElements((float)Convert.ToDouble(d), 1, 3, 3000.0f, decimalLat, decimalLon);
e = Marshal.PtrToStructure<MAGtype_GeoMagneticElements>(pnt);
}
catch (System.EntryPointNotFoundException se)
{
Console.WriteLine(se.Message);
}
return e;
}
}
}
Word設置必須爲同一兩個託管和非託管所以如果DLL是32位的,則.NET必須爲好。
沒有足夠的上下文。使用gcroot <>或Marshal :: GetFunctionPointerForDelegate()。 –