简体   繁体   中英

c++ dll using dllimport c#

I have a dll i wish to use in .net. I am trying to use dllimport but I am getting errors. I have put the dll in windows system32 folder and also where the exe is stored.the function definition I have been given that works in vb is.

        declare function LocalToWGS84 lib "TTDatum3.Dll"(Lat As Double, Lon As Double, ByVal Datum As Long) As Long

method sig

    #ifdef __cplusplus
extern "C" {
#endif

int __stdcall WGS84ToLocal(double * lat, double * longt, int d);

c#

  class Program
{

    [DllImport("TTDatum3.Dll", EntryPoint="LocalToWGS84")]
    public static extern long LocalToWGS84([In,Out]double lat, [In,Out]double lon,[In,Out]long datum);

    public static void Main(string[] args)
    {
        Console.WriteLine(LocalToWGS84(50.82011492,-0.117981131, 150));

    }
}

the error is:

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an idication that other memory is corrupt

Long in VB6 != Long in c#.net

Change Long to int

Hmm, without knowing the method signature your stabbing in the dark a bit eh, look for a reflector / decompiler tool that supports delphi, you could try some of these to get the method signature; DeDe seems the business (not sure how old the post is) Delphi Decompilers

You could try the MarshalAs attribute, but again theres a lot you could try. Without knowing the method signature it's very hit and miss.

Since the lat and lon parameters are defined as pointers to int's ( int* ) in the C definition you need to mark the parameters as ref . And the return value and the datum parameter should be an int and well:

 [DllImport("TTDatum3.Dll", EntryPoint="LocalToWGS84")]
 public static extern int LocalToWGS84([In,Out]ref double lat, [In,Out]ref double lon,[In,Out]int datum);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM