I am trying to call a C DLL from C#, but I'm not having any joy. The documentation for the DLL provides an example function delaration for VB that looks like;
Declare Function TransGeogPt Lib "c:\DLLS\GDAit.dll" (ByVal sGridFile As String, ByVal lDirection As
Long, ByVal dLat As Double, ByVal dLong As Double, pdLatNew As Double, pdLongNew As Double,
pdLatAcc As Double, pdLongAcc As Double) As Long
Declare Function TransProjPt Lib "c:\DLLS\GDAit.dll" (ByVal sGridFile As String, ByVal lDirection As
Long, ByVal dLat As Double, ByVal dLong As Double, ByVal lZone As Long, pdLatNew As Double,
pdLongNew As Double, pdLatAcc As Double, pdLongAcc As Double) As Long
I have therefore done the following;
public class GDAIt
{
public static string gridFileName = @"C:\Nat84.gsb";
[DllImport(@"c:\GDAit.dll")]
public static extern long TransGeogPt(string sGridFile, long lDirection, double dLat, double dLong, ref double pdLatNew, ref double pdLongNew, ref double pdLatAcc, ref double pdLongAcc);
[DllImport(@"c:\GDAit.dll")]
public static extern long TransProjPt(string sGridFile, long lDirection, double dLat, double dLong, long lZone, ref double pdLatNew, ref double pdLongNew, ref double pdLatAcc, ref double pdLongAcc);
public static long CallTransGeogPt(string sGridFile, long lDirection, double dLat, double dLong, ref double pdLatNew, ref double pdLongNew, ref double pdLatAcc, ref double pdLongAcc)
{
return TransGeogPt(sGridFile, lDirection, dLat, dLong, ref pdLatNew, ref pdLongNew, ref pdLatAcc, ref pdLongAcc);
}
public static long CallTransProjPt(string sGridFile, long lDirection, double dLat, double dLong, long lZone, ref double pdLatNew, ref double pdLongNew, ref double pdLatAcc, ref double pdLongAcc)
{
return TransProjPt(sGridFile, lDirection, dLat, dLong, lZone, ref pdLatNew, ref pdLongNew, ref pdLatAcc, ref pdLongAcc);
}
public static void Process()
{
double latitude = 0.0;
double longitude = 0.0;
double latAcc = 0.0;
double longAcc = 0.0;
long result = 0;
result = CallTransProjPt(gridFileName,
1,
394980,
7619799,
51,
ref latitude,
ref longitude,
ref latAcc,
ref longAcc);
Console.WriteLine(string.Format("Result was {0}, Lat: {1}, Long: {2}", result, latitude, longitude));
int error = Marshal.GetLastWin32Error();
Console.WriteLine(string.Format("Last error recieved was {0}", error));
}
}
I'm still not having much luck and have tried various other settings in the DLLImport statment such as; SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)
The output I get from the code is;
Result was 4690529317195612196, Lat: 0, Long: 0
Last error recieved was 6
If I'm right in looking at the info for Win32 errors, I think that refers to;
ERROR_INVALID_HANDLE The handle is invalid.
6 (0x6)
My guess is there is either a problem with passing in the filename as a string or the way I am passing doubles by ref? However, I really don't know, and I am at a loss as to how to investigate the issue further.
Any ideas are much appreciated.
Thanks.
You may want to define your c# signatures using marshalling attributes for your string parameters.
I'll also piggy-back on Mark Sowul's answer and say try calling with StdCall instead of Cdecl.
Also, as a precaution, I'd probably double-check to make sure that the compiler is set to compile x86 code, in case its compiling for 64-bit.
Looks like you aren't the only one to encounter that issue, have you tried StdCall? That worked for this fellow: http://www.mail-archive.com/delphi@ns3.123.co.nz/msg01227.html
I found the reason for my failed attempts by utilising a tool called; Microsoft(R) P/Invoke Interop Assistant as suggested by an answer on this thread.
I utilised this tool to input some of the C function prototypes and get it to generate the required C# prototype on my behalf. The C prototype looked like the following;
When entering this into the Interop assistant tool, it showed that rather than using longs (as I had done in my original question), these should be declared as an int. It produced the following output that meant my code above now worked as I'd hoped. Yay.
Thanks for everyones help with this.
Try changing
string sGridFile
toStringBuilder sGridFile
C++ has so many different kinds of strings that marshaling strings between manage and unmanaged code can be tricky.