与C#中的Delphi DLL通讯。该函数的Delphi定义是
function oziCreateWP(var Name: pchar; Symbol: integer; lat, lon: double; Altitude: double;
wpDate: double; MapDisplayFormat: integer; PointerDirection: integer;
GarminDisplayFormat: integer; ForeColor, BackColor: integer;
ProximityDistance: integer; var Description: pchar; FontSize: integer;
FontStyle: integer; SymbolSize: integer): integer; stdcall;
该函数使用指定参数在指定位置创建一个航路点。返回值是所创建的Waypoint的内部OziExplorer编号。如果以后在程序中操纵Waypoint,则可能需要在本地存储该编号。对于大多数参数,可以将值指定为-1,这意味着使用OziExplorer默认值。**表示以上选项适用于该字段。
Name - the name of the waypoint Symbol - the symbol number; ** lat,lon - the position in decimal degrees (WGS 84 datum) Altitude - in meters (-777 = altitude unknown) wpDate - the format of the date is at the end of this document; -1 = use todays date and time. MapDisplayFormat - 0 to 8; ** PointerDirection - 0 to 3; ** GarminDisplayFormat - 0 to 2; ** ForeColor - RGB color; ** BackColor - RGB color; ** ProximityDistance - in meters (0 = no proximity) Description - text string FontSize - in points; ** FontStyle - 0 = normal; 1 = bold; ** SymbolSize - 17 = normal; **
我的C#定义是..
[DllImport("oziapi.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int oziCreateWP(ref string Name, int Symbol, double lat, double lon,
double Altitude = oziAltitudeNull, double wpDate = oziDefault,
int MapDisplayFormat = oziDefault, int PointerDirection = oziDefault,
int GarminDisplayFormat = oziDefault, int ForeColor = oziDefault,
int BackColor = oziDefault, int ProximityDistance = oziDefault,
ref string Description, int FontSize = oziDefault,
int FontStyle = oziDefault, int SymbolSize = oziDefault);
我的实现代码是..
private void btnCreateAManualWP_Click(object sender, EventArgs e)
{
int nTemp;
int nAlt = -777;
int nNegOne = -1;
int nZero = 0;
string cName = cWPName.Text;
string cDescription = "No Description";
nTemp = oziCreateWP(ref cName, nNegOne, Convert.ToDouble(cWPLat.Text),
Convert.ToDouble(cWPLon.Text), nAlt, nNegOne, nNegOne, nNegOne, nNegOne, nNegOne,
nNegOne, nZero, ref cDescription, nNegOne, nNegOne, nNegOne);
nTemp = oziRefreshMap();
}
有了这个,我得到了一个CS1737Optional parameters must appear after all required parameters
错误消息标记说明。如果我更正定义中的参数顺序和解决此问题的调用,则在运行时会收到“内存访问冲突”错误。
参数的顺序是二进制接口的一部分。接口两侧的参数顺序必须相同。如果您更改C#代码中的顺序,则需要更改Delphi代码以使其匹配。
大概不能更改Delphi代码。因此,您将需要在不更改参数顺序的情况下修复C#代码。通过删除参数默认设置来做到这一点。
这些ref string
参数几乎可以肯定是错误的,尽管不可能100%地确定没有更多细节的情况。库的文档可能会解释如何var PChar
处理参数。
如果SetLastError = true
是正确的,我会感到惊讶。这个库真的调用SetLastError
吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句