Call C DLL from C #

I am trying to call a C DLL from C #, but I have no joy. The documentation for the DLL provides an example function defragmentation for VB, which looks like this:

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 

So I did 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 still have no luck and have tried other settings in the status of DLLImport, such as; SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)

Exit code:

 Result was 4690529317195612196, Lat: 0, Long: 0 Last error recieved was 6 

If I am right when looking at Win32 error information, I think this applies; ERROR_INVALID_HANDLE The handle is invalid.
6 (0x6)

My guess: is there a problem with passing the file name as a string or the way I pass duplicates by reference? However, I really don’t know, and I don’t understand how to investigate this issue further.

Any ideas are greatly appreciated.

Thanks.

+9
source share
4 answers

I found the reason for my failed attempts using the called tool; Microsoft (R) P / Invoke Interop Assistant , as suggested by the answer to this thread .

I used this tool to input some prototypes of the C function and asked him to generate the required C # prototype on my behalf. Prototype C was as follows:

 long __stdcall TransProjPt(LPSTR psGridFile, long lDirection, double dEasting, double dNorthing, long lZone, double* pdEastNew, double* pdNorthNew, double* pdEastAcc, double* pdNorthAcc) 

When you enter this into the Interop helper tool, it showed that instead of using longs (as I did in my original question) they should be declared as int. He produced the following output, which meant that my code above worked as I hoped. Yay

  /// Return Type: int ///psGridFile: LPSTR->CHAR* ///lDirection: int ///dEasting: double ///dNorthing: double ///lZone: int ///pdEastNew: double* ///pdNorthNew: double* ///pdEastAcc: double* ///pdNorthAcc: double* [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="TransProjPt", CallingConvention=System.Runtime.InteropServices.CallingConvention.StdCall)] public static extern int TransProjPt([System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] System.Text.StringBuilder psGridFile, int lDirection, double dEasting, double dNorthing, int lZone, ref double pdEastNew, ref double pdNorthNew, ref double pdEastAcc, ref double pdNorthAcc) ; 

Thanks to everyone for helping with this.

+8
source

You can define your C # signatures using the marshalling attributes for your string parameters.

 [DllImport(@"c:\GDAit.dll")] public static extern long TransGeogPt([MarshalAs(UnmanagedType.LPStr)] 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([MarshalAs(UnmanagedType.LPStr)] string sGridFile, long lDirection, double dLat, double dLong, long lZone, ref double pdLatNew, ref double pdLongNew, ref double pdLatAcc, ref double pdLongAcc); 

I will also answer Mark Soul's answer and say, try to call StdCall instead of Cdecl.

Also, as a precaution, I would probably double-check to make sure the compiler is configured to compile x86 code, if compiled for 64-bit.

+3
source

Try changing string sGridFile to StringBuilder sGridFile

There are so many different types of lines in C ++ that marshaling lines between control and unmanaged code can be complicated.

+2
source

It seems you are not the only one who ran into this problem, have you tried StdCall? This worked for this person: http://www.mail-archive.com/ delphi@ns3.123.co.nz /msg01227.html

+1
source

Source: https://habr.com/ru/post/885911/


All Articles