1. 对象不可当参数,容易出现指针无法解析
比如string, 需要在导出为char *
#ifdef GRIDXY2LONLAT_EXPORTS
#define GRIDXY2LONLAT_API __declspec(dllexport)
#else
#define GRIDXY2LONLAT_API __declspec(dllimport)
#endif
extern "C" XXX_API int funca(char * AFileName);
.CPP
int funca(char* AFileName)
{
return classbb::GetInstance().funca(AFileName);
}
2 代码示例:
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime;
namespace WindowsFormsApplication
{
public partial class Form1 : Form
{
[DllImport(@"XXXXX.dll", EntryPoint = "funca", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
public static extern int <span style="font-family: Arial, Helvetica, sans-serif;">funca </span><span style="font-family: Arial, Helvetica, sans-serif;">( string pFileName);</span>
[DllImport(@"<span style="font-family: Arial, Helvetica, sans-serif;">XXXXX</span><span style="font-family: Arial, Helvetica, sans-serif;">.dll", EntryPoint = "</span><span style="font-family: Arial, Helvetica, sans-serif;">XXXXX</span><span style="font-family: Arial, Helvetica, sans-serif;">", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]</span>
public static extern int FUNB( double ALon, double ALat,ref int Aearthid,ref int Axoffset,ref int Ayoffset,int GridSize);
public Form1()
{
InitializeComponent();
}
string convert_xy_by_gridsize(int EarthID, double X, double Y, int gridSize)
{
return "'" + EarthID + "," + (int)(X >= 0 ? Math.Ceiling(X / gridSize) : Math.Floor(X / gridSize)) + "," +
(int)(Y >= 0 ? Math.Ceiling(Y / gridSize) : Math.Floor(Y / gridSize)) + "'";
}
private void button1_Click(object sender, EventArgs e)
{
int iResult = FUNA(" InitValue.csv");
double ALon = 103.71755642;
double ALat = 29.56135396;
//103.71755642,29.56135396
int Aearthid = 0; int Axoffset = 0; int Ayoffset = 0;
FUNCB(ALon, ALat, ref Aearthid, ref Axoffset, ref Ayoffset, 1);
int i = 0;
i++;
}
}
}